动态开点权值线段树求助

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

1Stone @ 2022-10-02 10:37:59

数组要开多大才够啊,还是说这题的空间限制根本不能用权值线段树

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mid ((l + r) >> 1)
#define Down 0ll
#define Up (1 << 31)
ll cnt = 1, tree[1100005], lch[1100005], rch[1100005], last = 0, N, T, Sum;
ll read() {
    ll f = 1, x = 0;
    char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = - 1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        x = x * 10 + c - '0';
        c = getchar();
    }
    return f * x;
}
void Pushup(ll R){tree[R] = tree[lch[R]] + tree[rch[R]];}
void Update(ll R, ll l, ll r, ll k, ll v) {
    if(!R) R = ++cnt;
    if(l == r) {
        tree[R] += v;
        return;
    }
    if(k <= mid) {
        if(!lch[R]) lch[R] = ++cnt;
        Update(lch[R], l, mid, k, v);
    } else {
        if(!rch[R]) rch[R] = ++cnt;
        Update(rch[R], mid + 1, r, k, v);
    }
    Pushup(R);
}
ll Qrank(ll R, ll l, ll r, ll ql, ll qr) {
    if(!R) return 0;
    if(l >= ql && r <= qr) return tree[R];
    ll Sum = 0;
    if(mid >= ql) Sum += Qrank(lch[R], l, mid, ql, qr);
    if(mid + 1 <= qr) Sum += Qrank(rch[R], mid + 1, r, ql, qr);
    return Sum;
}
ll Qval(ll R, ll l, ll r, ll k) {
    if(k > tree[R]) return -1ll;
    if(l == r) return l;
    if(k <= tree[lch[R]]) return Qval(lch[R], l, mid, k);
    else return Qval(rch[R], mid + 1, r, k - tree[lch[R]]);
}
int main()
{
    N = read(); T = read();
    for(int i = 1; i <= N; i++) {
        Update(1, Down, Up, read(), 1);
    }
    while(T--) {
        ll op = read(), x = read();
        x ^= last;
        if(op == 1) {
            Update(1, Down, Up, x, 1ll);    
            continue;
        } 
        if(op == 2) {
            Update(1, Down, Up, x, -1ll);
            continue;
        }
        if(op == 3) {
            last = Qrank(1, Down, Up, Down, x - 1) + 1;
        }
        if(op == 4) {
            last = Qval(1 , Down, Up, x);
        }
        if(op == 5) {
            ll W = Qrank(1, Down, Up, Down, x - 1);
            last = Qval(1 , Down, Up, W);
        }
        if(op == 6) {
            ll W = Qrank(1, Down, Up, Down, x);
            last = Qval(1, Down, Up, W + 1);
        }
        Sum ^= last;

    }
    cout << Sum;

    return 0;
}

by Cat_shao @ 2022-10-03 08:23:11

@1Stone 不能用


|