Title Description
Ink has bought a set of N color brushes (some of which may be the same color) in a row and you need to answer the ink question.Ink will issue the following instructions as you do:
1. The Q L R representative asks you how many different colors you have from the L brush to the R brush.
2. R P Col replaces the P-th brush with the color Col.
Do you know what you need to do in order to meet the requirements of ink?
Input and Output Formats
Input format:
The first line contains two integers, N and M, representing the number of initial brushes and the number of things ink does.
Line 2 N integers representing the color of the first brush in the initial brush row.
Lines 3 to 2+M, each representing one thing ink will do, format see the cadre section.
Output format:
For each Query query, you need to give a number in the corresponding line representing several different colors from the L to R brushes.
Input and Output Samples
6 5 1 2 3 4 5 5 Q 1 4 Q 2 6 R 1 2 Q 1 4 Q 2 6
4 4 3 4
Explain
For 100% of the data, N is less than 10000, M is less than 10000, modifications are less than 1000, and all integers present in all the input data are greater than or equal to 1 and less than 10^6.
Source: bzoj2120
The data for this topic is self-made data from Logu, using CYaRon It takes 5 minutes to complete the data production.
Nude Moose Team with Modifications
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 #include<cstdlib> 7 #include<ctime> 8 using namespace std; 9 const int MAXN=10001; 10 static void read(int &n) 11 { 12 char c='+';int x=0;bool flag=0; 13 while(c<'0'||c>'9'){c=getchar();if(c=='-')flag=1;} 14 while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c-48);c=getchar();} 15 flag==1?n=-x:n=x; 16 } 17 int n,m; 18 int a[MAXN]; 19 struct CX 20 { 21 int l,r,id,tm;// tm Last change operation 22 }cx[MAXN]; 23 int cxnum; 24 struct GG 25 { 26 int pos,val,pre; 27 }gg[MAXN]; 28 int ggnum; 29 int head[MAXN]; 30 int where[MAXN]; 31 int base; 32 int vis[MAXN];// Is there a change operation 33 int color[MAXN]; 34 int ans=0; 35 int out[MAXN]; 36 int comp(const CX &a,const CX &b) 37 { 38 if(where[a.l]==where[b.l]) 39 return a.r<b.r; 40 else 41 return where[a.l]<where[b.l]; 42 } 43 int calc(int x) 44 { 45 if(vis[x]) 46 { 47 if(--color[a[x]]==0) 48 ans--; 49 } 50 else 51 { 52 if(++color[a[x]]==1) 53 ans++; 54 } 55 vis[x]=!vis[x]; 56 } 57 void change(int p,int v) 58 { 59 if(vis[p]) 60 { 61 calc(p); 62 a[p]=v; 63 calc(p); 64 } 65 else 66 a[p]=v; 67 } 68 69 int main() 70 { 71 read(n);read(m); 72 for(int i=1;i<=n;i++) 73 read(a[i]),head[i]=a[i]; 74 base=sqrt(n); 75 for(int i=1;i<=n;i++) 76 where[i]=(i-1)/base+1; 77 for(int i=1;i<=m;i++) 78 { 79 char c; 80 int x,y; 81 cin>>c; 82 read(x);read(y); 83 if(c=='Q')// query 84 { 85 cxnum++; 86 cx[cxnum].l=x; 87 cx[cxnum].r=y; 88 cx[cxnum].id=cxnum; 89 cx[cxnum].tm=ggnum; 90 } 91 else 92 { 93 ggnum++; 94 gg[ggnum].pos=x; 95 gg[ggnum].val=y; 96 gg[ggnum].pre=head[x]; 97 head[x]=y; 98 } 99 } 100 sort(cx+1,cx+cxnum+1,comp); 101 int l=1,r=0; 102 for(int i=1;i<=cxnum;i++) 103 { 104 for(int j=cx[i-1].tm+1;j<=cx[i].tm;j++) 105 change(gg[j].pos,gg[j].val); 106 for(int j=cx[i-1].tm;j>=cx[i].tm+1;j--) 107 change(gg[j].pos,gg[j].pre);// Here is pre,No val!!! 108 for(;l<cx[i].l;l++) 109 calc(l); 110 for(;l>cx[i].l;l--) 111 calc(l-1); 112 for(;r<cx[i].r;r++) 113 calc(r+1); 114 for(;r>cx[i].r;r--) 115 calc(r); 116 out[cx[i].id]=ans; 117 } 118 for(int i=1;i<=cxnum;i++) 119 printf("%d\n",out[i]); 120 return 0; 121 }