1 #include <cstdio> 2 const int Maxn=100010; 3 inline void Get_Int(int &x) 4 { 5 char ch=getchar(); x=0; 6 while (ch<'0' || ch>'9') ch=getchar(); 7 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 8 } 9 struct Node 10 { 11 Node * l,* r; int Sum; 12 inline void Push_Up() {Sum=l->Sum+r->Sum;} 13 }; 14 Node Memory[Maxn*100],* port=Memory,* null,* Root[Maxn]; 15 int n,m,Mx,a[Maxn],l,r; 16 inline int Max(int x,int y) {return x>y?x:y;} 17 inline void Init() {null=port++; null->Sum=0; null->l=null->r=null;} 18 inline Node * NewNode(Node * x) 19 { 20 Node * Ret=port++; 21 if (x==null) {Ret->l=Ret->r=null; Ret->Sum=0;} else 22 {Ret->l=x->l,Ret->r=x->r,Ret->Sum=x->Sum;} 23 return Ret; 24 } 25 26 Node * Insert(Node * Pre,int o,int l,int r,int v) 27 { 28 Node * Ret=NewNode(Pre); 29 if (l==r) 30 { 31 Ret->Sum+=v; 32 return Ret; 33 } 34 int mid=(l+r)>>1; 35 if (v<=mid) Ret->l=Insert(Pre->l,o<<1,l,mid,v); else Ret->r=Insert(Pre->r,o<<1|1,mid+1,r,v); 36 Ret->Push_Up(); 37 } 38 int Sum(Node * Now,int o,int l,int r,int p,int q) 39 { 40 if (Now==null) return 0; 41 if (l==p && r<=q) return Now->Sum; 42 int mid=(l+r)>>1; 43 if (q<=mid) return Sum(Now->l,o<<1,l,mid,p,q); 44 if (p>=mid+1) return Sum(Now->r,o<<1|1,mid+1,r,p,q); 45 if (p<=mid && q>=mid+1) return Sum(Now->l,o<<1,l,mid,p,mid)+Sum(Now->r,o<<1|1,mid+1,r,mid+1,q); 46 } 47 inline int Query(int L,int R) 48 { 49 int Ret=1; 50 while (true) 51 { 52 int t=Sum(Root[R],1,1,Mx,1,Ret)-Sum(Root[L],1,1,Mx,1,Ret); 53 if (t<Ret) return Ret; else Ret=t+1; 54 } 55 return Ret; 56 } 57 int main() 58 { 59 // freopen("data.in","r",stdin); 60 // freopen("data.out","w",stdout); 61 Init(); 62 Get_Int(n); 63 for (int i=1;i<=n;i++) Get_Int(a[i]),Mx=Max(Mx,a[i]); 64 Root[0]=NewNode(null); 65 for (int i=1;i<=n;i++) Root[i]=Insert(Root[i-1],1,1,Mx,a[i]); 66 Get_Int(m); 67 for (int i=1;i<=m;i++) 68 { 69 Get_Int(l),Get_Int(r); 70 printf("%d ",Query(l-1,r)); 71 } 72 return 0; 73 }
网上题解一堆..即找到1~Mx.那么1~Mx的Sum就都可以取到.
BZOJ上问Root讨来数据自测全A,交上去WA.