题目描述

Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges connect them. It is guaranteed that for any two camps, there is one and only one path connect them. At first Aragorn know the number of enemies in every camp. But the enemy is cunning , they will increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they will set two camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they will increase or decrease K soldiers to these camps. Now Aragorn wants to know the number of soldiers in some particular camps real-time.

Input Multiple test cases, process to the end of input.

For each case, The first line contains three integers N, M, P which means there will be N(1 ≤ N ≤ 50000) camps, M(M = N-1) edges and P(1 ≤ P ≤ 100000) operations. The number of camps starts from 1.

The next line contains N integers A1, A2, …AN(0 ≤ Ai ≤ 1000), means at first in camp-i has Ai enemies.

The next M lines contains two integers u and v for each, denotes that there is an edge connects camp-u and camp-v.

The next P lines will start with a capital letter ‘I’, ‘D’ or ‘Q’ for each line.

‘I’, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, increase K soldiers to these camps.

‘D’, followed by three integers C1, C2 and K( 0≤K≤1000), which means for camp C1, C2 and all camps on the path from C1 to C2, decrease K soldiers to these camps.

‘Q’, followed by one integer C, which is a query and means Aragorn wants to know the number of enemies in camp C at that time.

Output For each query, you need to output the actually number of enemies in the specified camp.

输入 输出
3 2 5
1 2 3
2 1
2 3
I 1 3 5
Q 2 7
D 1 2 2
Q 1 4
Q 3 8

思路:用线段树和树链剖分解决,通过树链剖分得出dfs序,按照dfs序进行建树

#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<algorithm> 
#include<string.h>
#include<iostream>
#include<queue>
#include<math.h>
#include<string>
#include<map>
#include<functional>


using namespace std;

#define length 50004

struct edge
{
	int to;
	int next;
}e[1000004];

int ne, cnt, h[length], A[length];


int belong[length], sz[length], pos[length], fa[length], dep[length],son[length];

int n, m, p;

void add(int u, int v)
{
	e[++ne].to = v;
	e[ne].next = h[u];
	h[u] = ne;
}


//线段树

struct point
{
	int val;
	int lazy;
}segtree[length * 4];


void pushup(int rt)
{
	segtree[rt].val = segtree[rt * 2].val + segtree[rt * 2 + 1].val;
}



void pushdown(int rt, int ln, int rn)
{
	if (segtree[rt].lazy)
	{
		segtree[rt * 2].lazy += segtree[rt].lazy;
		segtree[rt * 2 + 1].lazy += segtree[rt].lazy;
		segtree[rt * 2].val += segtree[rt].lazy*ln;
		segtree[rt * 2 + 1].val += segtree[rt].lazy*rn;

		segtree[rt].lazy = 0;
	}

}



//区间更新--增加
void update(int L, int R, int C, int l, int r, int rt)
{
	if (L <= l&&r <= R)
	{
		segtree[rt].val += C*(r - l + 1);
		segtree[rt].lazy += C;
		return;
	}

	int m = (l + r) / 2;

	pushdown(rt, m - l + 1, r - m);

	if (L <= m)update(L, R, C, l, m, rt * 2);

	if (R > m)update(L, R, C, m + 1, r, rt * 2 + 1);

	pushup(rt);

}

int Query(int L, int R, int l, int r, int rt)
{
	if (L <= l&&R >= r)
	{
		return segtree[rt].val;
	}
	if (L > r || R < l)
	{
		return 0;
	}

	int m = (l + r) / 2;

	pushdown(rt, m - l + 1, r - m);

	return Query(L, R, l, m, rt * 2) + Query(L, R, m + 1, r, rt * 2 + 1);
}

void dfs1(int x, int fath)
{
	sz[x] = 1;
	dep[x] = dep[fath] + 1;
	fa[x] = fath;
	son[x] = 0;
	for (int i = h[x]; i; i = e[i].next)
	{
		int to = e[i].to;
		if (to == fath)
		{
			continue;
		}
		dfs1(to, x);
		sz[x] += sz[to];
		if (sz[son[x]] < sz[to])
		{
			son[x] = to;
		}
	}
	return;
}




void dfs2(int x, int topx)
{
	belong[x] = topx;
	pos[x] = ++cnt;
	if (son[x] != 0)dfs2(son[x], topx);

	for (int i = h[x]; i; i = e[i].next)
	{
		if (e[i].to != fa[x] && e[i].to != son[x])
		{
			dfs2(e[i].to, e[i].to);//开启一个新的重链 
		}
	}
}//如果对x为根的整个子树操作,对应区间为[Nid[x],Nidp[x]+size[x]-1]



void solve(int x, int y, int v){
	while (belong[x] != belong[y]){
		if (dep[belong[x]]<dep[belong[y]])swap(x, y);
		update(pos[belong[x]], pos[x], v, 1, n, 1);
		x = fa[belong[x]];
	}
	if (pos[x]>pos[y])swap(x, y);
	update(pos[x], pos[y], v, 1, n, 1);
	return;
}

void ac()
{
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &A[i]);
	}
	cnt = 0;
	ne = 0;
	sz[0] = 0;
	memset(h, 0, sizeof(h));

	for (int i = 1; i <= m; i++)
	{
		int u, v;
		scanf("%d %d", &u, &v);

		add(u, v);
		add(v, u);
	}

	for (int i = 0; i < length * 4; i++)
	{
		segtree[i].lazy = 0;
		segtree[i].val = 0;
	}

	dep[1] = 1;
	dfs1(1,0);
	dfs2(1, 1);

	for (int i = 1; i <= n; i++)
	{
		update(pos[i], pos[i], A[i], 1, n, 1);//按照dfs序进行建树
	}

	while (p--)
	{
		char s[4];//注意这里的输入,p数量级为十万,用cin会tle
		int l, r, x;
		scanf("%s", s + 1);//采用字符串输入可以规避/n造成的干扰
		if (s[1] == 'I')
		{
			int l, r, x;
			scanf("%d %d %d", &l, &r, &x);
			solve(l, r, x);
		}
		if (s[1] == 'D')
		{
			int l, r, x;
			scanf("%d %d %d", &l, &r, &x);
			solve(l, r, -x);
		}
		if (s[1] == 'Q')
		{
			int x;
			scanf("%d", &x);
			int ans = Query(pos[x], pos[x], 1, n, 1);

			printf("%d\n", ans);
		}
	}

}

int main(void)
{
	while (~scanf("%d %d %d", &n, &m, &p))
	{
		ac();
	}

	system("pause");
	return 0;
}