BZOJ2120 数颜色(带修改莫队)

本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作。

 

 


本文作者:ljh2000
作者博客:http://www.cnblogs.com/ljh2000-jump/
转载请注明出处,侵权必究,保留最终解释权!

 

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。

 

 
正解:带修改莫队
解题报告:
  这道题的数据范围很小,以前写过两次,不过用的是暴力和分块。
  今天学可修改的莫队,发现这是一道模板题就顺便写了。
  因为莫队是通过改变询问的顺序来降低整体复杂度,而带修改的话就没办法处理先后顺序的问题了。我们考虑加入修改操作之后如何保证算法复杂度。
  因为查询操作只有在查询操作之前的所有修改操作完成之后才能保证正确性,也就是说我只要记录了之前有多少个修改操作,然后在执行到当前查询的时候我把多进行的修改操作还原,少进行的再进行修改就可以保证我的正确性。
  同时为了保证复杂度,采取的策略是对于每个询问按$(l/block,r/block,time)$排序(分别表示l所在的块、r所在的询问之前的修改次数),再顺序完成,根据复杂度证明可以保证复杂度上界为$O(n^ {\frac{5}{3}} )$。
 
 1 //It is made by ljh2000
 2 #include <iostream>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <cstdio>
 6 #include <cmath>
 7 #include <algorithm>
 8 #include <ctime>
 9 #include <vector>
10 #include <queue>
11 #include <map>
12 #include <set>
13 #include <string>
14 using namespace std;
15 typedef long long LL;
16 const int MAXN = 10011;
17 int n,m,a[MAXN],last[MAXN],cnt1,cnt2,block;
18 int L,R,head,ans,cnt[1000011],A[MAXN];
19 char ch[12];
20 struct ask{int l,r,lb,rb,tim,id;}q[MAXN];
21 struct modify{int x,y,last;}r[MAXN];
22 inline bool cmp(ask q,ask qq){ 
23     if(q.lb==qq.lb) {
24         if(q.rb==qq.rb) return q.tim<qq.tim;
25         return q.rb<qq.rb;
26     }
27     return q.lb<qq.lb;
28 }
29 inline int getb(int x){ return (x-1)/block+1; }
30 inline int getint(){
31     int w=0,q=0; char c=getchar(); while((c<'0'||c>'9') && c!='-') c=getchar();
32     if(c=='-') q=1,c=getchar(); while (c>='0'&&c<='9') w=w*10+c-'0',c=getchar(); return q?-w:w;
33 }
34 
35 inline void change(int x,int col){
36     if(L<=x&&x<=R) {
37         cnt[a[x]]--; if(cnt[a[x]]==0) ans--;
38         a[x]=col;
39         if(cnt[a[x]]==0) ans++;    cnt[a[x]]++; 
40     }
41     else a[x]=col;
42 }
43 
44 inline void update(int x,int type){
45     int cun=cnt[a[x]]; cnt[a[x]]+=type;
46     if(cun==0 && cnt[a[x]]==1) ans++;
47     else if(cun==1 && cnt[a[x]]==0) ans--;
48 }
49 
50 inline void work(){
51     n=getint(); m=getint();    for(int i=1;i<=n;i++) a[i]=getint(),last[i]=a[i];
52     block=sqrt(n);
53     for(int i=1;i<=m;i++) {
54         scanf("%s",ch);
55         if(ch[0]=='R') {
56             r[++cnt1].x=getint();
57             r[cnt1].y=getint();
58             r[cnt1].last=last[r[cnt1].x];
59             last[r[cnt1].x]=r[cnt1].y;
60         }
61         else{
62             q[++cnt2].l=getint(); q[cnt2].r=getint(); q[cnt2].id=cnt2;
63             q[cnt2].lb=getb(q[cnt2].l); q[cnt2].rb=getb(q[cnt2].r);
64             q[cnt2].tim=cnt1; 
65         }
66     }
67     sort(q+1,q+cnt2+1,cmp); L=1; R=0; head=0;
68     for(int i=1;i<=cnt2;i++) {
69         while(head>q[i].tim) {
70             change(r[head].x,r[head].last);
71             head--;
72         }
73         while(head<q[i].tim) {
74             head++;
75             change(r[head].x,r[head].y);
76         }
77         while(R<q[i].r) R++,update(R,1);
78         while(L>q[i].l) L--,update(L,1);
79         while(R>q[i].r) update(R,-1),R--;
80         while(L<q[i].l) update(L,-1),L++;
81         A[q[i].id]=ans;
82     }
83     for(int i=1;i<=cnt2;i++) printf("%d\n",A[i]);
84 }
85 
86 int main()
87 {
88     work();
89     return 0;
90 }

 

posted @ 2016-12-25 14:08  ljh_2000  阅读(2975)  评论(4编辑  收藏  举报