传送门
先处理出重链,再用线段树维护区间和(单点更新)
LL ans = 0;
for (int i = h[root]; ~i; i = ne[i])//便利当前root所连的所有边
{
int t = e[i];//与root直接相连的点
if (t == fa[root])//如果是父节点
{
ans += (query_tree(1) - query_tree(root)) * w[i];
}
else//如果是儿子节点
{
ans += query_tree(t) * w[i];
}
}
printf("%lld\n", ans);
#include
using namespace std;
//-----pre_def----
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
#define fir(i, a, b) for (int i = (a); i <= (b); i++)
#define rif(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
#define init_h memset(h, -1, sizeof h), idx = 0;
#define lowbit(x) x &(-x)
//---------------
const int N = 1e6 + 10;
int h[N], e[N << 1], w[N << 1], ne[N << 1], idx;
int n, q, root = 1;
int dep[N], fa[N], sz[N], top[N], son[N], nd[N], cnt;
struct node
{
int l, r;
LL v;
} tr[N << 2];
void add(int a, int b, int c)
{
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx++;
}
void dfs1(int u, int father, int depth)
{
dep[u] = depth, fa[u] = father, sz[u] = 1;
for (int i = h[u]; ~i; i = ne[i])
{
int t = e[i];
if (t == fa[u])
continue;
dfs1(t, u, depth + 1);
sz[u] += sz[t];
if (sz[son[u]] < sz[t])
{
son[u] = t;
}
}
}
void dfs2(int u, int father)
{
nd[u] = ++cnt;
top[u] = father;
if (!son[u])
return;
dfs2(son[u], father);
for (int i = h[u]; ~i; i = ne[i])
{
int t = e[i];
if (t == fa[u] || t == son[u])
continue;
dfs2(t, t);
}
}
void pushup(int u)
{
tr[u].v = tr[u << 1].v + tr[u << 1 | 1].v;
}
void build(int u, int l, int r)
{
tr[u] = {l, r, 0};
if (l == r)
{
return;
}
int mid = l + r >> 1;
build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
}
void modify(int u, int l, int r, int k)
{
//cout<
if (l <= tr[u].l && tr[u].r <= r)
{
tr[u].v += k;
return;
}
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid)
modify(u << 1, l, r, k);
if (mid < r)
modify(u << 1 | 1, l, r, k);
pushup(u);
}
LL query(int u, int l, int r)
{
if (l <= tr[u].l && tr[u].r <= r)
{
return tr[u].v;
}
LL res = 0;
int mid = tr[u].l + tr[u].r >> 1;
if (l <= mid)
res += query(u << 1, l, r);
if (mid < r)
res += query(u << 1 | 1, l, r);
return res;
}
LL query_tree(int u)
{
return query(1, nd[u], nd[u] + sz[u] - 1);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
int StartTime = clock();
#endif
scanf("%d", &n);
init_h;
fir(i, 1, n - 1)
{
int a, b, v;
scanf("%d%d%d", &a, &b, &v);
add(a, b, v);
add(b, a, v);
}
dfs1(1, -1, 1);
dfs2(1, 1);
build(1, 1, n);
scanf("%d", &q);
while (q--)
{
int op, v, x;
scanf("%d", &op);
if (op == 1)
{
scanf("%d%d", &v, &x);
modify(1, nd[v], nd[v], x);
LL ans = 0;
for (int i = h[root]; ~i; i = ne[i])
{
int t = e[i];
if (t == fa[root])
{
ans += (query_tree(1) - query_tree(root)) * w[i];
}
else
{
ans += query_tree(t) * w[i];
}
}
printf("%lld\n", ans);
}
else
{
scanf("%d", &v);
root = v;
LL ans = 0;
for (int i = h[root]; ~i; i = ne[i])
{
int t = e[i];
if (t == fa[root])
{
ans += (query_tree(1) - query_tree(root)) * w[i];
}
else
{
ans += query_tree(t) * w[i];
}
}
printf("%lld\n", ans);
}
}
#ifndef ONLINE_JUDGE
printf("Run_Time = %d ms\n", clock() - StartTime);
#endif
return 0;
}
你可能感兴趣的:(模板&裸题,dfs,icpc)