洛谷P3369 【模板】普通平衡树 01trie/骚操作

本文深入解析洛谷P3369题目中使用的普通平衡树01Trie实现技巧,包括代码实现、节点插入、排名查询及kth元素查找等核心操作,为读者提供了一套完整的平衡树01Trie应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

洛谷P3369 【模板】普通平衡树 01trie/骚操作

Code:

#include <cstdio>
#include <algorithm>
#include <cstring>
#define setIO(s) freopen(s".in","r",stdin)
#define maxn 100010 * 33 
using namespace std;
int root=1,tot=1,sumv[maxn],n,opt,x,ch[maxn][2]; 
void ins(int val,int c){
    val += (int)1e7;
    for(int i=31,p=root,t;i>=0;--i){
        t=(val>>i)&1;
        if(!ch[p][t]) ch[p][t]=++tot;
        p=ch[p][t]; sumv[p]+=c; 
    }
}
int rank(int val){
    val += (int)1e7; 
    int res=0,p=root; 
    for(int i=31;i>=0;--i){
        int t=(val>>i)&1;
        if(t) res += sumv[ch[p][0]];
        p=ch[p][t]; 
    }
    return res; 
}
int kth(int val){
    int k=root,res=0;
    for(int i=31;i>=0;--i){
        if(val>sumv[ch[k][0]]) res|=(1<<i),val-=sumv[ch[k][0]],k=ch[k][1];
        else k=ch[k][0]; 
    } 
    res-=(1e7); return res; 
}
int main(){
    //setIO("input");
    scanf("%d",&n);
    while(n--){
        scanf("%d%d",&opt,&x);
        if(opt==1) ins(x,1);
        else if(opt==2) ins(x,-1);
        else if(opt==3) printf("%d\n",rank(x)+1);
        else if(opt==4) printf("%d\n",kth(x));
        else if(opt==5) printf("%d\n",kth(rank(x)));
        else if(opt==6) printf("%d\n",kth(rank(x+1)+1)); 
    }
    return 0; 
}

  

posted @ 2019-02-13 15:23 EM-LGH 阅读( ...) 评论( ...) 编辑 收藏
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值