bzoj 2727
修正一下
l = (l_0 + x – 1) mod n + 1, r = (r_0 + x – 1) mod n + 1
Output
HINT
修正下:
n <= 40000, m <= 50000
分析:
比较经典的区间众数问题,利用分块的思路解决:
1.cnt[i][j]表示前j块里面i出现的次数
2.v[i][j]表示第i块到第j块里面的众数
3.num[i][j]表示第i块到第j块里面的众数出现的次数
时间复杂度O(n*sqrt(n))
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define F(i,a,b) for(int i=a;i<=b;i++) 4 #define D(i,a,b) for(int i=a;i>=b;i--) 5 #define ms(i,a) memset(a,i,sizeof(a)) 6 #define st(x) ( (x-1)*B+1) 7 #define ed(x) ( min(n, x*B)) 8 #define bl(x) ( (x-1)/B+1) 9 #define sum(x) ( (n-1)/B+1) 10 11 int inline read(){ 12 int x=0 ;char c=getchar(); 13 while (c<'0' || c>'9') c=getchar(); 14 while (c>='0' && c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar(); 15 return x; 16 } 17 18 int const maxn=50003; 19 20 int n,m,B,cnt[maxn][301],v[301][301],num[301][301]; 21 int a[maxn],b[maxn],c[maxn],tmp[maxn]; 22 23 int query(int l,int r){ 24 int x=bl(l); 25 int y=bl(r); 26 int V,NUM=0; 27 if(x+1<=y-1){ 28 V=v[x+1][y-1]; 29 NUM=num[x+1][y-1]; 30 } 31 int s,e; 32 tmp[0]=0; 33 if(x==y){ 34 F(i,l,r) { 35 tmp[++tmp[0]]=a[i]; 36 c[a[i]]++; 37 } 38 }else { 39 s=l;e=ed(x); 40 F(i,s,e) { 41 tmp[++tmp[0]]=a[i]; 42 c[a[i]]++; 43 } 44 s=st(y);e=r; 45 F(i,s,e){ 46 tmp[++tmp[0]]=a[i]; 47 c[a[i]]++; 48 } 49 } 50 F(i,1,tmp[0]) { 51 int t=x+1<=y-1? cnt[tmp[i]][y-1]-cnt[tmp[i]][x]: 0; 52 if(c[tmp[i]]+t>NUM || ( c[tmp[i]]+t ==NUM && V> tmp[i])) { 53 V=tmp[i]; NUM=c[tmp[i]]+t; 54 } 55 } 56 F(i,1,tmp[0]) c[tmp[i]]=0; 57 return V; 58 } 59 60 int main(){ 61 n=read(); 62 m=read(); 63 F(i,1,n) a[i]=b[i]=read(); 64 sort(b+1,b+n+1); 65 int k=unique(b+1,b+n+1)-b-1; 66 F(i,1,n) a[i]=lower_bound(b+1,b+k+1,a[i])-b; 67 B=(int)sqrt(n); 68 int t=sum(n); 69 F(i,1,t) { 70 ms(0,c); 71 int s=st(i); 72 int e=ed(i); 73 F(j,s,e) c[a[j]]++; 74 F(j,1,k) cnt[j][i]=cnt[j][i-1]+c[j]; 75 } 76 F(i,1,t){ 77 ms(0,c); 78 F(j,i,t){ 79 int V=v[i][j-1]; 80 int NUM=num[i][j-1]; 81 int s=st(j); 82 int e=ed(j); 83 F(p,s,e){ 84 c[a[p]]++ ; 85 if (c[a[p]]> NUM || (NUM==c[a[p]] && a[p] < V)) { 86 V=a[p]; NUM=c[a[p]]; 87 } 88 } 89 v[i][j]=V; 90 num[i][j]=NUM; 91 } 92 } 93 int x=0; 94 ms(0,c); 95 while (m--){ 96 int l=read(); 97 int r=read(); 98 l=(l+x-1+n) % n+1; 99 r=(r+x-1+n) % n+1; 100 if(l>r) swap(l,r); 101 printf("%d\n",x=b[query(l,r)]); 102 } 103 return 0; 104 }