社区讨论
01trie不过
P3369【模板】普通平衡树参与者 1已保存回复 0
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 0 条
- 当前快照
- 1 份
- 快照标识符
- @lo966lly
- 此快照首次捕获于
- 2023/10/28 06:13 2 年前
- 此快照最后确认于
- 2023/10/28 06:13 2 年前
CPP
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Node{
ll size;
Node *lson, *rson;
};
const ll N = 4000005;
ll n;
Node *root;
Node *x, *y;
void New(Node *p) {
x = new Node(), y = new Node();
p->lson = x; p->rson = y;
x->lson = NULL; x->rson = NULL;
y->lson = NULL; y->rson = NULL;
x->size = 0; y->size = 0;
}
/*
void build(int k, Node *fa) {
if(k > 20) return;
New(fa);
build(k+1, fa->lson);
build(k+1, fa->rson);
return;
}
*/
void ist(ll m, ll delta) {
Node *u; u = root;
for(ll i = 31; i >= 0; i --) {
ll k = (m>>i) & 1;
if(u->lson == NULL) New(u);
if(k == 0) u = u->lson, u->size += delta;
else u = u->rson, u->size += delta;
}
}
ll rnk(ll m) {
Node *u; u = root; ll x = 0;
for(ll i = 31; i >= 0; i --) {
ll k = (m>>i) & 1;
if(u->lson == NULL) New(u);
if(k == 1) {
x += u->lson->size, u = u->rson;
}
else u = u->lson;
}
return x+1;
}
ll val(int m) {
Node *u; u = root; ll x = 0;
for(int i = 31; i >= 0; i --) {
if(u->lson->size >= m) u = u->lson;
else m -= u->lson->size, u = u->rson, x |= (1 << i);
}
return x;
}
ll pre(int m) {
return val(rnk(m)-1);
}
ll nxt(int m) {
return val(rnk(m+1));
}
signed main() {
scanf("%lld", &n);
root = new Node(); root->lson = NULL; root->rson = NULL;
for(int i = 1; i <= n; i ++) {
ll opt, m;
scanf("%lld%lld", &opt, &m);
if(opt == 1) ist(m, 1);
else if(opt == 2) ist(m,-1);
else if(opt == 3) printf("%lld\n", rnk(m));
else if(opt == 4) printf("%lld\n", val(m));
else if(opt == 5) printf("%lld\n", pre(m));
else if(opt == 6) printf("%lld\n", nxt(m));
}
}
按照题解思路打了一个01trie,但是是指针版的,只有50pts...求助dalao
回复
共 0 条回复,欢迎继续交流。
正在加载回复...