社区讨论

样例过但0pts求条

P3833[SHOI2012] 魔法树参与者 1已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mlyjb660
此快照首次捕获于
2026/02/23 10:05
2 周前
此快照最后确认于
2026/02/23 10:11
2 周前
查看原帖
rt
CPP
#include<bits/stdc++.h>
#define int long long
using namespace std;

const int MAXN = 5e5 + 1;

int n, m, q, r, p, a[MAXN], s[MAXN], maxn[MAXN], lazy[MAXN];
int cnt, fa[MAXN], dep[MAXN], siz[MAXN], dfn[MAXN], son[MAXN], top[MAXN], seg[MAXN], ed[MAXN];
vector<int> c[MAXN];

void dfs1(int x, int f) {
    dep[x] = dep[f] + 1;
    fa[x] = f;
    siz[x] = 1;
    for(int i = 0; i < c[x].size(); i++) {
        int y = c[x][i];
        if(y == f) {
            continue;
        }
        dfs1(y, x);
        siz[x] += siz[y];
        if(siz[y] > siz[son[x]]) {
            son[x] = y;
        }
    }
}

void dfs2(int x, int tp) {
    dfn[x] = ++cnt;
    seg[cnt] = x;
    top[x] = tp;
    if(son[x]) {
        dfs2(son[x], tp);
    }
    for(int i = 0; i < c[x].size(); i++) {
        if(c[x][i] != fa[x] && c[x][i] != son[x]) {
            dfs2(c[x][i], c[x][i]);
        }
    }
    ed[x] = cnt;
}

void push_up(int x) {
    s[x] = s[x * 2] + s[x * 2 + 1];
}
void push_down(int x, int l, int r) {
	if(!lazy[x]) {
		return;
	}
	int mid = (l + r) / 2;
	s[x * 2] += (mid - l + 1) * lazy[x], s[x * 2 + 1] += (r - mid) * lazy[x];
	lazy[x * 2] += lazy[x], lazy[x * 2 + 1] += lazy[x];
	lazy[x] = 0;
}

void build(int x, int l, int r) {
    if(l == r) {
        s[x] = a[l];
        maxn[x] = a[l];
        return;
    }
    int mid = (l + r) / 2;
    build(x * 2, l, mid);
    build(x * 2 + 1, mid + 1, r);
    push_up(x);
}

void update(int L, int R, int l, int r, int k, int c) {
    if(l >= L && r <= R) {
        s[k] += c * (r - l + 1);
		lazy[k] += c;
		return; 
    }
    push_down(k, l, r);
    int mid = (l + r) / 2;
    if(L <= mid) {
        update(L, R, l, mid, k * 2, c);
    }
    if(mid < R) {
        update(L, R, mid + 1, r, k * 2 + 1, c);
    }
    push_up(k);
}

int query_sum(int L, int R, int l, int r, int k) {
    if(l >= L && r <= R) {
        return s[k];
    }
    push_down(k, l, r);
    int mid = (l + r) / 2, res = 0;
    if(L <= mid) {
        res += query_sum(L, R, l, mid, k * 2);
    }
    if(mid < R) {
        res += query_sum(L, R, mid + 1, r, k * 2 + 1);
    }
    return res;
}
int LCA_change(int x, int y, int c) {
    int res = 0;
    while(top[x] != top[y]) {
        if(dep[top[x]] > dep[top[y]]) {
            update(dfn[top[x]], dfn[x], 1, n, 1, 1);
            x = fa[top[x]];
        }
        else {
            update(dfn[top[y]], dfn[y], 1, n, 1, 1);
            y = fa[top[y]];
        }
    }
    if(dfn[x] > dfn[y]) {
        swap(x, y);
    }
    update(dfn[x], dfn[y], 1, n, 1, 1);
    return res;
}

signed main() {
//    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> n;
    for(int i = 1, x, y; i < n; i++) {
        cin >> x >> y;
        x++, y++;
        c[x].push_back(y);
        c[y].push_back(x);
    }
    dfs1(1, 0);
    dfs2(1, 1);
    cin >> q;
    while(q--) {
        char op;
        int x, y, c;
        cin >> op >> x;
        x++;
        if(op == 'A') {
            cin >> y >> c;
            y++;
            LCA_change(x, y, c);
        }
        else {
            cout << query_sum(dfn[x], dfn[x] + siz[x] - 1, 1, n, 1) << "\n";
        }
    }
    return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...