Bzoj4372: 烁烁的游戏

题面

戳我

Sol

bzoj震波那道题差不多
加上线段树标记永久化就好了

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(2e5 + 10), __(2e7 + 10);

IL ll Read(){
	RG ll x = 0, z = 1; RG char c = getchar();
	for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
	for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
	return x * z;
}

int n, fst[_], nxt[_], w[_], to[_], cnt, Q;

IL void Add(RG int u, RG int v){  nxt[cnt] = fst[u]; to[cnt] = v; fst[u] = cnt++;  }

namespace ChainDiv{
	int fa[_], size[_], top[_], deep[_], son[_], dfn[_], Index;

	IL void Dfs1(RG int u){
		size[u] = 1;
		for(RG int e = fst[u]; e != -1; e = nxt[e]){
			if(size[to[e]]) continue;
			deep[to[e]] = deep[u] + 1; fa[to[e]] = u;
			Dfs1(to[e]);
			size[u] += size[to[e]];
			if(size[to[e]] > size[son[u]]) son[u] = to[e];
		}
	}

	IL void Dfs2(RG int u, RG int Top){
		top[u] = Top; dfn[u] = ++Index;
		if(son[u]) Dfs2(son[u], Top);
		for(RG int e = fst[u]; e != -1; e = nxt[e]) if(!dfn[to[e]]) Dfs2(to[e], to[e]);
	}

	IL ll Dis(RG int u, RG int v){
		RG ll dis = deep[u] + deep[v];
		while(top[u] ^ top[v]){  if(deep[top[u]] < deep[top[v]]) swap(u, v); u = fa[top[u]];  }
		if(deep[u] > deep[v]) swap(u, v);
		return dis - 2 * deep[u];
	}
}

int rot[_], ls[__], rs[__], tag[__], tot;
int size[_], mx[_], frt[_], vis[_], rt, sz, root;

IL void Getroot(RG int u, RG int ff){
	size[u] = 1; mx[u] = 0;
	for(RG int e = fst[u]; e != -1; e = nxt[e]){
		if(vis[to[e]] || to[e] == ff) continue;
		Getroot(to[e], u);
		size[u] += size[to[e]];
		mx[u] = max(mx[u], size[to[e]]);
	}
	mx[u] = max(mx[u], sz - size[u]);
	if(mx[u] < mx[rt]) rt = u;
}

IL void Create(RG int u, RG int ff){
	frt[u] = ff; vis[u] = 1;
	for(RG int e = fst[u]; e != -1; e = nxt[e]){
		if(vis[to[e]]) continue;
		rt = 0; sz = size[to[e]];
		Getroot(to[e], u); Create(rt, u);
	}
}

IL void Update(RG int &x, RG int l, RG int r, RG int pos, RG int d){
	if(!x) x = ++tot;
	if(pos >= r){  tag[x] += d; return;  }
	RG int mid = (l + r) >> 1;
	if(pos > mid) Update(rs[x], mid + 1, r, pos, d);
	Update(ls[x], l, mid, pos, d);
}

IL int Sum(RG int x, RG int add, RG int l, RG int r, RG int k){
	if(!x) return add; add += tag[x];
	if(l == r) return add;
	RG int mid = (l + r) >> 1;
	if(k <= mid) return Sum(ls[x], add, l, mid, k);
	return Sum(rs[x], add, mid + 1, r, k);
}

IL void Modify(RG int u, RG int k, RG int d){
	Update(rot[u], 0, n - 1, k, d);
	for(RG int v = u, dis; frt[v]; v = frt[v]){
		dis = ChainDiv::Dis(frt[v], u);
		if(k < dis) continue;
		Update(rot[frt[v]], 0, n - 1, k - dis, d);
		Update(rot[v + n], 0, n - 1, k - dis, d);
	}
}

IL int Query(RG int u){
	RG int ans = Sum(rot[u], 0, 0, n - 1, 0);
	for(RG int v = u, dis; frt[v]; v = frt[v]){
		dis = ChainDiv::Dis(frt[v], u);
		ans += Sum(rot[frt[v]], 0, 0, n - 1, dis);
		ans -= Sum(rot[v + n], 0, 0, n - 1, dis);
	}
	return ans;
}

int main(RG int argc, RG char* argv[]){
	sz = n = Read(); Q = Read(); Fill(fst, -1);
	for(RG int i = 1, a, b; i < n; ++i) a = Read(), b = Read(), Add(a, b), Add(b, a);
	ChainDiv::Dfs1(1); ChainDiv::Dfs2(1, 1);
	mx[0] = n + 1; cnt = 0;
	Getroot(1, 0); root = rt; Create(rt, 0);
	for(; Q; --Q){
		RG char op; scanf(" %c", &op); RG int x = Read(), y, z;
		if(op == 'Q') printf("%d\n", Query(x));
		else y = Read(), z = Read(), Modify(x, y, z);
	}
    return 0;
}

posted @ 2018-01-13 16:34  Cyhlnj  阅读(295)  评论(0编辑  收藏  举报