「luogu3567」[POI2014]KUR-Couriers

主席树

空间要开够!!!

空间要开够!!!

空间要开够!!!

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N=500010;
 4 int n,m,root[N];
 5 int tot,lc[N<<5],rc[N<<5],cnt[N<<5];
 6 void build(int& k,int l,int r,int v){
 7     tot++;
 8     cnt[tot]=cnt[k]+1,lc[tot]=lc[k],rc[tot]=rc[k];
 9     k=tot;
10     if(l==r) return;
11     int mid=(l+r)>>1;
12     if(v<=mid) build(lc[k],l,mid,v);
13     else build(rc[k],mid+1,r,v);
14     return;
15 }
16 int que(int now,int pre,int l,int r,int len){
17     if(l==r) return l;
18     int mid=(l+r)>>1;
19     if(2*(cnt[lc[now]]-cnt[lc[pre]])>len) return que(lc[now],lc[pre],l,mid,len);
20     else if(2*(cnt[rc[now]]-cnt[rc[pre]])>len) return que(rc[now],rc[pre],mid+1,r,len);
21     else return 0;
22 }
23 int main(){
24     int t1,t2;
25     scanf("%d%d",&n,&m);
26     for(int i=1;i<=n;i++){
27         scanf("%d",&t1);
28         root[i]=root[i-1];
29         build(root[i],1,n,t1);
30     }
31     for(int i=1;i<=m;i++){
32         scanf("%d%d",&t1,&t2);
33         printf("%d\n",que(root[t2],root[t1-1],1,n,t2-t1+1));
34     }
35     return 0;
36 }

 

posted @ 2018-03-12 22:44  Cupcake  阅读(116)  评论(0编辑  收藏  举报