Multipart, pre-processing:
① The number of types of even values between block I and block J.
② The number of times each value appears in the first block. (Prefix and) (difference)
During each query, the elements that are not in the whole block are transferred to another group by force.
Note: to reduce the use of memset, do not write 100000 memset; otherwise, it will be TLE, rather than O (SQRT (N) after each query )) to subtract the temporary array of the number of occurrences of each value of the record.
Code:
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 #include<cmath> 5 using namespace std; 6 int n,kind,m,a[100001],sz,sum,num[100001],l[320],r[320],w[320][320]; 7 int Time[100001],ans,x,y; 8 int ev[100001][320]; 9 void makeev() 10 { 11 for(int i=1;i<=n;i++) 12 for(int j=num[i];j<=sum;j++) 13 ev[a[i]][j]++; 14 } 15 inline int getsum(const int &v,const int &L,const int &R){return ev[v][R]-ev[v][L-1];} 16 void makeblock() 17 { 18 for(sum=1;sum*sz<n;sum++) 19 { 20 l[sum]=(sum-1)*sz+1; 21 r[sum]=sum*sz; 22 for(int i=l[sum];i<=r[sum];i++) 23 num[i]=sum; 24 } 25 l[sum]=sz*(sum-1)+1; 26 r[sum]=n; 27 for(int i=l[sum];i<=r[sum];i++) 28 num[i]=sum; 29 } 30 void init() 31 { 32 for(int i=1;i<=sum;i++) 33 { 34 memset(Time,0,sizeof(Time)); 35 for(int j=i;j<=sum;j++) 36 { 37 w[i][j]=w[i][j-1]; 38 for(int k=l[j];k<=r[j];k++) 39 { 40 Time[a[k]]++; 41 if(!(Time[a[k]]&1))w[i][j]++; 42 else if(Time[a[k]]!=1)w[i][j]--; 43 } 44 } 45 } 46 } 47 int res,CH[20],Num;char c; 48 inline int G() 49 { 50 res=0;c=‘*‘; 51 while(c<‘0‘||c>‘9‘)c=getchar(); 52 while(c>=‘0‘&&c<=‘9‘){res=res*10+(c-‘0‘);c=getchar();} 53 return res; 54 } 55 inline void P(int x) 56 { 57 if(!x){putchar(‘0‘);putchar(‘\n‘);return;}Num=0; 58 while(x>0){CH[++Num]=x%10;x/=10;} 59 while(Num)putchar(CH[Num--]+48); 60 putchar(‘\n‘); 61 } 62 int main() 63 { 64 n=G();kind=G();m=G(); 65 sz=sqrt(n); 66 for(int i=1;i<=n;i++)a[i]=G(); 67 makeblock();init();makeev(); 68 memset(Time,0,sizeof(Time)); 69 for(int i=1;i<=m;i++) 70 { 71 x=G();y=G();x=(x+ans)%n+1;y=(y+ans)%n+1;if(x>y)swap(x,y); 72 ans=w[num[x]+1][num[y]-1]; 73 if(num[x]+1>=num[y]) 74 { 75 for(int j=x;j<=y;j++) 76 { 77 Time[a[j]]++; 78 if(!(Time[a[j]]&1))ans++; 79 else if(Time[a[j]]!=1)ans--; 80 } 81 for(int j=x;j<=y;j++)Time[a[j]]--; 82 } 83 else 84 { 85 for(int j=x;j<=r[num[x]];j++) 86 { 87 Time[a[j]]++; 88 if(!((Time[a[j]]+getsum(a[j],num[x]+1,num[y]-1))&1))ans++; 89 else if(Time[a[j]]+getsum(a[j],num[x]+1,num[y]-1)!=1)ans--; 90 } 91 for(int j=l[num[y]];j<=y;j++) 92 { 93 Time[a[j]]++; 94 if(!((Time[a[j]]+getsum(a[j],num[x]+1,num[y]-1))&1))ans++; 95 else if(Time[a[j]]+getsum(a[j],num[x]+1,num[y]-1)!=1)ans--; 96 } 97 for(int j=x;j<=r[num[x]];j++)Time[a[j]]--; 98 for(int j=l[num[y]];j<=y;j++)Time[a[j]]--; 99 }100 P(ans);101 }102 return 0;103 }
[Part] bzoj2821 poetry (poetize)