标签:cpp str ++ cstring pos 分块 tor -- class
Code:
#include<cstdio> #include<string> #include<iostream> #include<algorithm> #include<vector> #include<cstring> #include<cmath> using namespace std; void setIO(string a){ freopen((a+".in").c_str(),"r",stdin); } const int maxn=40000+5; int n,m,block; int col[maxn],A[maxn],belong[maxn],bucket[maxn]; int prefix[300][maxn]; int best[300][300]; vector<int>position[maxn],solve; int st(int i){ return (i-1)*block+1; } int ed(int i){ return min(i*block,n);} int main(){ //setIO("input"); scanf("%d%d",&n,&m); block=sqrt(n); for(int i=1;i<=n;++i) { scanf("%d",&col[i]); A[i]=col[i]; belong[i]=(i-1)/block+1; } sort(A+1,A+1+n); for(int i=1;i<=n;++i) { col[i]=lower_bound(A+1,A+1+n,col[i])-A; prefix[belong[i]][col[i]]+=1; } for(int i=1;i<=belong[n];++i) for(int j=1;j<=n;++j) prefix[i][j]+=prefix[i-1][j]; for(int length=1;length<=belong[n];++length) for(int i=1;i+length-1<=belong[n];++i) { int j=i+length-1; int tmp=0,cur=0; for(int k=st(i);k<=ed(i);++k) //枚举 i 中颜色 { int delta=prefix[j][col[k]]-prefix[i-1][col[k]]; if(delta >= tmp){ if(delta>tmp) tmp=delta,cur=col[k]; else if(col[k]<cur) cur=col[k]; } } for(int k=st(j);k<=ed(j);++k){ //枚举 j 中颜色 int delta=prefix[j][col[k]]-prefix[i-1][col[k]]; if(delta >= tmp){ if(delta>tmp) tmp=delta,cur=col[k]; else if(col[k]<cur) cur=col[k]; } } if(j>i){ int delta; delta=prefix[j][best[i+1][j]]-prefix[i-1][best[i+1][j]]; if(delta>=tmp){ if(delta>tmp) tmp=delta, cur=best[i+1][j]; else if(best[i+1][j]<cur) cur=best[i+1][j]; } delta=prefix[j][best[i][j-1]]-prefix[i-1][best[i][j-1]]; if(delta>=tmp){ if(delta>tmp) tmp=delta, cur=best[i][j-1]; else if(best[i][j-1]<cur) cur=best[i][j-1]; } } best[i][j]=cur; } int lastans=0,l,r; while(m--){ scanf("%d%d",&l,&r); l=(l+lastans-1)%n+1; r=(r+lastans-1)%n+1; if(l>r) swap(l,r); for(int i=l;i<=min(ed(belong[l]),r);++i) { bucket[col[i]]+=1; solve.push_back(col[i]); } if(belong[l] != belong[r]){ for(int i=st(belong[r]);i<=r;++i) { bucket[col[i]]+=1; solve.push_back(col[i]); } } int tmp=0,cur=0; int blockl=belong[l]+1, blockr=belong[r]-1; if(blockl<=blockr){ cur=best[blockl][blockr], tmp=prefix[blockr][cur]-prefix[blockl-1][cur]; for(int i=0;i<solve.size();++i){ int delta=prefix[blockr][solve[i]]-prefix[blockl-1][solve[i]]+bucket[solve[i]]; if(delta>=tmp){ if(delta>tmp) tmp=delta,cur=solve[i]; else if(solve[i]<cur) cur=solve[i]; } } } else{ for(int i=0;i<solve.size();++i){ int delta=bucket[solve[i]]; if(delta>=tmp){ if(delta>tmp) tmp=delta, cur=solve[i]; else if(solve[i]<cur) cur=solve[i]; } } } for(int i=0;i<solve.size();++i) bucket[solve[i]]=0; solve.clear(); lastans=A[cur]; printf("%d\n",lastans); } return 0; }
标签:cpp str ++ cstring pos 分块 tor -- class
原文地址:https://www.cnblogs.com/guangheli/p/9887060.html