码迷,mamicode.com
首页 > 其他好文 > 详细

「luogu3567」[POI2014]KUR-Couriers

时间:2018-03-13 00:52:35      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:AC   uri   roo   bsp   names   ++   pos   main   void   

主席树

空间要开够!!!

空间要开够!!!

空间要开够!!!

 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 }

 

「luogu3567」[POI2014]KUR-Couriers

标签:AC   uri   roo   bsp   names   ++   pos   main   void   

原文地址:https://www.cnblogs.com/mycups/p/8552541.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!