zoukankan      html  css  js  c++  java
  • BZOJ-2743: [HEOI2012]采花(树状数组 or TLE莫队)

    2743: [HEOI2012]采花

    Time Limit: 15 Sec  Memory Limit: 128 MB
    Submit: 2512  Solved: 1292
    [Submit][Status][Discuss]

    Description

    萧芸斓是Z国的公主,平时的一大爱好是采花。
    今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花。花园足够大,容纳了n朵花,花有c种颜色(用整数1-c表示),且花是排成一排的,以便于公主采花。公主每次采花后会统计采到的花的颜色数,颜色数越多她会越高兴!同时,她有一癖好,她不允许最后自己采到的花中,某一颜色的花只有一朵。为此,公主每采一朵花,要么此前已采到此颜色的花,要么有相当正确的直觉告诉她,她必能再次采到此颜色的花。由于时间关系,公主只能走过花园连续的一段进行采花,便让女仆福涵洁安排行程。福涵洁综合各种因素拟定了m个行程,然后一一向你询问公主能采到多少朵花(她知道你是编程高手,定能快速给出答案!),最后会选择令公主最高兴的行程(为了拿到更多奖金!)。

    Input

     第一行四个空格隔开的整数n、c以及m。接下来一行n个空格隔开的整数,每个数在[1, c]间,第i个数表示第i朵花的颜色。接下来m行每行两个空格隔开的整数l和r(l ≤ r),表示女仆安排的行程为公主经过第l到第r朵花进行采花。

    Output

     
    共m行,每行一个整数,第i个数表示公主在女仆的第i个行程中能采到的花的颜色数。

    Sample Input

    5 3 5
    1 2 2 3 1
    1 5
    1 2
    2 2
    2 3
    3 5

    Sample Output

    2
    0
    0
    1
    0
    【样例说明】
    询问[1, 5]:公主采颜色为1和2的花,由于颜色3的花只有一朵,公主不采;询问[1, 2]:颜色1和颜色2的花均只有一朵,公主不采;
    询问[2, 2]:颜色2的花只有一朵,公主不采;
    询问[2, 3]:由于颜色2的花有两朵,公主采颜色2的花;
    询问[3, 5]:颜色1、2、3的花各一朵,公主不采。

    HINT

    【数据范围】

    对于100%的数据,1 ≤ n ≤    10^6,c ≤ n,m ≤10^6。

    Source

    看到题目第一眼……mmp这不是傻逼莫队嘛……于是怒怼了一个莫队上去,评测的时候laj还想说laj又做了bzoj上的一道水题呢mmp结果先是PE结果后来全是TLE……一看数据mmp 1e6 莫队个jb啊(好吧这道傻逼题搞了我两节半课才搞好QAQ) 

    先贴个莫队的TLE代码。。

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=1e6+5;
     5 int n,m,faq;
     6 int a[MAX],b[MAX],pos[MAX],bas,ans,an[MAX];
     7 struct Node{
     8     int id;
     9     int l,r;
    10     bool operator < (const Node &tt) const {
    11         if (pos[l]!=pos[tt.l])
    12             return pos[l]<pos[tt.l];
    13         return r<tt.r;
    14     }
    15 }que[MAX];
    16 inline int read(){
    17     int an=0,x=1;char c=getchar();
    18     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
    19     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
    20     return an*x;
    21 }
    22 void update(int x,int y){
    23     ans-=(b[a[x]]>1?1:0);
    24     b[a[x]]+=y;
    25     ans+=(b[a[x]]>1?1:0);
    26 }
    27 int main(){
    28     freopen ("flower.in","r",stdin);
    29     freopen ("flower.out","w",stdout);
    30     register int i,j;
    31     n=read(),faq=read(),m=read();bas=(int)sqrt(n*1.0);
    32     for (i=1;i<=n;++i) a[i]=read(),pos[i]=i/bas;
    33     for (i=1;i<=m;++i){
    34         que[i].id=i,
    35         que[i].l=read(),que[i].r=read();
    36     }
    37     sort(que+1,que+m+1);
    38     memset(b,0,sizeof(b));
    39     register int L=1,R=0;
    40     for (i=1;i<=m;++i){
    41         while (R<que[i].r) update(++R,1);
    42         while (L>que[i].l) update(--L,1);
    43         while (R>que[i].r) update(R--,-1);
    44         while (L<que[i].l) update(L++,-1);
    45         an[que[i].id]=ans;
    46     }
    47     for (i=1;i<m;++i)
    48         printf("%d
    ",an[i]);
    49     printf("%d",an[m]);
    50     return 0;
    51 }

    好吧好吧滚去写树状数组,看懂了hzwer的HH的项链还有这题,又在luogu上TLE了无数遍最后终于AC了mmp

    为什么不写思路呢……因为我也讲不出来,只可意会,不可言传 ovo 应该说HH的项链是初始化和每次操作都是把下一位标1,此题首先初始化就把下下位标1然后操作是把下一位(也就是上一位的下下位)去掉,再把当前位的下下位标1。。。

     1 #include "bits/stdc++.h"
     2 using namespace std;
     3 typedef long long LL;
     4 const int MAX=1e6+5;
     5 int n,m,faq;
     6 int a[MAX],b[MAX],next[MAX],p[MAX],an[MAX];
     7 struct Node{
     8     int id;
     9     int l,r;
    10     bool operator < (const Node &tt) const {return l<tt.l;}
    11 }que[MAX];
    12 inline int read(){
    13     int an=0,x=1;char c=getchar();
    14     while (c<'0' || c>'9') {if (c=='-') x=-1;c=getchar();}
    15     while (c>='0' && c<='9') {an=an*10+c-'0';c=getchar();}
    16     return an*x;
    17 }
    18 void add(int x,int y){for (;x<=n;x+=(x&-x)) b[x]+=y;}
    19 inline int search(int x){int an=0;for (;x>0;x-=(x&-x)) an+=b[x];return an;}
    20 int main(){
    21     freopen ("flower.in","r",stdin);
    22     freopen ("flower.out","w",stdout);
    23     register int i,j;
    24     n=read(),faq=read(),m=read();
    25     for (i=1;i<=n;++i) a[i]=read();
    26     for (i=1;i<=m;++i){
    27         que[i].id=i,
    28         que[i].l=read(),que[i].r=read();
    29     }
    30     sort(que+1,que+m+1);
    31     memset(b,0,sizeof(b));memset(p,0,sizeof(p));
    32     for (i=n;i>=1;i--) next[i]=p[a[i]],p[a[i]]=i;
    33     for (i=1;i<=faq;i++) if (next[p[i]]) add(next[p[i]],1);
    34     int L=1;
    35     for (i=1;i<=m;i++){
    36         while (L<que[i].l){
    37             if (next[L]) add(next[L],-1);
    38             if (next[next[L]]) add(next[next[L]],1);
    39             L++;
    40         }
    41         an[que[i].id]=search(que[i].r)-search(que[i].l-1);
    42     }
    43     for (i=1;i<=m;i++) printf("%d
    ",an[i]);
    44     return 0;
    45 }
    未来是什么样,未来会发生什么,谁也不知道。 但是我知道, 起码从今天开始努力, 肯定比从明天开始努力, 要快一天实现梦想。 千里之行,始于足下! ——《那年那兔那些事儿》
  • 相关阅读:
    剑桥雅思写作高分范文ESSAY96
    剑桥雅思写作高分范文ESSAY95
    剑桥雅思写作高分范文ESSAY94
    剑桥雅思写作高分范文ESSAY93
    剑桥雅思写作高分范文ESSAY92
    剑桥雅思写作高分范文ESSAY91
    PHP 伪静态规则写法RewriteRule-htaccess详细语法使用
    php页面静态常用函数
    正则函数[原则,能用字符串函数解决不用正则,速度问题]s
    使用xshell文件传输
  • 原文地址:https://www.cnblogs.com/keximeiruguo/p/7693580.html
Copyright © 2011-2022 走看看