萌新树剖不过样例求调
查看原帖
萌新树剖不过样例求调
439207
hhw_khw楼主2022/3/12 23:51
#include <bits/stdc++.h>
using namespace std;
const int maxn = 5e3 + 10;
struct edge {
    int to, next;
} e[maxn << 1];
int head[maxn], cnt, ecnt;
int top[maxn], fa[maxn], d[maxn], siz[maxn], son[maxn], a[maxn], id[maxn];
int n, m;
int route[maxn];
struct node {
    int l, r, val, len, lazy;
} t[maxn << 2];
void dfs1(int p, int f) {
    siz[p] = 1;
    fa[p] = f;
    d[p] = d[f] + 1;
    for (int i = head[p]; i; i = e[i].next) {
        if (e[i].to == f) continue;
        dfs1(e[i].to, p);
        siz[p] += siz[e[i].to];
        if (p == 1) cout << e[i].to << ' ' << siz[e[i].to] << endl;
        if (siz[e[i].to] > siz[son[p]]) son[p] = e[i].to;
    }
}
void dfs2(int p, int t) {
    top[p] = t;
    id[p] = ++ ecnt;
    if (son[p]) {
        dfs2(son[p], t);
    }
    for (int i = head[p]; i; i = e[i].next) {
        if (e[i].to == son[p] || e[i].to == fa[p]) continue;
        dfs2(e[i].to, e[i].to);
    }
}
void build(int p, int l, int r) {
    t[p].l = l;
    t[p].r = r;
    t[p].len = r - l + 1;
    if (l == r) {
//        t[p].val = a[l];
        t[p].lazy = -1;
        return;
    }
    int mid = l + r >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
    t[p].val = t[p << 1].val + t[p << 1 | 1].val;
}
void pushdown(int p) {
    if (t[p].lazy != -1) {
        t[p << 1].val = t[p << 1].len * t[p].lazy;
        t[p << 1].lazy = t[p].lazy;
        t[p << 1 | 1].val = t[p << 1 | 1].len * t[p].lazy;
        t[p << 1 | 1].lazy = t[p].lazy;
        t[p].lazy = -1;
    }
}
void modify(int p, int l, int r, int val) {
    if (l > r) swap(l, r);
    if (t[p].l >= l && t[p].r <= r) {
        t[p].val = t[p].len * val;
        t[p].lazy = val;
        return;
    }
    pushdown(p);
    if (t[p << 1].r >= l) modify(p << 1, l, t[p << 1].r, val);
    if (t[p << 1 | 1].l <= r) modify(p << 1 | 1, t[p << 1 | 1].l, r, val);
    t[p].val = t[p << 1].val + t[p << 1 | 1].val;
}
void rmodify(int u, int v) {
    while (top[u] != top[v]) {
        if (d[top[u]] < d[top[v]]) swap(u, v);
        modify(1, top[u], id[u], 1);
        u = fa[top[u]];
    }
    if (d[top[u]] < d[top[v]]) swap(u, v);
    cout << top[u] << ' ' << top[v] << endl;
    cout << u << ' ' << v << endl;
    modify(1, id[top[u]], id[top[v]], 1);
}
int query(int p, int l, int r) {
    if (t[p].l >= l && t[p].r <= r) return t[p].val;
    pushdown(p);
    int res = 0;
    if (t[p << 1].r >= l) res += query(p << 1, l, r);
    if (t[p << 1 | 1].l <= r) res += query(p << 1 | 1, l, r);
}
void add(int x, int y) {
    e[++ cnt].next = head[x];
    e[cnt].to = y;
    head[x] = cnt;
}
int main() {
    cin >> n;
    for (int i = 1; i <= n - 1; i ++) {
        int temp;
        cin >> temp;
        temp ++;
        if (i != temp) {
            add(i, temp);
            add(temp, i);
        }
    }
    dfs1(1, 0);
    dfs2(1, 1);
    cin >> m;
    for (int i = 1; i <= m; i ++) {
        string opt;
        int temp;
        int sum;
        sum = t[1].val;
        cin >> opt >> temp;
        temp ++;
        if (opt == "install") {
            rmodify(temp, 1);
            cout << sum - t[1].val << endl;
        }
        else {
            modify(1, id[temp], id[temp] + siz[temp] - 1, 0);
            cout << sum - t[1].val << endl;
        }
    }
    return 0;
}


2022/3/12 23:51
加载中...