This question is considered well written. He wrote three methods.
There is a like qwq $ $ $ N \ sqrt (N) $ approach ,, but kindly big brother to help me see why so slow $ qwq $ (behind third)
NOTE: $ pos [i] $ $ I $ denotes belonging $ pos [i] $ block.
The first section is possible to count all of the blocks (block i to j-th block), the number of times each appears, denoted $ f [i] [j] [k] $, and all possible block the answer composition range, denoted $ h [i] [j] $.
Then put each answer block as an initial response and then for each value of the discrete pieces $ $ VL, violence modify the corresponding $ f [i] [j] [vl] $, update answers.
When the block length takes $ N ^ \ frac {2} {3} $, time complexity $ O (N ^ \ frac {5} {3}) $ level.
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cctype> #include<cstdlib> #include<vector> #include<queue> #include<map> #include<set> #define ull unsigned long long #define ll long long #define R register int #define pause (for(R i=1;i<=10000000000;++i)) #define OUT freopen("out.out","w",stdout); using namespace std; namespace Fread { static char B[1<<15],*S=B,*D=B; #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return ch<=36||ch>=127;} inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));} }using Fread::g; using Fread::gs; const int N=40010,M=37; int n,m,tot,T,lst; int f[M][M][N],h[M][M],vl[N],a[N],b[N],pos[N]; inline void PRE() { R mx=0,ans=0; for(R i=1;i<=n;++i) pos[i]=(i-1)/T+1; for(R j=1,L=pos[n];j<=L;++j,mx=0,ans=0) for(R t=j;t<=L;++t) { memcpy(f[j][t],f[j][t-1],sizeof(f[j][t-1])); for(R i=(t-1)*T+1,LL=min(t*T,n);i<=LL;++i) ++f[j][t][a[i]]; for(R i=tot;i;--i) if(f[j][t][i]>=mx) mx=f[j][t][i],ans=i; h[j][t]=ans; } } signed main() { #ifdef JACK freopen("NOIPAK++.in","r",stdin); OUT; #endif n=g(),m=g(),T=n/pow(n,1.0/3); for(R i=1;i<=n;++i) a[i]=g(); memcpy(b,a,sizeof(a)); sort(b+1,b+n+1); tot=unique(b+1,b+n+1)-b-1; for(R i=1;i<=n;++i) a[i]=lower_bound(b+1,b+tot+1,a[i])-b; memcpy(vl,b,sizeof(int)*(tot+1)); PRE(); for(R i=1,l,r;i<=m;++i) { R mx=0,ans=0; l=(g()+lst-1)%n+1,r=(g()+lst-1)%n+1; l>r?swap(l,r):(void)0; R p=pos[l]+1,q=pos[r]-1; ans=h[p][q],mx=f[p][q][ans]; if(pos[l]==pos[r]) { for(R i=l;i<=r;++i) { ++f[p][q][a[i]]; if(f[p][q][a[i]]>mx||(f[p][q][a[i]]==mx&&a[i]<ans)) mx=f[p][q][a[i]],ans=a[i]; } for(R i=l;i<=r;++i) --f[p][q][a[i]]; } else { ans=h[p][q],mx=f[p][q][ans]; for(R i=l,L=pos[l]*T;i<=L;++i) { ++f[p][q][a[i]]; if(f[p][q][a[i]]>mx||(f[p][q][a[i]]==mx&&a[i]<ans)) mx=f[p][q][a[i]],ans=a[i]; } for(R i=(pos[r]-1)*T+1;i<=r;++i) { ++f[p][q][a[i]]; if(f[p][q][a[i]]>mx||(f[p][q][a[i]]==mx&&a[i]<ans)) mx=f[p][q][a[i]],ans=a[i]; } for(R i=l,L=pos[l]*T;i<=L;++i) --f[p][q][a[i]]; for(R i=(pos[r]-1)*T+1;i<=r;++i) --f[p][q][a[i]]; } printf("%d\n",lst=vl[ans]); } }
The second pretreatment all possible blocks of the interval (the $ $ I $ J $ block to block) answers $ f [i] [j] $, and take a number of each deposit $ $ Vector $ $ VL to appear $ s [vl] [1 ... n] $.
The answer is initialized to answer block, and for each of the number of discrete pieces $ $ VL, in $ s [vl] $ separated in two $ [l, r] $ minimum and maximum position of the lower standard, subtraction is $ [l, r] $ how many $ vl $, and then update the answer.
When the block length takes $ \ sqrt {\ frac {N} {logN}} $, time complexity $ O (N \ sqrt {NlogN}) $.
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cctype> #include<cstdlib> #include<vector> #include<queue> #include<map> #include<set> #define ull unsigned long long #define ll long long #define R register int #define pause (for(R i=1;i<=10000000000;++i)) #define OUT freopen("out.out","w",stdout); using namespace std; namespace Fread { static char B[1<<15],*S=B,*D=B; #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return ch<=36||ch>=127;} inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));} }using Fread::g; using Fread::gs; map<int,int> mp; const int N=40010; int n,m,T,tot,lst; vector<int> s[N]; #define pb push_back int f[10010][10010],cnt[N],p[N],pos[N],a[N],b[N],vl[N]; inline void PRE(int p) { R ans=0,mx=0; memset(cnt,0,sizeof(cnt)); for(R t=p,lim=pos[n];t<=lim;++t) { for(R i=(t-1)*T+1,lim=min(n,t*T);i<=lim;++i) { if(++cnt[a[i]]>mx||(cnt[a[i]]==mx&&a[i]<ans)) mx=cnt[a[i]],ans=a[i]; } f[p][t]=ans; } } inline int calc(int l,int r,int x) {return upper_bound(s[x].begin(),s[x].end(),r)-lower_bound(s[x].begin(),s[x].end(),l);} inline int solve(int l,int r) { R mx=0,ret=0; if(pos[l]==pos[r]) { memset(cnt,0,sizeof(cnt)); for(R i=l;i<=r;++i) if(++cnt[a[i]]>mx||(cnt[a[i]]==mx&&a[i]<ret)) ret=a[i],mx=cnt[a[i]]; } else { ret=f[pos[l]+1][pos[r]-1],mx=calc(l,r,ret); for(R i=l,lim=pos[l]*T;i<=lim;++i) { R t=calc(l,r,a[i]); if(t>mx||(t==mx&&a[i]<ret)) ret=a[i],mx=t; } for(R i=(pos[r]-1)*T+1;i<=r;++i) { R t=calc(l,r,a[i]); if(t>mx||(t==mx&&a[i]<ret)) ret=a[i],mx=t; } } return ret; } signed main() { #ifdef JACK freopen("NOIPAK++.in","r",stdin); OUT; #endif n=g(),m=g();//,T=n/sqrt(n*log2(n)); T=qpow(n,1.0/4); for(R i=1;i<=n;++i) a[i]=g(); memcpy(b,a,sizeof(a)); sort(b+1,b+n+1); tot=unique(b+1,b+n+1)-b-1; memcpy(vl,b,sizeof(int)*(tot+1)); for(R i=1;i<=n;++i) a[i]=lower_bound(b+1,b+tot+1,a[i])-b,s[a[i]].pb(i); for(R i=1;i<=n;++i) pos[i]=(i-1)/T+1; for(R i=1;i<=pos[n];++i) PRE(i); for(R i=1,l,r;i<=m;++i) { l=(g()+lst-1)%n+1,r=(g()+lst-1)%n+1; l>r?swap(l,r):(void)0; printf("%d\n",lst=vl[solve(l,r)]); } }
The third is arguably the complexity of the best, but not very fast run $ qwq $.
With a second, pre-treatment of all possible blocks of interval (block i to the j-th block) answers $ F [i] [j] $, $ Vector and take a number of each storage $ $ $ VL to appear $ s [vl] [1 ... n] $.
Then pretreatment $ a [i] $ is a whole number of columns in the first few $ a [i] $, the first to the second $ 1 $ I $ $ block rearmost $ VL are several $ $ $ VL, referred to as $ d [i] [vl] $, pretreatment of the n-$ $ $ I $ block to block in the most forward $ $ VL are several $ vl $, denoted by $ h [i] [ vl] $.
For discrete pieces left in the $ vl $, check $ d [pos [r] -1] [vl] $, and if there is the closer to the right $ $ VL in discrete pieces (can save up beforehand with an array) then this range can be determined how many $ vl $ (each $ vl $ is the first of several $ vl $ already know), update the answer.
When the block length takes $ \ sqrt {n} $, the time complexity is O (N \ sqrt {N}) stage (not push the wrong push do not know).
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<cmath> #include<cctype> #include<cstdlib> #include<vector> #include<queue> #include<map> #include<set> #define ull unsigned long long #define ll long long #define R register int #define pause (for(R i=1;i<=10000000000;++i)) #define OUT freopen("out.out","w",stdout); using namespace std; namespace Fread { static char B[1<<15],*S=B,*D=B; #define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++) inline int g() { R ret=0,fix=1; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-1:fix; do ret=ret*10+(ch^48); while(isdigit(ch=getchar())); return ret*fix; } inline bool isempty(const char& ch) {return ch<=36||ch>=127;} inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));} }using Fread::g; using Fread::gs; const int N=40010,M=1000; int n,m,T,tot,lst; vector<int> s[N]; #define pb push_back int f[M][M],cnt[N],P[N],pos[N],a[N],b[N],vl[N],h[M][N],d[M][N],c[M][M]; inline void PRE(int p) { R ans=0,mx=0; memset(cnt,0,sizeof(cnt)); for(R t=p,lim=pos[n];t<=lim;++t) { for(R i=(t-1)*T+1,lim=min(n,t*T);i<=lim;++i) { if(++cnt[a[i]]>mx||(cnt[a[i]]==mx&&a[i]<ans)) mx=cnt[a[i]],ans=a[i]; } f[p][t]=ans,c[p][t]=mx; } } inline int solve(int l,int r) { R mx=0,ret=0,p=pos[l]+1,q=pos[r]-1; if(pos[l]==pos[r]) { memset(cnt,0,sizeof(cnt)); for(R i=l;i<=r;++i) if(++cnt[a[i]]>mx||(cnt[a[i]]==mx&&a[i]<ret)) ret=a[i],mx=cnt[a[i]]; } else { ret=f[p][q],mx=c[p][q]; memset(cnt,0x3f,sizeof(cnt)); for(R i=l,L=pos[l]*T;i<=L;++i) if(cnt[a[i]]==0x3f3f3f3f) cnt[a[i]]=P[i]; for(R i=q*T+1;i<=r;++i) { R tmp=P[i]+1-min(cnt[a[i]],h[p][a[i]]); if(tmp>mx||(tmp==mx&&a[i]<ret)) ret=a[i],mx=tmp; cnt[a[i]]=P[i]; } for(R i=l,L=pos[l]*T;i<=L;++i) { R tmp=max((cnt[a[i]]==0x3f3f3f3f?0:cnt[a[i]]),d[q][a[i]])-P[i]+1; if(tmp>mx||(tmp==mx&&a[i]<ret)) ret=a[i],mx=tmp; } } return ret; } signed main() { JACK #ifdef The freopen ( " NOIPAK ++ in. " , " R & lt " , stdin); OUT; #endif n- = G (), m = G (); T = POW (n-, . 1 / 2.3 ); // if less a little faster (sooner not smaller) for (I = R & lt . 1 ; I <= n-; ++ I) A [I] = G (); the memcpy (B, A, the sizeof (A)); Sort ( + B . 1 , B + n-+ . 1 ); TOT = UNIQUE (B + . 1 , B + n-+ . 1 )-b- . 1 ; the memcpy (VL, B, the sizeof ( int ) * (+ TOT . 1 )); for(R i=1;i<=n;++i) a[i]=lower_bound(b+1,b+tot+1,a[i])-b,s[a[i]].pb(i); for(R i=1;i<=n;++i) pos[i]=(i-1)/T+1; for(R i=1;i<=n;++i) P[i]=++cnt[a[i]]; memset(cnt,0,sizeof(cnt)); memset(h[pos[n]+1],0x3f,sizeof(h[pos[n]+1])); for(R t=pos[n];t;--t) { memcpy(h[t],h[t+1],sizeof(h[t+1])); for(R i=min(n,t*T);i>(t-1)*T;--i) h[t][a[i]]=P[i]; } for(R t=1;t<=pos[n];++t) { memcpy(d[t],d[t-1],sizeof(d[t-1])); for(R i=(t-1)*T+1,L=t*T;i<=L;++i) d[t][a[i]]=P[i]; } for(R i=1;i<=pos[n];++i) PRE(i); for(R i=1,l,r;i<=m;++i) { l=(g()+lst-1)%n+1,r=(g()+lst-1)%n+1; l>r?swap(l,r):(void)0; printf("%d\n",lst=vl[solve(l,r)]); } }
2019.06.28