洛谷 P3178 [HAOI2015]树上操作
Description
- 有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个操作,分为三种:
- 操作 1 :把某个节点 x 的点权增加 a 。
- 操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。
- 操作 3 :询问某个节点 x 到根的路径中所有点的点权和。
Input
- 第一行包含两个整数 N, M 。表示点数和操作数。
接下来一行 N 个整数,表示树中节点的初始权值。
接下来 N-1 行每行两个正整数 from, to , 表示该树中存在一条边 (from, to) 。
再接下来 M 行,每行分别表示一次操作。其中第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。
Output
- 对于每个询问操作,输出该询问的答案。答案之间用换行隔开。
Sample Input
Sample Output
Data Size
- 对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不会超过 10^6 。
题解:
#include <iostream>
#include <cstdio>
#define N 100005
#define int long long
using namespace std;
struct T {int l, r, val, tag;} t[N * 4];
struct E {int next, to;} e[N * 2];
int n, m, num, dex;
int a[N], h[N], dep[N], size[N], fat[N], son[N], top[N], dfn[N], val[N];
int read()
{
int x = 0, f = 1; char c = getchar();
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x *= f;
}
void add(int u, int v)
{
e[++num].next = h[u];
e[num].to = v;
h[u] = num;
}
void dfs1(int x, int fath, int depth)
{
dep[x] = depth, fat[x] = fath, size[x] = 1;
int maxSon = 0;
for(int i = h[x]; i != 0; i = e[i].next)
if(e[i].to != fath)
{
dfs1(e[i].to, x, depth + 1);
size[x] += size[e[i].to];
if(size[e[i].to] > maxSon)
{
maxSon = size[e[i].to];
son[x] = e[i].to;
}
}
}
void dfs2(int x, int head)
{
top[x] = head, dfn[x] = ++dex, val[dex] = a[x];
if(!son[x]) return;
dfs2(son[x], head);
for(int i = h[x]; i != 0; i = e[i].next)
if(e[i].to != fat[x] && e[i].to != son[x])
dfs2(e[i].to, e[i].to);
}
void build(int p, int l, int r)
{
t[p].l = l, t[p].r = r;
if(l == r) {t[p].val = val[l]; return;}
int mid = (l + r) >> 1;
build(p << 1, l, mid), build(p << 1 | 1, mid + 1, r);
t[p].val = t[p << 1].val + t[p << 1 | 1].val;
}
void down(int p)
{
int son1 = p << 1, son2 = p << 1 | 1;
t[son1].tag += t[p].tag, t[son2].tag += t[p].tag;
t[son1].val += (t[son1].r - t[son1].l + 1) * t[p].tag;
t[son2].val += (t[son2].r - t[son2].l + 1) * t[p].tag;
t[p].tag = 0;
}
void upd(int p, int l, int r, int add)
{
if(t[p].l >= l && t[p].r <= r)
{t[p].tag += add, t[p].val += (t[p].r - t[p].l + 1) * add; return;}
if(t[p].tag) down(p);
int mid = (t[p].l + t[p].r) >> 1;
if(l <= mid) upd(p << 1, l, r, add);
if(r > mid) upd(p << 1 | 1, l, r, add);
t[p].val = t[p << 1].val + t[p << 1 | 1].val;
}
int ask(int p, int l, int r)
{
if(t[p].l >= l && t[p].r <= r) return t[p].val;
if(t[p].tag) down(p);
int mid = (t[p].l + t[p].r) >> 1, ans = 0;
if(l <= mid) ans += ask(p << 1, l, r);
if(r > mid) ans += ask(p << 1 | 1, l, r);
return ans;
}
void updLink(int x, int y, int add)
{
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
upd(1, dfn[top[x]], dfn[x], add);
x = fat[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
upd(1, dfn[x], dfn[y], add);
}
int askLink(int x, int y)
{
int ans = 0;
while(top[x] != top[y])
{
if(dep[top[x]] < dep[top[y]]) swap(x, y);
ans += ask(1, dfn[top[x]], dfn[x]);
x = fat[top[x]];
}
if(dep[x] > dep[y]) swap(x, y);
ans += ask(1, dfn[x], dfn[y]);
return ans;
}
signed main()
{
cin >> n >> m;
for(int i = 1; i <= n; i++) a[i] = read();
for(int i = 1; i < n; i++)
{
int u = read(), v = read();
add(u, v), add(v, u);
}
dfs1(1, 0, 1);
dfs2(1, 1);
build(1, 1, n);
for(int i = 1; i <= m; i++)
{
int op = read();
if(op == 1)
{
int x = read(), add = read();
updLink(x, x, add);
}
else if(op == 2)
{
int x = read(), add = read();
upd(1, dfn[x], dfn[x] + size[x] - 1, add);
}
else if(op == 3)
{
int x = read();
printf("%lld\n", askLink(1, x));
}
}
return 0;
}