czyzh @ 2023-01-30 09:42:32
样例过不了,3操作出问题了
#include <bits/stdc++.h>
using namespace std;
const int N = 2e7 + 10, INF = 1e9 + 7;
int n, m, ans = 0, last = 0;
struct Node
{
int l, r;
int key;
int val;
int cnt;
int size;
} tr[N];
int root = 0, idx;
void pushup(int p)
{
tr[p].size = tr[tr[p].l].size + tr[tr[p].r].size + tr[p].cnt;
}
int get_node(int key)
{
tr[++idx].key = key;
tr[idx].val = rand();
tr[idx].cnt = tr[idx].size = 1;
return idx;
}
void build()
{
get_node(-INF), get_node(INF);
root = 0, tr[1].r = 2;
pushup(root);
}
void zig(int &p)
{
int q = tr[p].l;
tr[p].l = tr[q].r;
tr[q].r = p;
p = q;
pushup(tr[p].r);
pushup(p);
}
void zag(int &p)
{
int q = tr[p].r;
tr[p].r = tr[q].l;
tr[q].l = p;
p = q;
pushup(tr[p].l);
pushup(p);
}
void insert(int &p,int key)
{
if(!p) p = get_node(key);
else if (tr[p].key == key)
{
tr[p].cnt++;
}
else if (tr[p].key > key)
{
insert(tr[p].l,key);
if(tr[tr[p].l].val > tr[p].val) zig(p);
}
else
{
insert(tr[p].r,key);
if(tr[tr[p].r].val > tr[p].val) zag(p);
}
pushup(p);
}
void erase(int &p,int key)
{
if(!p) return ;
if(tr[p].key == key)
if(tr[p].cnt > 1) tr[p].cnt--;
else if (tr[p].l || tr[p].r)
{
if(!tr[p].r || tr[tr[p].l].val > tr[tr[p].r].val)
{
zig(p);
erase(tr[p].r,key);
}
else
{
zag(p);
erase(tr[p].l,key);
}
}
else
{
p = 0;
return ;
}
else if(tr[p].key > key) erase(tr[p].l,key);
else erase(tr[p].r,key);
pushup(p);
}
int _rank(int p, int key)
{
if (!p) return 1;//存疑,原为 0
if (tr[p].key == key) return tr[tr[p].l].size + 1;
if (tr[p].key > key) return _rank(tr[p].l,key);
return tr[tr[p].l].size + tr[p].cnt + _rank(tr[p].r,key);
}
int _key(int p, int rank)
{
if(!p) return INF;//或为 return 0
if(tr[tr[p].l].size >= rank) return _key(tr[p].l,rank);
if(tr[tr[p].l].size + tr[p].cnt >= rank) return tr[p].key;
return _key(tr[p].r,rank - tr[tr[p].l].size - tr[p].cnt);
}
int prev(int p, int key)
{
if(!p) return -INF;
if(tr[p].key >= key) return prev(tr[p].l,key);
return max(tr[p].key,prev(tr[p].r,key));
}
int next(int p, int key)
{
if(!p) return INF;
if(tr[p].key <= key)return next(tr[p].r,key);
return min(tr[p].key,next(tr[p].l,key));
}
int main()
{
srand(time(NULL));
scanf("%d%d" , &n, &m);
build();
for(int i = 1; i <= n; i++)
{
int cnt;
scanf("%d" , &cnt);
insert(root, cnt);
}
while(m--)
{
int op,x;
scanf("%d%d" , &op, &x);
x ^= last;
if(op == 1)
{
insert(root,x);
}
else if(op == 2)
{
erase(root,x);
}
else if(op == 3)
{
last = (_rank(root,x) - 1);
ans ^= last;
}
else if(op == 4)
{
last = (_key(root,x + 1));
ans ^= last;
}
else if(op == 5)
{
last = (prev(root,x));
ans ^= last;
}
else if(op == 6)
{
last = (next(root,x));
ans ^= last;
}
// if(op != 1 && op != 2) cout << " " << last << endl;
}
printf("%d" , ans);
return 0;
}
by reclusive @ 2023-05-20 16:41:36
我也一样