Description
给一颗n个节点的树,边权均为1,初始点权均为0,m次操作:
Q x:询问x的点权。
M x d w:将树上与节点x距离不超过d的节点的点权均加上w。
Solution
与震波差不多,只是换成了区间修改、单点查询。
不卡常就是舒服
Code
/************************************************
* Au: Hany01
* Date: Jul 9th, 2018
* Prob: BZOJ4372 烁烁的游戏
* Inst: Yali High School
************************************************/
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
#define File(a) freopen(a".in", "r", stdin), freopen(a".out", "w", stdout)
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia
template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline bool chkmin(T &a, T b) { return b < a ? a = b, 1 : 0; }
inline int read()
{
register int _, __; register char c_;
for (_ = 0, __ = 1, c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -1;
for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << 1) + (_ << 3) + (c_ ^ 48);
return _ * __;
}
const int maxn = 100005;
int n, m, beg[maxn], e, nex[maxn << 1], v[maxn << 1], tot, ST[19][maxn * 3], Log[maxn * 3], rt, ns, Fa[maxn], Rt[maxn << 1], dep[maxn], maxch[maxn], sz[maxn], vis[maxn], pos[maxn];
inline void add(int uu, int vv) { v[++ e] = vv, nex[e] = beg[uu], beg[uu] = e; }
void init(int u, int pa = 0) {
ST[0][pos[u] = ++ tot] = u, dep[u] = dep[pa] + 1;
for (register int i = beg[u]; i; i = nex[i])
if (v[i] != pa) init(v[i], u), ST[0][++ tot] = u;
}
inline int LCA(int u, int v) {
static int l, r, x, y, t;
l = pos[u], r = pos[v];
if (l > r) swap(l, r);
return dep[x = ST[t = Log[r - l + 1]][l]] < dep[y = ST[t][r - (1 << t) + 1]] ? x : y;
}
inline int dist(int u, int v) { return dep[u] + dep[v] - (dep[LCA(u, v)] << 1); }
void getrt(int u, int pa = 0) {
sz[u] = 1, maxch[u] = 0;
for (register int i = beg[u]; i; i = nex[i])
if (v[i] != pa && !vis[v[i]]) getrt(v[i], u), sz[u] += sz[v[i]], chkmax(maxch[u], sz[v[i]]);
chkmax(maxch[u], ns - sz[u]);
if (maxch[rt] > maxch[u]) rt = u;
}
void gettree(int u, int pa = 0) {
vis[u] = 1, Fa[u] = pa;
for (register int i = beg[u], prens = ns; i; i = nex[i]) if (!vis[v[i]])
ns = sz[u] < sz[v[i]] ? prens - sz[u] : sz[v[i]], rt = 0, getrt(v[i]), gettree(rt, u);
}
struct SegmentTreeNode { int val, lc, rc; }tr[maxn * 200];
#define mid ((l + r) >> 1)
void update(int &t, int l, int r, int x, int y, int dt) {
if (!t) t = ++ tot;
if (x <= l && r <= y) { tr[t].val += dt; return; }
if (x <= mid) update(tr[t].lc, l, mid, x, y, dt);
if (y > mid) update(tr[t].rc, mid + 1, r, x, y, dt);
}
int query(int t, int l, int r, int x) {
if (!t) return 0;
if (l == r) return tr[t].val;
if (x <= mid) return tr[t].val + query(tr[t].lc, l, mid, x);
return tr[t].val + query(tr[t].rc, mid + 1, r, x);
}
#undef mid
inline void Update() {
int u = read(), d = read(), w = read();
update(Rt[u], 0, n - 1, 0, d, w);
for (int v = u, t; Fa[v]; v = Fa[v])
if (d >= (t = dist(u, Fa[v])))
update(Rt[Fa[v]], 0, n - 1, 0, d - t, w), update(Rt[v + n], 0, n - 1, 0, d - t, w);
}
inline int Query() {
int u = read(), ans = query(Rt[u], 0, n - 1, 0);
for (int v = u, t; Fa[v]; v = Fa[v])
t = dist(u, Fa[v]), ans += query(Rt[Fa[v]], 0, n - 1, t) - query(Rt[v + n], 0, n - 1, t);
return ans;
}
int main()
{
#ifdef hany01
File("bzoj4372");
#endif
static int uu, vv;
static char op[3];
n = read(), m = read();
For(i, 2, n) uu = read(), vv = read(), add(uu, vv), add(vv, uu);
init(1);
For(i, 2, tot) Log[i] = Log[i >> 1] + 1;
For(j, 1, Log[tot]) For(i, 1, tot - (1 << j) + 1)
ST[j][i] = dep[uu = ST[j - 1][i]] > dep[vv = ST[j - 1][i + (1 << (j - 1))]] ? vv : uu;
tot = 0, ns = n, maxch[rt = 0] = INF, getrt(1), gettree(rt);
while (m --) {
scanf("%s", op);
if (op[0] == 'Q') printf("%d\n", Query()); else Update();
}
return 0;
}
//人生如逆旅,我亦是行人。
// -- 苏轼《临江仙·送钱穆父》