Without modifying the chairman Tree naked <=> Mo team + weight block naked question.
Complexity O (m*sqrt (n)).
P.S. Topic Description Pit Daddy, the second number is the range of weights.
#include <cstdio> #include <algorithm> #include <cmath>using namespace std; #define N 300001#define M 10001int f,c;inline void R (int &x) {c=0;f=1; for (;c< ' 0 ' | | C> ' 9 '; C=getchar ()) if (c== '-') f=-1; for (x=0;c>= ' 0 ' &&c<= ' 9 '; C=getchar ()) (x*=10) + = (c ' 0 '); X*=f;} void P (int x) {if (x<10) putchar (x+ ' 0 '); Else{p (X/10);p Utchar (x%10+ ' 0 ');}} int sum=1,lim,l[105],n,m,num[10001],r[105],cnts[105],a[n],b[10001],anss[m],num2[n];struct Ask{int l,r,p;} Q[m];bool operator < (const ask &a,const ask &b) {return NUM2[A.L]!=NUM2[B.L]? NUM2[A.L]<NUM2[B.L]: a.r< B.R;} void Val_mb () {int sz=sqrt (LIM); if (!sz) sz=1; for (; sum*sz<lim;++sum) {l[sum]=r[sum-1]+1; r[sum]=sum*sz; for (int i=l[sum];i<=r[sum];++i) num[i]=sum; } l[sum]=r[sum-1]+1; R[sum]=lim; for (int i=l[sum];i<=lim;++i) num[i]=sum;} void Mo_mb () {int tot=1,sz=sqrt (n); if (!sz) sz=1;for (; tot*sz<n;++tot) {int r=tot*sz; for (int i= (tot-1) *sz+1;i< =r;++i) Num2[i]=tot; }for (int i= (tot-1) *sz+1;i<=n;++i) Num2[i]=tot;} void Insert (const int &x) {++b[x]; ++cnts[num[x]];} void Delete (const int &x) {--b[x];--cnts[num[x]];} int Query (const int &l,const int &r) {int goal= (r-l+1>>1); for (int j=1;j<=sum;++j) if (cnts[j]>goal) for (int i=l[j];i<=r[j];++i) if (b[i]>goal) return i;return-1;} int main () {R (n); R (Lim); for (int i=1;i<=n;++i) R (A[i]); VAL_MB (); R (M); for (int i=1;i<=m;++i) R (Q[I].L), R (Q[I].R), q[i].p=i; MO_MB (); Sort (q+1,q+m+1); for (int i=q[1].l;i<=q[1].r;++i) Insert (A[i]); Anss[q[1].p]=query (Q[1].L,Q[1].R); for (int i=2;i<=m;++i) {if (Q[I].L<Q[I-1].L) for (int j=q[i-1].l-1;j>=q[i].l;--j) Insert (A[j]); else for (int j=q[i-1].l;j<q[i].l;++j) Delete (A[j]); if (Q[I].R<Q[I-1].R) for (int j=q[i-1].r;j>q[i].r;--j) Delete (A[j]); else for (int j=q[i-1].r+1;j<=q[i].r;++j) Insert (A[j]); Anss[q[i].p]=query (Q[I].L,Q[I].R); } for (inT i=1;i<=m;++i) if (anss[i]==-1) puts ("no"); else {printf ("yes"); P (Anss[i]); Puts ("");} return 0;}
Mo team algorithm, weight bzoj2223 [coci 2009]patuljci