HDU

来源:互联网 发布:微信网页授权 多域名 编辑:程序博客网 时间:2024/06/07 18:31

Aragorn's Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13639    Accepted Submission(s): 3632


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.
 

Sample Input
3 2 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
 

Sample Output
748
Hint
1.The number of enemies may be negative.2.Huge input, be careful.
 

题目大意:
 给一棵树,并给定各个点权的值,然后有3种操作: 
I C1 C2 K: 把C1与C2的路径上的所有点权值加上K 
D C1 C2 K:把C1与C2的路径上的所有点权值减去K 
Q C:查询节点编号为C的权值
解题思路:
先将树进行树链剖分,然后用线段树进行维护
具体树链剖分部分,会在代码中注释体现

对于边权问题来说,修改的地方为我们要判断这两个的深度,对于这两个点的边权来说,将值赋给深度较深的那个点
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <set>#include <map>#include <string>#include <cmath>#include <cstdlib>#include <ctime>using namespace std;typedef long long LL;//strattime:2017/10/11 13:12//endtime:2017/10/9 21:07const int MAXN=50010;int cnt,son[MAXN],deep[MAXN],father[MAXN],siz[MAXN],top[MAXN],pre[MAXN],tid[MAXN];int n,m,q;struct point{int flag,cnt;}tree[MAXN*4];vector<int> tu[MAXN];int a[MAXN];void init(){memset(son,0,sizeof(son));memset(tu,0,sizeof(tu));cnt=0;}//树链剖分部分void dfs1(int s,int fa,int de)//沿着树查找子节点以及子树中的重链{int i,k;deep[s]=de;//deep[s]表示s这个点所在的层数为defather[s]=fa;//father[s]表示s这个点的父亲节点为fasiz[s]=1;//sizes[s]表示以s为根节点的子树一共有几个点for(i=0;i<tu[s].size();i++){k=tu[s][i];if(k==fa)continue;dfs1(k,s,de+1);siz[s]+=siz[k];if(son[s]==0||siz[k]>siz[son[s]])son[s]=k;//son[s]表示与s在同一重链上的儿子(俗称重儿子)}}void dfs2(int x,int tp)//将树转化为线段树{int i,k;top[x]=tp;//top[x]表示x所在重链上的头结点tid[x]=++cnt;//tid[x]表示x在线段树上的编号pre[cnt]=x;//pre[cnt]表示cnt这个标号代表的树上的点if(son[x])dfs2(son[x],tp);for(i=0;i<tu[x].size();i++){k=tu[x][i];if(k!=son[x]&&k!=father[x])//轻边dfs2(k,k);}}//线段树部分void build(int l,int r,int de){tree[de].flag=0;if(l==r){tree[de].cnt=a[pre[l]];return;}int mid=(l+r)/2;build(l,mid,de*2);build(mid+1,r,de*2+1);}void putdown(int de,int k){if(tree[de].flag){tree[de*2+1].flag+=tree[de].flag;tree[de*2].flag+=tree[de].flag;tree[de*2].cnt+=(k-k/2)*tree[de].flag;tree[de*2+1].cnt+=k/2*tree[de].flag;tree[de].flag=0;}}void update(int L,int R,int l,int r,int de,int k){if(L<=l&&r<=R){tree[de].flag+=k;tree[de].cnt+=(r-l+1)*k;return ;}putdown(de,r-l+1);int mid=(l+r)/2;if(L<=mid)update(L,R,l,mid,de*2,k);if(mid<R)update(L,R,mid+1,r,de*2+1,k);}int query(int l,int r,int de,int pos){if(l==r)return tree[de].cnt;putdown(de,r-l+1);int mid=(l+r)/2;int sum=0;if(pos<=mid) sum=query(l,mid,de*2,pos);else sum=query(mid+1,r,de*2+1,pos);return sum;}void change(int x,int y,int k){while(top[x]!=top[y]){if(deep[top[x]]<deep[top[y]]) swap(x,y);update(tid[top[x]],tid[x],1,n,1,k);x=father[top[x]];}
//f(x==y) return 值    为边权的修改if(deep[x]>deep[y]) swap(x,y);update(tid[x],tid[y],1,n,1,k);}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout);while(scanf("%d%d%d", &n, &m, &q) != EOF){char s;int i,x,y,z;init();for(i=1;i<=n;i++)scanf("%d",&a[i]);for(i=1;i<=m;i++){scanf("%d%d",&x,&y);tu[x].push_back(y);tu[y].push_back(x);}dfs1(1,0,1);dfs2(1,1);build(1,n,1);while(q--){scanf("%s",&s);if(s=='Q'){scanf("%d",&x);printf("%d\n",query(1,n,1,tid[x]));}else if(s=='D'){scanf("%d%d%d",&x,&y,&z);change(x,y,-z);}else{scanf("%d%d%d",&x,&y,&z);change(x,y,z);}}}return 0;}