「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 }

 

转载于:https://www.cnblogs.com/mycups/p/8552541.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值