P3833 [SHOI2012]魔法树
大致题意就是让你写一个程序,支持维护某一条路径上点的权值和查询一颗子树上的点的权值和。
最近做树剖的裸题都做傻了qaq。
思路:树剖+线段树维护。
和板子题出奇的像。
对于路径上加值的操作转化为链上的操作,用线段树维护。
查询子树的信息,易知一颗子树上的\(dfs\)序是连续的,所以也是用线段树维护qwq。
树剖不太透彻的可以去翻我博客qwq。
有一点就是我在输入操作种类时用的\(getchat()\),莫名其妙的\(WA\)掉了。
后来才发现是\(getchar()\)会读入换行符....qaq,注意一下就好了。
代码:
#include <bits/stdc++.h>
using namespace std;
template<typename temp>void read(temp &x){
x = 0;temp f = 1;char ch;
while(!isdigit(ch = getchar())) (ch == '-') and (f = -1);
for(x = ch^48; isdigit(ch = getchar()); x = (x<<1)+(x<<3)+(ch^48));
x *= f;
}
template <typename temp, typename ...Args>void read(temp& a, Args& ...args){read(a), read(args...);}
const int maxn(1e6);
int n, q;
vector<int> v[maxn];
struct segpoutree{
#define ls (now << 1)
#define rs (now<<1|1)
#define mid ((l+r)>>1)
int cnt, dfn[maxn], dep[maxn], fa[maxn], top[maxn], size[maxn], height_son[maxn], id[maxn];
struct no{
long long l, r, sum, tag;
long long get(){return sum + tag*(r-l+1);}
}node[maxn<<1];
void OvO(){return;}
void build_poutree(int now){
size[now] = 1;
for(int i = 0; i < v[now].size(); i ++){
int to = v[now][i];
if(dep[to]) continue;
dep[to] = dep[fa[to]=now] + 1;
build_poutree(to);
size[now] += size[to];
if(size[to] > size[height_son[now]]) height_son[now] = to;
}
}
void dfs(int now, int topfa){
top[now] = topfa, dfn[now] = ++cnt;
if(height_son[now]) dfs(height_son[now], topfa);
for(int i = 0; i < v[now].size(); i ++){
int to = v[now][i];
if(fa[now] == to or height_son[now] == to) continue;
dfs(to,to);
}
}
inline void up(int now){return (void)(node[now].sum = node[ls].get() + node[rs].get());}
inline void down(int now){
node[ls].tag += node[now].tag, node[rs].tag += node[now].tag;
node[now].tag = 0;
return OvO();
}
void build_segtree(int l, int r, int now){
node[now].l = l, node[now].r = r;
if(l == r) return (void)(node[now].sum = 0);
build_segtree(l, mid, ls), build_segtree(mid+1, r, rs);
return up(now);
}
void chenge(int l, int r, int now, int val){
if(node[now].l > r or node[now].r < l) return;
if(l <= node[now].l and node[now].r <= r) return (void)(node[now].tag += val);
down(now);
chenge(l, r, ls, val), chenge(l, r, rs, val);
return up(now);
}
void quary(int l, int r, int now, long long &ans){
if(node[now].l > r or node[now].r < l) return;
if(l <= node[now].l and node[now].r <= r) return (void)(ans += node[now].get());
down(now);
quary(l, r, ls, ans), quary(l, r, rs, ans);
return up(now);
}
void function(){
char opt;
cin >> opt;
if(opt == 'A'){
int x, y, z;
read(x, y, z);
x += 1, y += 1;
while(top[x] != top[y]){
if(dep[top[x]] < dep[top[y]]) swap(x,y);
chenge(dfn[top[x]], dfn[x], 1, z);
x = fa[top[x]];
}
if(dep[x] < dep[y]) swap(x,y);
return chenge(dfn[y], dfn[x], 1, z);
}else{
long long ans = 0; int x;
read(x);
x += 1;
quary(dfn[x], dfn[x]+size[x]-1, 1, ans);
printf("%lld\n", ans);
return OvO();
}
}
}tree;
signed main(){
read(n);
for(int i = 1, x, y; i < n; i ++){
read(x,y);
v[x+1].push_back(y+1);
v[y+1].push_back(x+1);
}
read(q);
tree.build_poutree(1*(tree.dep[1]=1)), tree.dfs(1,1), tree.build_segtree(1,n,1);
for(int qwq; q; q --) tree.function();
return 0;
}