P3178 [HAOI2015]树上操作

(color{#0066ff}{题目描述})

有一棵点数为 N 的树,以点 1 为根,且树点有边权。然后有 M 个操作,分为三种:

操作 1 :把某个节点 x 的点权增加 a 。
操作 2 :把某个节点 x 为根的子树中所有点的点权都增加 a 。
操作 3 :询问某个节点 x 到根的路径中所有点的点权和。

(color{#0066ff}{输入格式})

第一行包含两个整数 N, M 。表示点数和操作数。
接下来一行 N 个整数,表示树中节点的初始权值。
接下来 N-1 行每行两个正整数 from, to , 表示该树中存在一条边 (from, to) 。
再接下来 M 行,每行分别表示一次操作。其中第一个数表示该操作的种类( 1-3 ) ,之后接这个操作的参数( x 或者 x a ) 。

(color{#0066ff}{输出格式})

对于每个询问操作,输出该询问的答案。答案之间用换行隔开。

(color{#0066ff}{输入样例})

5 5
1 2 3 4 5
1 2
1 4
2 3
2 5
3 3
1 2 1
3 5
2 1 2
3 3

(color{#0066ff}{输出样例})

6
9
13

(color{#0066ff}{数据范围与提示})

对于 100% 的数据, N,M<=100000 ,且所有输入数据的绝对值都不会超过 10^6 。

(color{#0066ff}{题解})

操作1.2,线段树上直接修改

操作3,暴力跳top

不开LL见祖宗

#include <bits/stdc++.h>

#define LL long long

const int maxn = 1e5 + 10;
LL in() {
	char ch; LL x = 0, f = 1;
	while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48), ch = getchar();
	return x * f;
}

struct node {
	int to;
	node *nxt;
	node(int to = 0, node *nxt = NULL):to(to), nxt(nxt) {}
	void *operator new (size_t) {
		static node *S = NULL, *T = NULL;
		return (S == T) && (T = (S = new node[1024]) + 1024), S++;
	}
};

struct sgt {
	int l, r;
	LL tag, val;
	sgt *ch[2];
	sgt(int l = 0, int r = 0, int tag = 0, int val = 0):l(l), r(r), tag(tag), val(val) {}
	void *operator new (size_t) {
		static sgt *S = NULL, *T = NULL;
		return (S == T) && (T = (S = new sgt[1024]) + 1024), S++;
	}
	LL siz() { return r - l + 1; }
	void upd() { val = ch[0]->val + ch[1]->val; }
	void dwn() {
		if(!tag) return;
		ch[0]->val += ch[0]->siz() * tag;
		ch[1]->val += ch[1]->siz() * tag;
		ch[0]->tag += tag;
		ch[1]->tag += tag;
		tag = 0;
	}
};

int val[maxn], dep[maxn], top[maxn], dfn[maxn], redfn[maxn], fa[maxn], son[maxn], siz[maxn], cnt;
int n, m;
node *head[maxn];
sgt *root;

void add(int from, int to) {
	node *o = new node(to, head[from]);
	head[from] = o;
}

void dfs1(int x, int f) {
	fa[x] = f;
	siz[x] = 1;
	dep[x] = dep[f] + 1;
	for(node *i = head[x]; i; i = i->nxt) {
		if(i->to == f) continue;
		dfs1(i->to, x);
		siz[x] += siz[i->to];
		if(!son[x] || siz[i->to] > siz[son[x]]) son[x] = i->to;
	}
}

void dfs2(int x, int t) {
	top[x] = t;
	dfn[x] = ++cnt;
	redfn[cnt] = x;
	if(son[x]) dfs2(son[x], t);
	for(node *i = head[x]; i; i = i->nxt)
		if(!dfn[i->to]) dfs2(i->to, i->to);
}

void build(sgt *&o, int l, int r) {
	o = new sgt(l, r, 0, 0);
	if(l == r) return (void)(o->val = val[redfn[l]]);
	int mid = (l + r) >> 1;
	build(o->ch[0], l, mid);
	build(o->ch[1], mid + 1, r);
	o->upd();
}

void lazy(sgt *o, int l, int r, int k) {
	if(o->r < l || o->l > r) return;
	if(l <= o->l && o->r <= r) {
		o->tag += k;
		o->val += o->siz() * k;
		return;
	}
	o->dwn();
	lazy(o->ch[0], l, r, k), lazy(o->ch[1], l, r, k);
	o->upd();
}

LL query(sgt *o, int l, int r) {
	if(o->r < l || o->l > r) return 0;
	if(l <= o->l && o->r <= r) return o->val;
	o->dwn();
	return query(o->ch[0], l, r) + query(o->ch[1], l, r);
}

LL querypath(int x) {
	LL ans = 0;
	int fx = top[x];
	while(x) {
		fx = top[x];
		ans += query(root, dfn[fx], dfn[x]);
		x = fa[fx];
	}
	return ans;
}

int main() {
	n = in(), m = in();
	int p, x;
	for(int i = 1; i <= n; i++) val[i] = in();
	for(int i = 1; i < n; i++) p = in(), x = in(), add(p, x), add(x, p);
	dfs1(1, 0);
	dfs2(1, 1);
	build(root, 1, n);
	while(m--) {
		p = in(), x = in();
		if(p == 1) lazy(root, dfn[x], dfn[x], in());
		if(p == 2) lazy(root, dfn[x], dfn[x] + siz[x] - 1, in());
		if(p == 3) printf("%lld
", querypath(x));
	}
	return 0;
}
原文地址:https://www.cnblogs.com/olinr/p/10140155.html