36分求助

P6136 【模板】普通平衡树(数据加强版)

yuechu0 @ 2023-09-05 20:42:07

样例其实也不对

#include<bits/stdc++.h>
using namespace std;
#define IOS cin.tie(0) -> sync_with_stdio(false), cout.tie(0);
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define endl '\n'
using ll = long long;
using pii = pair<ll, ll>;

const int N = 2e6 + 10;

ll n, m;

struct node {
    int s[2];
    int p;
    int v;
    int cnt;
    int size;
    void init(int p1, int v1) {
        p = p1, v = v1; cnt = size = 1;
    } 
}tr[N];
int root, idx;

void pushup(int x) {
    tr[x].size = tr[tr[x].s[0]].size + tr[tr[x].s[1]].size 
        + tr[x].cnt;
}

void rotate(int x) {
    int y = tr[x].p, z = tr[y].p;
    int k = tr[y].s[1] == x;
    tr[y].s[k] = tr[x].s[k ^ 1];
    tr[tr[x].s[k ^ 1]].p = y;
    tr[x].s[k ^ 1] = y;
    tr[y].p = x;
    tr[z].s[tr[z].s[1] == y] = x;
    tr[x].p = z;
    pushup(y), pushup(x);
}  

void splay(int x, int k) {
    while(tr[x].p != k) {
        int y = tr[x].p, z = tr[y].p;
        if(z != k) {
            (tr[y].s[0] == x) ^ (tr[z].s[0] == y) ? 
            rotate(x) : rotate(y);
        }
        rotate(x);
    }
    if(k == 0) root = x;
}

void find(int v) {
    int x = root;
    while(tr[x].s[v > tr[x].v] && v != tr[x].v) 
        x = tr[x].s[v > tr[x].v];
    splay(x, 0);
}

int get_pre(int v) {
    find(v);
    int x = root;
    if(tr[x].v < v) return x;
    x = tr[x].s[0];
    while(tr[x].s[1]) x = tr[x].s[1];
    return x;
}

int get_suc(int v) {
    find(v);
    int x = root;
    if(tr[x].v > v) return x;
    x = tr[x].s[1];
    while(tr[x].s[0]) x = tr[x].s[0];
    return x; 
}

void del(int v) {
    int pre = get_pre(v);
    int suc = get_suc(v);
    splay(pre, 0), splay(suc, pre);
    int del = tr[suc].s[0];
    if(tr[del].cnt > 1) tr[del].cnt --, splay(del, 0);
    else tr[suc].s[0] = 0, splay(suc, 0);
}

int get_rank(int v) {
    find(v);
    return tr[tr[root].s[0]].size;
}

int get_val(int k) {
    int x = root;
    while(1) {
        int y = tr[x].s[0];
        if(tr[y].size + tr[x].cnt < k) {
            k -= tr[y].size + tr[x].cnt;
            x = tr[x].s[1];
        }
        else {
            if(tr[y].size >= k) x = tr[x].s[0];
            else break;
        }
    }
    splay(x, 0);
    return tr[x].v;
}

void insert(int v) {
    int x = root, p = 0;
    while(x && tr[x].v != v) 
        p = x, x = tr[x].s[v > tr[x].v];
    if(x) tr[x].cnt ++;
    else {
        x = ++idx;
        tr[p].s[v > tr[p].v] = x;
        tr[x].init(p, v);
    }
    splay(x, 0);
}

void solve() {
    cin>>n>>m;
    int op, x;
    insert(-1e9), insert(1e9);
    for(int i = 1; i <= n; i ++) cin>>x, insert(x);
    int last = 0;
    int ans = 0;
    while(m --) {
        cin>>op>>x;
        x ^= last;
        if(op == 1) insert(x);
        if(op == 2) del(x + 1);
        if(op == 3) last = get_rank(x);
        if(op == 4) last = get_val(x + 1);
        if(op == 5) last = tr[get_pre(x)].v;
        if(op == 6) last = tr[get_suc(x)].v;
        //cout<<op<<' '<<x<<' '<<last<<endl;
        if(op > 2) ans ^= last;
    }
    cout<<ans<<endl;
}

int main() {
    IOS
    #ifdef yuechu
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    #endif
    //int t; cin>>t; while(t --)
        solve();
    return 0;
}

|