Title: http://www.lydsy.com/JudgeOnline/problem.php?id=2724
Block + two points. First discretization, maintenance ans[i][j] represents the number of blocks of block I to block J, and then for the inquiry L,r, in the complete block we take out and then brute sweep the non-complete block.
As long as we put the same number together, for A[i], we can find the first position of the number before r by two points P and the position of the number after the first in L q,p-q+1 that is a[i] in question the number of occurrences of l,r. And then update the answer just fine.
(Because the function of the name of the too ugly + hand is a long time tat ...
#include <cstring>#include<cctype>#include<cstdio>#include<algorithm>#include<cmath>#defineRep (i,l,r) for (int i=l;i<=r;i++)#defineDown (i,l,r) for (int i=l;i>=r;i--)#defineCLR (x, y) memset (x,y,sizeof (×))#definell Long Long#defineMAXN 50005#defineMM Int (1e9+7)using namespacestd;structdata{intV,p;} D[MAXN];inth[maxn],a[maxn],l[maxn],r[maxn],l[maxn],r[maxn],c[maxn],belong[maxn],ans[205][205];intn,m,cnt,block,lastans,x,y;intRead () {intx=0, f=1;CharCh=GetChar (); while(!isdigit (CH)) {if(ch=='-') f=-1; Ch=GetChar ();} while(IsDigit (CH)) {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}intFind (intx) { intL=1, r=N; while(l<=S) { intMid= (L+R)/2; if(X==h[mid])returnmid; if(X1; ElseL=mid+1; } returnl;}BOOLCMP (data a,data b) {returna.v==b.v?a.p<b.p:a.v<B.V;}voidPre () {Rep (I,1, N) {D[I].V=a[i]; d[i].p=i; } sort (d+1, d+1+n,cmp); Rep (I,1, N) { if(! L[D[I].V]) l[d[i].v]=i; R[D[I].V]=i; } Rep (I,1, CNT) { inttmp,mx=0; CLR (c,0); Rep (j,l[i],n) {C[a[j]]++; if(c[a[j]]>mx| | (c[a[j]]==mx&&a[j]<tmp)) mx=c[a[j]],tmp=A[j]; ANS[I][BELONG[J]]=tmp; } } }intFindup (intLintRintx) { intmid,tmp; while(l<=R) {Mid= (l+r)/2; if(d[mid].p>x) r=mid-1; Else{tmp=mid; l=mid+1;} } returntmp;}intFinddown (intLintRintx) { intmid,tmp; while(l<=R) {Mid= (l+r)/2; if(d[mid].p<x) l=mid+1; Else{tmp=mid; r=mid-1;} } returntmp;}intQueintXinty) { intmx=0, TMP; if(belong[x]==Belong[y]) {Rep (i,x,y) {intNum=findup (l[a[i]],r[a[i]],y)-finddown (l[a[i]],r[a[i]],x) +1; if(num>mx| | (num==mx&&a[i]<tmp)) mx=num,tmp=A[i]; } } Else { if(belong[x]+1<Belong[y]) {tmp=ans[belong[x]+1][belong[y]-1]; MX=findup (l[tmp],r[tmp],y)-finddown (l[tmp],r[tmp],x) +1; } Rep (I,x,r[belong[x]]) {intNum=findup (l[a[i]],r[a[i]],y)-finddown (l[a[i]],r[a[i]],x) +1; if(num>mx| | (num==mx&&a[i]<tmp)) mx=num,tmp=A[i]; } rep (i,l[belong[y]],y) {intNum=findup (l[a[i]],r[a[i]],y)-finddown (l[a[i]],r[a[i]],x) +1; if(num>mx| | (num==mx&&a[i]<tmp)) mx=num,tmp=A[i]; } } returntmp;}intMain () {n=read (); m=read (); Rep (I,1, n) a[i]=read (), h[i]=A[i]; Sort (H+1, H +1+N); Rep (I,1, N) a[i]=Find (A[i]); Block=int(sqrt (n)); CNT=n/block;if(N%block) cnt++; Rep (I,1, CNT) l[i]=block* (i-1)+1, r[i]=block*i; intnow=1; Rep (I,1, n) {belong[i]=now;if(i%block==0) now++;} Pre (); Lastans=0; Rep (I,1, M) {x=read (); y=read (); X= (x+lastans-1)%n+1; Y= (y+lastans-1)%n+1; if(x>y) Swap (x, y); printf ("%d\n", lastans=h[que (x, y)]); } return 0; }
Bzoj 2724: [Violet 6] Dandelion