题目链接:传送门
树剖板子
区间加,子树查询
树剖里查询的时候x和y地方小于号写反T了一会
a,b写成dfn[a],dfn[b]竟然还有50分又WA了一会
也就交了二十遍
。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <complex>
#include <algorithm>
#include <climits>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define
#define
using namespace std;
typedef long long ll;
int n, m, cnt, a, b, q; ll c;
int fa[A], dfn[A], siz[A], dep[A], top[A], son[A];
namespace Seg {
struct node {
int l, r; ll w, f;
}tree[A << 2];
void build(int k, int l, int r) {
tree[k].l = l; tree[k].r = r;
if (l == r) {
tree[k].w = 0;
return;
}
int m = (l + r) >> 1;
build(k << 1, l, m);
build(k << 1 | 1, m + 1, r);
}
void down(int k) {
tree[k << 1].f += tree[k].f;
tree[k << 1 | 1].f += tree[k].f;
tree[k << 1].w += (tree[k << 1].r - tree[k << 1].l + 1) * tree[k].f;
tree[k << 1 | 1].w += (tree[k << 1 | 1].r - tree[k << 1 | 1].l + 1) * tree[k].f;
tree[k].f = 0;
}
void change(int k, int l, int r, ll add) {
if (tree[k].l >= l and tree[k].r <= r) {
tree[k].w += (tree[k].r - tree[k].l + 1) * add;
tree[k].f += add;
return;
}
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1;
if (l <= m) change(k << 1, l, r, add);
if (r > m) change(k << 1 | 1, l, r, add);
tree[k].w = tree[k << 1].w + tree[k << 1 | 1].w;
}
ll ask(int k, int l, int r) {
if (tree[k].l >= l and tree[k].r <= r) return tree[k].w;
if (tree[k].f) down(k);
int m = (tree[k].l + tree[k].r) >> 1; ll ans = 0;
if (l <= m) ans += ask(k << 1, l, r);
if (r > m) ans += ask(k << 1 | 1, l, r);
return ans;
}
}
namespace Cut {
struct node {
int next, to;
}e[A];
int head[A], num;
void add(int fr, int to) {
e[++num].next = head[fr];
e[num].to = to;
head[fr] = num;
}
void prepare(int fr) {
siz[fr] = 1;
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr]) continue;
fa[ca] = fr;
dep[ca] = dep[fr] + 1;
prepare(ca);
siz[fr] += siz[ca];
if (siz[ca] > siz[son[fr]]) son[fr] = ca;
}
}
void dfs(int fr, int tp) {
dfn[fr] = ++cnt, top[fr] = tp;
if (!son[fr]) return;
dfs(son[fr], tp);
for (int i = head[fr]; i; i = e[i].next) {
int ca = e[i].to;
if (ca == fa[fr] or ca == son[fr]) continue;
dfs(ca, ca);
}
}
void change(int x, int y, ll val) {
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
Seg::change(1, dfn[top[x]], dfn[x], val);
x = fa[top[x]];
}
if (dep[x] > dep[y]) swap(x, y);
Seg::change(1, dfn[x], dfn[y], val);
}
ll ask(int a) {
return Seg::ask(1, dfn[a], dfn[a] + siz[a] - 1);
}
}
int main(int argc, char const *argv[]) {
scanf("%d", &n);
for (int i = 2; i <= n; i++) {
scanf("%d%d", &a, &b); a++; b++;
Cut::add(a, b);
}
Cut::prepare(1); Cut::dfs(1, 1);
Seg::build(1, 1, n);
scanf("%d", &q);
while (q--) {
char opt = getchar();
if (opt == 'A') {
scanf("%d%d%lld", &a, &b, &c); a++; b++;
Cut::change(a, b, c);
}
else {
scanf("%d", &a); a++;
printf("%lld\n", Cut::ask(a));
}
}
}