暴力,,最暴力。。。。当然,用分块做也是可以的,用带修莫队也是可以的
1 #include<bits/stdc++.h> 2 #define N 100005 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 int n,m,T,f[11005],a[10005],cnt; 14 map<int ,int > mp; 15 int main() 16 { 17 n=ra(); m=ra(); 18 for (int i=1; i<=n; i++) 19 { 20 int x; 21 if (!mp[x=ra()]) 22 mp[x]=++cnt; 23 a[i]=mp[x]; 24 } 25 for (int i=1; i<=m; i++) 26 { 27 char s[3]; scanf("%s",s); 28 if (s[0]=='Q') 29 { 30 int ans=0; T++; 31 int x=ra(),y=ra(); 32 for (int j=x; j<=y; j++) 33 if (f[a[j]]!=T) 34 ans++,f[a[j]]=T; 35 printf("%d ",ans); 36 } 37 else 38 { 39 int pos=ra(),col=ra(); 40 if (!mp[col]) 41 mp[col]=++cnt; 42 a[pos]=mp[col]; 43 } 44 } 45 return 0; 46 }
1 #include<bits/stdc++.h> 2 #define N 1000005 3 #define LL long long 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 inline int ra() 7 { 8 int x=0,f=1; char ch=getchar(); 9 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 10 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 11 return x*f; 12 } 13 struct node{int pos,col,pre;}w[N]; 14 struct data{int l,r,id,time;}q[N]; 15 int n,m,pos[N],last[N],col[N],cnt,sz,num[N],ans[N],ans1; 16 bool vis[N]; 17 int cmp(data a, data b) 18 { 19 if (pos[a.l]==pos[b.l]) 20 { 21 if (a.r==b.r) return a.time<b.time; 22 return a.r<b.r; 23 } 24 return pos[a.l]<pos[b.l]; 25 } 26 void cal(int x) 27 { 28 if (vis[x]) 29 { 30 if (!--num[col[x]]) ans1--; 31 } 32 else 33 { 34 if (++num[col[x]]==1) ans1++; 35 } 36 vis[x]^=1; 37 } 38 void change(int x, int c) 39 { 40 if (vis[x]) 41 { 42 cal(x); 43 col[x]=c; 44 cal(x); 45 } 46 else col[x]=c; 47 } 48 int main() 49 { 50 n=ra(); m=ra(); 51 for (int i=1; i<=n; i++) col[i]=ra(),last[i]=col[i]; 52 for (int i=1; i<=m; i++) 53 { 54 char s[3]; scanf("%s",s); 55 int x=ra(),y=ra(); 56 if (s[0]=='R') 57 { 58 w[++cnt].pos=x; w[cnt].col=y; w[cnt].pre=last[x]; 59 last[x]=y; 60 } 61 else 62 { 63 q[++sz].l=x; q[sz].r=y; q[sz].id=sz; q[sz].time=cnt; 64 } 65 } 66 int block=sqrt(n); 67 for (int i=1; i<=n; i++) 68 pos[i]=(i-1)*block+1; 69 sort(q+1,q+sz+1,cmp); 70 int l=1,r=1; cal(1); 71 for (int i=1; i<=m; i++) 72 { 73 for (int j=q[i-1].time+1; j<=q[i].time; j++) change(w[j].pos,w[j].col); 74 for (int j=q[i-1].time; j>q[i].time; j--) change(w[j].pos,w[j].pre); 75 while (l<q[i].l) cal(l++); 76 while (l>q[i].l) cal(--l); 77 while (r<q[i].r) cal(++r); 78 while (r>q[i].r) cal(r--); 79 ans[q[i].id]=ans1; 80 } 81 for (int i=1; i<=sz; i++) printf("%d ",ans[i]); 82 return 0; 83 }