本地 + 洛谷IDE AC, 交上去全t, 求调.

P3391 【模板】文艺平衡树

AlphaZe @ 2022-05-12 17:14:04

RT

#include <cstdio>
#include <cctype> 
#include <algorithm>

using namespace std; 

inline int read() {
    int x = 0, f = 0; char ch = getchar(); 
    while (!isdigit(ch)) f = ch == '-', ch = getchar(); 
    while (isdigit(ch)) x = (x << 3) + (x << 1) + (ch ^ 48), ch = getchar(); 
    return f ? -x : x; 
}

const int N = 1e5 + 10; 
int n, m, rt; 
struct Node {
    int fa, son[2], siz, rev, data; 
};
Node nd[N]; 

int get(int x) {
    return nd[nd[x].fa].son[1] == x; 
}

void connect(int x, int fa, int t) {
    if (x) nd[x].fa = fa; 
    if (fa) nd[fa].son[t] = x; 
}

void pushdown(int x) {
    if (nd[x].rev) { 
        swap(nd[x].son[0], nd[x].son[1]); 
        nd[nd[x].son[0]].rev ^= 1; nd[nd[x].son[1]].rev ^= 1; 
        nd[x].rev = 0; 
    }
}

int update(int x) { nd[x].siz = nd[nd[x].son[0]].siz + nd[nd[x].son[1]].siz + 1; }

void rotate(int x) {
    int f = nd[x].fa, ff = nd[f].fa, t = get(x), tf = get(f), b = nd[x].son[t ^ 1]; 
    connect(x, ff, tf); connect(b, f, t); connect(f, x, t ^ 1); 
    update(f); update(x); 
}

void splay(int x, int u) {
    for (int f = nd[x].fa; f != u; f = nd[x].fa) {
        if (nd[f].fa != u) {
            rotate(get(x) == get(f) ? f : x); 
        }
        rotate(x); 
    }
    if (!u) rt = x; 
}

void find(int x, int idx, int u) { 
    pushdown(x); 
    int s = nd[nd[x].son[0]].siz; 
    if (idx == s + 1) {
        splay(x, u); 
    } else if (idx <= s) {
        find(nd[x].son[0], idx, u); 
    } else {
        find(nd[x].son[1], idx - s - 1, u); 
    }
}

void reverse(int l, int r) {
    find(rt, l - 1, 0); 
    find(rt, r + 1, rt); 
    nd[nd[nd[rt].son[1]].son[0]].rev ^= 1;
}

void print(int x) {
    pushdown(x); 
    if (nd[x].son[0]) print(nd[x].son[0]); 
    if (nd[x].data) printf("%d ", nd[x].data); 
    if (nd[x].son[1]) print(nd[x].son[1]); 
}

int main() {
    n = read(); m = read(); 
    rt = n + 2; 
    for (int i = 1; i <= n + 2; ++i) {
        nd[i].fa = i + 1; 
        nd[i].son[0] = i - 1; 
        nd[i].siz = i; 
    }
    for (int i = 2; i <= n + 1; ++i) {
        nd[i].data = i - 1; 
    }
    nd[rt].fa = 0; 
    while (m--) {
        int l = read(), r = read(); 
        reverse(l + 1, r + 1); 
    }
    print(rt); 
    return 0; 
}

by Fleeing_loser @ 2022-05-12 17:23:04

@AlphaZe

这里

int update(int x) { nd[x].siz = nd[nd[x].son[0]].siz + nd[nd[x].son[1]].siz + 1; }

改成这样就行了

int update(int x) 
{ return nd[x].siz = nd[nd[x].son[0]].siz + nd[nd[x].son[1]].siz + 1; }

int 类型的函数一定要加return


by AlphaZe @ 2022-05-12 17:24:37

@真__蒟蒻 %%%


by Lifty @ 2022-05-12 17:25:02

@真__蒟蒻 强强强,帮机房大佬调代码


by AlphaZe @ 2022-05-12 17:27:38

@阿莱丽兹 orz


by Lifty @ 2022-05-12 17:32:32

stO @AlphaZe


|