2120: Number color time limit: 6 sec memory limit: 259 MB
Submit: 1394 solved: 516
[Submit] [Status] Description
Mo mo purchased a set of n color brushes (some of which may have the same color) and placed them in a row. You need to answer Mo's questions. Ink and ink will send the following command as you do: 1. q l r indicates that you have several different colors from the l paint brush to the r paint brush. 2. R p col replaces the P paint brush with the color Col. Do you know what you need to do to meet Momo's requirements?
Input
Two integers (N and M) in the row 1st represent the number of the initial paint brushes and the number of tasks that ink and ink will do. N integers in row 2nd, representing the I-th paint brush color in the first paint brush row, respectively. Lines from 3rd to 2nd + M represent one thing that Mo mo will do. For details about the format, refer to cadre points.
Output
For each query, you need to give a number in the corresponding row, representing the number of different colors from the l paint brush to the r paint brush.
Sample input6 5
1 2 3 4 5
Q 1 4
Q 2 6
R 1 2
Q 1 4
Q 2 6
Sample output4
4
3
4
Hint
For 100% of data, n ≤ 10000, m ≤ 10000, the modification operation is no more than 1000 times. All integers in all input data are greater than or equal to 1 and cannot exceed 10 ^ 6.
Source
Question:
Same maintenance queue
Code:
1 #include<cstdio> 2 #include<cstdlib> 3 #include<cmath> 4 #include<cstring> 5 #include<algorithm> 6 #include<iostream> 7 #include<vector> 8 #include<map> 9 #include<set>10 #include<queue>11 #include<string>12 #define inf 100000000013 #define maxn 10000+100014 #define maxm 1000000+100015 #define eps 1e-1016 #define ll long long17 #define pa pair<int,int>18 using namespace std;19 inline int read()20 {21 int x=0,f=1;char ch=getchar();22 while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}23 while(ch>=‘0‘&&ch<=‘9‘){x=10*x+ch-‘0‘;ch=getchar();}24 return x*f;25 }26 int n,m,block,b[maxn],c[maxn],pre[maxn],pos[maxn],last[maxm];27 void reset(int x)28 {29 int l=(x-1)*block+1,r=min(x*block,n);30 for(int i=l;i<=r;i++)pre[i]=b[i];31 sort(pre+l,pre+r+1);32 }33 int find(int x,int y)34 {35 int l=(x-1)*block+1,r=x*block,mid;36 while(l<=r)37 {38 mid=(l+r)>>1;39 if(pre[mid]>=y)r=mid-1;else l=mid+1;40 }41 return l-(x-1)*block-1;42 }43 int query(int x,int y)44 {45 int sum=0,bx=pos[x],by=pos[y];46 if(by-bx<=1)47 {48 for(int i=x;i<=y;i++)if(b[i]<x)sum++;49 }50 else51 {52 for(int i=x;i<=bx*block;i++)if(b[i]<x)sum++;53 for(int i=(by-1)*block+1;i<=y;i++)if(b[i]<x)sum++;54 }55 for(int i=bx+1;i<by;i++)sum+=find(i,x);56 return sum;57 }58 void change(int x,int y)59 {60 for(int i=1;i<=n;i++)last[c[i]]=0;61 c[x]=y;62 for(int i=1;i<=n;i++)63 {64 int t=b[i];65 b[i]=last[c[i]];66 last[c[i]]=i;67 if(t!=b[i])reset(pos[i]);68 }69 }70 int main()71 {72 freopen("input.txt","r",stdin);73 freopen("output.txt","w",stdout);74 n=read();m=read();75 block=floor(sqrt(n));76 for(int i=1;i<=n;i++)77 {78 c[i]=read();79 pos[i]=(i-1)/block+1;80 b[i]=last[c[i]];81 last[c[i]]=i;82 }83 for(int i=1;i<=pos[n];i++)reset(i);84 char ch;int x,y;85 while(m--)86 {87 ch=‘ ‘;88 while(ch!=‘Q‘&&ch!=‘R‘)ch=getchar();x=read();y=read();89 if(ch==‘R‘)change(x,y);else printf("%d\n",query(x,y));90 } 91 return 0;92 }
View code
Bzoj2120: Number color