2724: [Violet 6] Dandelion time limit: Sec Memory Limit: MB
Submit: 970 Solved: 319
[Submit] [Status] [Discuss] Description
Input
Fix it.
L = (l_0 + x-1) mod n + 1, r = (r_0 + x-1) mod n + 1
Output
Sample Input6 3
1 2 3 2 1 2
1 5
3 6
1 5Sample Output1
2
1HINT
Fixed under:
n <= 40000, M <= 50000
Source
Vani Original
Chunking.
First, the n number is divided into sqrt (n) blocks, preprocessing the answer of each block at the beginning to all positions behind him, O (N*SQRT (n)).
For each inquiry (L,R), we divide it into (L,L-1), (L,r), where L is the beginning of a block.
The latter has been preprocessed, the former violent enumeration, using the dichotomy method to calculate the number of occurrences of his (l,r), update the answer.
(In order to find the number of occurrences, we need to a[i] discretization, for each a[i] according to the position from left to right in order to record down, and then can be two points)
Time Complexity O (m*sqrt (n) *logn+n*sqrt (n))
#include <iostream> #include <cstring> #include <cstdio> #include <cstdlib> #include <cmath > #include <algorithm> #include <vector> #define PB push_back#define M 40005using namespace Std;int belong[ m],cnt[m],n,m,tot,b,num;vector<int> v[m];struct data{int Hash,id;} A[m];struct ans{int Ans,id;} F[205][m];struct help{int pos,v;} B[m];void read (int &tmp) {Tmp=0;char ch=getchar (); int fu=1;for (;ch< ' 0 ' | | Ch> ' 9 '; Ch=getchar ()) if (ch== '-') fu=-1;for (; ch>= ' 0 ' &&ch<= ' 9 '; Ch=getchar ()) tmp=tmp*10+ch-' 0 '; tmp *=fu;} void Prepare () {for (int. i=1;i<=num;i++) {for (int j= (i-1) *b+1;j<=i*b;j++) belong[j]=i;for (int j=1;j<=tot;j++) cnt[j]=0; Ans x;x.ans=0,x.id=0;for (int j= (i-1) *b+1;j<=n;j++) {int h=a[j].hash;cnt[h]++;if (cnt[h]>x.ans| | (cnt[h]==x.ans&&a[j].id<x.id)) x.ans=cnt[h],x.id=a[j].id;f[i][j]=x;}}} BOOL CMP (help A,help B) {if (B.V==A.V) return A.pos<b.pos;return A.V<B.V;} int findd (int k,int l,int R,int w) {if (l==r) returN l;int m= (l+r) >>1;if (v[k][m]>w&&m-1>=0&&v[k][m-1]>=w) return Findd (K,L,M-1,W); if (v K [M]<w] return findd (K,M+1,R,W); return m;} int findx (int k,int l,int R,int w) {if (l==r) return l;int m= (l+r) >>1;if (V[k][m]<w&&m+1<v[k].size () & AMP;&V[K][M+1]<=W) return Findx (K,M+1,R,W), if (v[k][m]>w) return findx (K,L,M-1,W); return m;} int Get (int k,int l,int r) {int s=v[k].size (); return Findx (K,0,s-1,r)-findd (k,0,s-1,l) +1;} int Solve (int l,int r) {int k=belong[l];if ((k-1) *b+1==l) return f[k][r].id; Ans x=f[k+1][r];for (int i=l;i<=min (K*B,R); i++) {int now=get (A[I].HASH,L,R); if (now>x.ans| | (now==x.ans&&a[i].id<x.id)) X.ans=now,x.id=a[i].id;} return x.id;} int main () {scanf ("%d%d", &n,&m); tot=0; for (int i=1;i<=n;i++) {read (a[i].id); b[i].v=a[i].id,b[i].pos=i;} Sort (b+1,b+1+n,cmp); b[0].v=-10;for (int i=1;i<=n;i++) if (B[I].V==B[I-1].V) A[b[i].pos].hash=tot; else a[b[i].pos].hash=++tot;for (int i=1;i<=n;i++) v[a[i].hASH].PB (i); B=SQRT (n); num= (n+b-1)/b; Prepare (); int Ans=0;while (m--) {int l,r;read (l), read (R); l= (l+ans-1)%n+1,r= (r+ans-1)%n+1;if (l>r) swap (l,r); ans= Solve (l,r);p rintf ("%d\n", ans); return 0;}
Sentiment:
1. Two points written incorrectly;
Solve note (l,r) may be less than one piece, so loop to min (k*b,r)
"Bzoj 2724" [Violet 6] Dandelion