为什么我的Splay大小要开2e5才过

P3391 【模板】文艺平衡树

XiaoYiii @ 2024-08-08 09:01:50

rt,附上代码与RE记录

#include<bits/stdc++.h>
const int N = 2e5 + 10;

int n, m;
namespace Splay{
    struct node{
        int p, s[2], val, siz, tag;
        void init(int _val, int _p){
            val = _val;
            p = _p;
            tag = 0;
        }
    };
    struct Tree{
        node tr[N];
        int root, tot;
        Tree(){
            root = tot = 0;
        }
        void update(int u){
            tr[u].siz = tr[tr[u].s[0]].siz + 1 + tr[tr[u].s[1]].siz;
        }
        void down(int u){
            if(u && tr[u].tag){
                tr[tr[u].s[0]].tag ^= 1;
                tr[tr[u].s[1]].tag ^= 1;
                std::swap(tr[u].s[0], tr[u].s[1]);
                tr[u].tag ^= 1;
            }
        }
        void rotate(int);
        void splay(int, int);
        void insert(int);
        int fid(int);
        int rk(int);
        int kth(int);
        int pren(int);
        int sucn(int);
        int merge(int, int);
        void del(int, int &, int &);
        void del(int);
        void print(int);
        void reserve(int, int);
    };
    void Tree::rotate(int x){
        int y = tr[x].p, z = tr[y].p, k = tr[y].s[1] == x;
        if(z) tr[z].s[tr[z].s[1] == y] = x;
        tr[x].p = z;
        tr[y].s[k] = tr[x].s[k ^ 1];
        if(tr[x].s[k ^ 1]) tr[tr[x].s[k ^ 1]].p = y;
        tr[x].s[k ^ 1] = y;
        tr[y].p = x;
        update(y);
        update(x);
    }
    void Tree::splay(int x, int k){
        while(tr[x].p != k){
            int y = tr[x].p, z = tr[y].p;
            if(z != k){
                if((tr[y].s[1] == x) ^ (tr[z].s[1] == y)) rotate(x);
                else rotate(y);
            }
            rotate(x);
        }
        if(!k) root = x;
    }
    void Tree::insert(int val){
        int u = root, p = 0;
        while(u) p = u, u = tr[u].s[val > tr[u].val];
        u = ++tot;
        if(p) tr[p].s[val > tr[p].val] = u;
        tr[u].init(val, p);
        splay(u, 0);
    }
    int Tree::kth(int k){
        int u = root;
        while(u){
            down(u);
            if(tr[tr[u].s[0]].siz >= k) u = tr[u].s[0];
            else if(tr[tr[u].s[0]].siz + 1 == k) return u;
            else k -= tr[tr[u].s[0]].siz + 1, u = tr[u].s[1];
        }
        return -1;
    }
    void Tree::print(int u){
        if(!u) return;
        down(u);
        print(tr[u].s[0]);
        //printf("%d %d %d %d\n", u, tr[u].s[0], tr[u].s[1], tr[u].val);
        if(tr[u].val >= 1 && tr[u].val <= n) printf("%d ", tr[u].val);
        print(tr[u].s[1]);
    }
    void Tree::reserve(int x, int y){
        int l = kth(x), r = kth(y + 2);
        if(l == -1 || r == -1){
            puts("!");
            return;
        }
        splay(l, 0);
        splay(r, l);
        int pos = tr[r].s[0];
        if(pos) tr[pos].tag ^= 1;
    }
}
using namespace std;
using namespace Splay;
int main(){
    Tree T;
    scanf("%d%d", &n, &m);
    for(int i = 0; i <= n + 1; i++) T.insert(i);
    int x, y;
    while(m--){
        scanf("%d%d", &x, &y);
        T.reserve(x, y);
    }
    T.print(T.root);

    return 0;
}

|