救救萌新

P3391 【模板】文艺平衡树

ZEROLANE @ 2019-07-25 16:03:37

RT 死循环了,还查不出错

#include<bits/stdc++.h>

#define scf scanf
#define ptf printf

#define ll long long
#define Rint register int 
#define Rll register long long

#define mid (l + r >> 1)
using namespace std;
const int N = (6e5 + 101) * 40;
int num[N];
int cnt[N], ch[N][2], fath[N], rak[N], size[N], lazy[N];
int lenth;
int  tot = 0, root;
inline int read(){
    int  x = 0, f = 1;
    char ch = getchar();
    while(!isdigit(ch)){
        if(ch == '-')
            f = -1;
        ch = getchar();
    }
    while(isdigit(ch)){
        x = (x << 1) + (x << 3) + (ch ^ 48);
        ch = getchar();
    }
    return x * f; 
}
inline void clea_p(Rint  x){
    ch[x][0] = ch[x][1] = fath[x] = size[x] = cnt[x] = rak[x] = 0;
}
inline bool get(Rint x){
    return ch[fath[x]][1] == x;
}
inline void pushup(Rint x){
    size[x] += size[ch[x][0]] + size[ch[x][1]] + 1;     
}
inline void pushdown(Rint now){
    if(lazy[now]){
        lazy[ch[now][0]] ^= 1;
        lazy[ch[now][1]] ^= 1;
        swap(ch[now][0], ch[now][1]);
        lazy[now] = 0;
    }
}
inline void rotate(Rint x){
    int x_fa = fath[x], fa_fa = fath[x_fa];
    int x_pla = get(x), x_fa_pla = get(x_fa);
    if(fa_fa)
        ch[fa_fa][x_fa_pla] = x;
    else
        root = x;
    fath[x] = fa_fa;
    if(ch[x][!x_pla])
        fath[ch[x][!x_pla]] = x_fa;
        ch[x_fa][x_pla] = x_fa;
        ch[x][!x_pla] = x_fa;
        fath[x_fa] = x;
        pushup(x_fa); 
        pushup(x);
}
inline void splay(Rint x, Rint pos){
    while(fath[x] != pos){
        int x_fa = fath[x], fa_fa = fath[x_fa];
        if(fa_fa == pos)
            rotate(x);
        else{
            if(get(x) == get(x_fa)){
                rotate(x_fa);
                rotate(x);
            }
            else{
                rotate(x);
                rotate(x);
            }
        }
    }   
}
inline int find(Rint x){
    int now = root;
    while(1){
        pushdown(now);
        if(ch[now][0] && x <= size[ch[now][0]])
            now = ch[now][0];
        else{
            int temp = (ch[now][0] ? size[ch[now][0]] : 0) + 1;
            if(x <= temp)
                return now;
            x -= temp;
            now = ch[now][1];    
        }   
    }
}
inline int build(Rint rt, Rint l, Rint r){
    fath[mid] = rt;
    rak[mid] = num[mid];
    if(l < mid)
        ch[mid][0] = build(mid, l, mid - 1);
    if(r > mid)
        ch[mid][1] = build(mid, mid + 1, r);    
    pushup(mid);
    return mid;  
}
inline void print(Rint now){
    pushdown(now);
    if(ch[now][0])
        print(ch[now][0]);
    num[++tot] = rak[now];
    if(ch[now][1])
        print(ch[now][1]);  
}
int main(){
    int n = read(), m = read();
    for(Rint i = 1; i <= n + 2; i++)
        num[i] = i - 1;
    root = build(0, 1, n + 2);  
    for(Rint i = 1; i <= m; i++){
        int x = read(), y = read();
        int x_ = find(x), y_ = find(y + 2);
        splay(x_, 0);
        splay(y_, x_);
        lazy[ch[ch[root][1]][0]] ^= 1;
    }
    print(root);
    for(Rint i = 1; i <= n; i++)
        printf("%d ",num[i + 1]);
    return 0;
}

by Gary818 @ 2019-07-25 16:04:30

QNMDMX


|