其他分享
首页 > 其他分享> > P3203 [HNOI2010]弹飞绵羊

P3203 [HNOI2010]弹飞绵羊

作者:互联网

Luogu3203

求两点之间的步数,则考虑设size[x]表示x的子树大小,然后在query(x,y)中size[y]-1就是步数

所以设状态只需要满足在\([x,y]\)这个\(splay\)中是合法的就可以了

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
inline int read() {
    int res = 0; bool bo = 0; char c;
    while (((c = getchar()) < '0' || c > '9') && c != '-');
    if (c == '-') bo = 1; else res = c - 48;
    while ((c = getchar()) >= '0' && c <= '9')
        res = (res << 3) + (res << 1) + (c - 48);
    return bo ? ~res + 1 : res;
}
const int N = 2e5 + 5;
int n, m, fa[N], lc[N], rc[N], rev[N], que[N], len, sze[N], val[N];
int which(int x) {return rc[fa[x]] == x;}
bool is_root(int x) {
    return !fa[x] || (lc[fa[x]] != x && rc[fa[x]] != x);
}
void down(int x) {
    if (rev[x]) {
        swap(lc[x], rc[x]);
        if (lc[x]) rev[lc[x]] ^= 1;
        if (rc[x]) rev[rc[x]] ^= 1;
        rev[x] = 0;
    }
}
void upt(int x) {
    sze[x] = 1;
    if (lc[x]) sze[x] += sze[lc[x]];
    if (rc[x]) sze[x] += sze[rc[x]];
}
void rotate(int x) {
    int y = fa[x], z = fa[y], b = lc[y] == x ? rc[x] : lc[x];
    if (z && !is_root(y)) (lc[z] == y ? lc[z] : rc[z]) = x;
    fa[x] = z; fa[y] = x; b ? fa[b] = y : 0;
    if (x == lc[y]) rc[x] = y, lc[y] = b;
    else lc[x] = y, rc[y] = b; upt(y); upt(x);
}
void splay(int x) {
    int i, y; que[len = 1] = x;
    for (y = x; !is_root(y); y = fa[y]) que[++len] = fa[y];
    for (i = len; i >= 1; i--) down(que[i]);
    while (!is_root(x)) {
        if (!is_root(fa[x])) {
            if (which(x) == which(fa[x])) rotate(fa[x]);
            else rotate(x);
        }
        rotate(x);
    }
    upt(x);
}
void Access(int x) {
    int y;
    for (y = 0; x; y = x, x = fa[x]) {
        splay(x); rc[x] = y;
        if (y) fa[y] = x; upt(x);
    }
}
int Find_Root(int x) {
    Access(x); splay(x);
    while (down(x), lc[x]) x = lc[x];
    splay(x); return x;
}
void Make_Root(int x) {
    Access(x); splay(x);
    rev[x] ^= 1;
}
void Link(int x, int y) {
    Make_Root(x); fa[x] = y;
}
void Cut(int x, int y) {
    Make_Root(x); Access(y); splay(y);
    lc[y] = 0; fa[x] = 0; upt(y);
}
int Select(int x, int y) {
    Make_Root(x); Access(y); splay(y);
    return sze[y];
}
int main() {
    int i, x, y, z; n = read();
    for (i = 1; i <= n + 1; i++) sze[i] = 1;
    for (i = 1; i <= n; i++) {
        x = val[i] = read();
        if (i + x <= n) Link(i, i + x);
        else Link(i, n + 1);
    }
    m = read(); while (m--) {
        x = read();
        if (x == 2) {
            y = read() + 1; z = read();
            Cut(y, y + val[y] <= n ? y + val[y] : n + 1);
            Link(y, y + z <= n ? y + z : n + 1);
            val[y] = z;
        }
        else {
            y = read() + 1;
            printf("%d\n", Select(y, n + 1) - 1);
        }
    }
    return 0;
}

标签:Access,int,splay,fa,HNOI2010,P3203,弹飞,include,Root
来源: https://www.cnblogs.com/lizehon/p/10453301.html