zoukankan      html  css  js  c++  java
  • 1619. [HEOI2012]采花

    1619. [HEOI2012]采花

    ★★☆   输入文件:1flower.in   输出文件:1flower.out   简单对比
    时间限制:5 s   内存限制:128 MB

    【题目描述】

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

    【输入格式】


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

    【输出格式】

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

    【样例输入】

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

    【样例输出】

    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的花各一朵,公主不采。
      

    【提示】


    【数据范围】

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


    【来源】

     

    【题目来源】

    耒阳大世界(衡阳八中) OJ 2743

    一开始以为这题是线段树,,后来发现线段树好像没有这个功能

    然后就想莫队,看了看时间发现还有一个半小时,以为这道题做不出来了,就打了个暴力去做T2

    临收卷还有二十分钟左右的时候老师说T3莫队20分,(后来我用莫队AC了,,,实力打脸)

    暴力思路:暴力

    正解:

    1.裸莫队

    复制代码
     1     #include<iostream>
     2     #include<cstdio>
     3     #include<cstring>
     4     #include<cmath>
     5     #include<algorithm>
     6     using namespace std;
     7     const int MAXN=1000001;
     8     int colornum[MAXN],n,color,m,a[MAXN];
     9     int base,pos[MAXN],out[MAXN];
    10     struct node
    11     {
    12         int l,r,id;
    13     }q[MAXN];
    14     int ans=0;
    15     int read(int & n)
    16     {
    17         char c='/';int flag=0,x=0;
    18         while(c<'0'||c>'9')
    19         {c=getchar();}
    20         while(c>='0'&&c<='9')
    21         {x=(x<<3)+(x<<1)+(c-48);
    22         c=getchar();}
    23         n=x;
    24     }
    25     inline void dele(int where)
    26     {
    27         if(colornum[a[where]]==2)
    28             ans--;
    29         colornum[a[where]]--;
    30     }
    31     inline void add(int where)
    32     {
    33         if(colornum[a[where]]==1)
    34             ans++;
    35         colornum[a[where]]++;
    36     }
    37     inline int comp(const node & a,const node & b)
    38     {
    39         if(pos[a.l]==pos[b.l])
    40             return a.r<b.r;
    41         else return pos[a.l]<pos[b.l];
    42     }
    43     inline void modui()
    44     {
    45         int l=1,r=0;
    46         for(int i=1;i<=m;++i)
    47         {
    48             for(;l<q[i].l;++l)
    49                 dele(l);
    50             for(;l>q[i].l;--l)
    51                 add(l-1);
    52             for(;r<q[i].r;++r)
    53                 add(r+1);
    54             for(;r>q[i].r;--r)
    55                 dele(r);
    56             out[q[i].id]=ans;
    57         }
    58     }
    59     int main()
    60     {
    61         freopen("1flower.in","r",stdin);
    62         freopen("1flower.out","w",stdout);
    63         read(n);read(color);read(m);
    64         base=sqrt(n);
    65         for(int i=1;i<=n;++i)
    66             read(a[i]);
    67         for(int i=1;i<=n;++i)
    68             pos[i]=(i-1)/base+1;
    69         for(int i=1;i<=m;++i)
    70         {
    71             read(q[i].l);
    72             read(q[i].r);
    73             q[i].id=i;
    74         }
    75         sort(q+1,q+m+1,comp);
    76         modui();
    77         for(int i=1;i<=m;++i)
    78         {
    79             printf("%d
    ",out[i]);
    80         }
    81         return 0;
    82     }
    复制代码

    2.因为一个颜色只有出现两次的时候才会被采

    那么我们可以记录这个颜色出现的位置,但这个颜色出现的次数达到两次的时候,我们把这个区间+1

    维护区间可以用树状数组实现

    复制代码
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 #include<algorithm>
     6 using namespace std;
     7 const int MAXN=1000001;
     8 int a[MAXN];
     9 int lb(int x)
    10 {return x&-x;}
    11 int read(int & n)
    12 {
    13     char c='/';int flag=0,x=0;
    14     while(c<'0'||c>'9')
    15     {if(c=='-')flag=1;
    16     c=getchar();}
    17     while(c>='0'&&c<='9')
    18     {x=x*10+(c-48);
    19     c=getchar();}
    20     if(flag)n=-x;
    21     else n=x;
    22 }
    23 int n,colornum,m;
    24 struct node
    25 {
    26     int l,r,id;
    27 }q[MAXN];
    28 int first[MAXN],second[MAXN],tree[MAXN],ans[MAXN];
    29 int comp(const node & a ,const node & b)
    30 {return a.r<b.r;}
    31 void add(int pos,int v)
    32 {
    33     while(pos<=n)
    34     {
    35         tree[pos]+=v;
    36         pos=pos+lb(pos);    
    37     }
    38     
    39 }
    40 int query(int pos)
    41 {
    42     int tot=0;
    43     while(pos)
    44     {
    45         tot=tot+tree[pos];
    46         pos=pos-lb(pos);// 等差序列维护所以从尾加到头 
    47     }
    48     return tot;
    49 }
    50 int main()
    51 {
    52     //freopen("1flower.in","r",stdin);
    53     //freopen("1flower.out","w",stdout);
    54     read(n);read(colornum);read(m);
    55     for(int i=1;i<=n;i++)
    56     {
    57         read(a[i]);
    58         second[i]=first[a[i]];// 如果a[i]出现过的话,那么second一定是记录的第二次的位置 
    59         first[a[i]]=i;
    60     }
    61     for(int i=1;i<=m;i++)
    62     {
    63         read(q[i].l);read(q[i].r);
    64         q[i].id=i;
    65     }
    66     sort(q+1,q+m+1,comp);
    67     int last=1;
    68     for(int i=1;i<=n;i++)
    69     {
    70         if(second[i])
    71         {
    72             add(second[i]+1,-1);
    73             add(second[second[i]]+1,1);
    74         }
    75         while(i==q[last].r)
    76         {
    77             ans[q[last].id]=query(q[last].l);
    78             last++;
    79         }
    80     }
    81     for(int i=1;i<=m;i++)
    82         printf("%d
    ",ans[i]);
    83     return 0;
    84 }
  • 相关阅读:
    雷霆战机
    各种 Python 库/模块/工具
    redis
    25
    为什么Python中“2==2>1”结果为True
    thinkphp3.2路由美化,url简化
    thinkphp调整框架核心目录think的位置
    thinkphp3.2中开启静态缓存后对404页面的处理方法
    thinphp中volist嵌套循环时变量$i 被污染问题,key="k"
    thinkphp中如何是实现多表查询
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6914169.html
Copyright © 2011-2022 走看看