bzoj 2120: 数颜色

来源:互联网 发布:手机网络文件共享访问 编辑:程序博客网 时间:2024/05/16 18:13

Description

墨墨购买了一套N支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会像你发布如下指令: 1、 Q L R代表询问你从第L支画笔到第R支画笔中共有几种不同颜色的画笔。 2、 R P Col 把第P支画笔替换为颜色Col。为了满足墨墨的要求,你知道你需要干什么了吗?

Input

第1行两个整数N,M,分别代表初始画笔的数量以及墨墨会做的事情的个数。第2行N个整数,分别代表初始画笔排中第i支画笔的颜色。第3行到第2+M行,每行分别代表墨墨会做的一件事情,格式见题干部分。

Output

对于每一个Query的询问,你需要在对应的行中给出一个数字,代表第L支画笔到第R支画笔中共有几种不同颜色的画笔。

Sample Input

6 5
1 2 3 4 5 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6

Sample Output

4
4
3
4

HINT

对于100%的数据,N≤10000,M≤10000,修改操作不多于1000次,所有的输入数据中出现的所有整数均大于等于1且不超过10^6。


本来想写个分块结果放弃了。。

和糖果公园一样的莫队搞法就可以过了

#include<cmath>#include<cstdio>#include<algorithm>using namespace std;int a[10001],b[10001];struct ask{     int l,r;     int t,p;     int x;}as[10001];struct change{     int l,x;     int la;}ch[10001];int belong[10001];int s[1000001];bool v[10001];int nt,sx;int ans;inline bool cmp1(ask x,ask y){     if(belong[x.l]<belong[y.l])          return true;     if(belong[x.l]==belong[y.l])     {          if(x.r<y.r)               return true;          if(x.r==y.r&&x.t<y.t)               return true;     }     return false;}inline bool cmp2(ask x,ask y){     if(x.p<y.p)          return true;     return false;}inline void turn(int l){     if(v[l])     {          s[a[l]]--;          if(s[a[l]]==0)               ans--;          v[l]=false;     }     else     {          s[a[l]]++;          if(s[a[l]]==1)               ans++;          v[l]=true;     }}inline void adt(int t){ int loc=ch[t].l,co=ch[t].x; if(v[loc]) {      s[a[loc]]--;      if(s[a[loc]]==0)           ans--;          a[loc]=co;          s[co]++;          if(s[co]==1)               ans++;     }     else          a[loc]=co;}inline void inct(int t){ int loc=ch[t].l,co=ch[t].la; if(v[loc]) {      s[a[loc]]--;      if(s[a[loc]]==0)           ans--;          a[loc]=co;          s[co]++;          if(s[co]==1)               ans++;     }     else          a[loc]=co;}int main(){     int n,m;     scanf("%d%d",&n,&m);     int i;     for(i=1;i<=n;i++)     {          scanf("%d",&a[i]);          b[i]=a[i];     }     nt=sqrt(n);     sx=(n-1)/nt+1;     for(i=1;i<=n;i++)          belong[i]=(i-1)/nt+1;     int p1=0,p2=0;     char x[4];     for(i=1;i<=m;i++)     {       scanf("%s",x);       if(x[0]=='Q')       {          p1++;               scanf("%d%d",&as[p1].l,&as[p1].r);               as[p1].t=p2;               as[p1].p=p1;          }          else          {             p2++;               scanf("%d%d",&ch[p2].l,&ch[p2].x);               ch[p2].la=b[ch[p2].l];               b[ch[p2].l]=ch[p2].x;          }     }     sort(as+1,as+1+p1,cmp1);     int l=1,r=0,t=0;     for(i=1;i<=m;i++)     {          while(l<as[i].l)          {             turn(l);       l++;          }          while(l>as[i].l)          {               l--;               turn(l);          }          while(r<as[i].r)          {           r++;             turn(r);          }          while(r>as[i].r)          {               turn(r);               r--;          }          while(t<as[i].t)          {               t++;               adt(t);          }          while(t>as[i].t)          {               inct(t);               t--;          }                    as[i].x=ans;     }     sort(as+1,as+1+p1,cmp2);     for(i=1;i<=p1;i++)          printf("%d\n",as[i].x);     return 0;}


0 0
原创粉丝点击