HDU 3966 Aragorn's Story(树剖-点)
2015-10-07 21:42
381 查看
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
Sample Output
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 5 1 2 3 2 1 2 3 I 1 3 5 Q 2 D 1 2 2 Q 1 Q 3
Sample Output
7 4 8 Hint 1.The number of enemies may be negative. 2.Huge input, be careful.
#include<cstdio> #include<cstring> #include<algorithm> #include<vector> #include<string> #include<iostream> #include<queue> #include<cmath> #include<map> #include<stack> #include<set> using namespace std; #define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i ) #define REP( i , n ) for ( int i = 0 ; i < n ; ++ i ) #define CLEAR( a , x ) memset ( a , x , sizeof a ) const int INF=0x3f3f3f3f; typedef long long LL; const int maxn=5*(1e4+100); struct node{ int to,next; }e[maxn*2]; int head[maxn],tot; int top[maxn],fa[maxn],deep[maxn],num[maxn],p[maxn],fp[maxn],son[maxn]; int pos; void init() { tot=0;pos=1; CLEAR(head,-1); CLEAR(son,-1); } void addedge(int u,int v) { e[tot].to=v;e[tot].next=head[u]; head[u]=tot++; } void dfs1(int u,int pre,int d) { deep[u]=d;fa[u]=pre; num[u]=1; for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(v==pre) continue; dfs1(v,u,d+1); num[u]+=num[v]; if(son[u]==-1||num[v]>num[son[u]]) son[u]=v; } } void dfs2(int u,int sp) { top[u]=sp;p[u]=pos++; fp[p[u]]=u; if(son[u]==-1) return ; dfs2(son[u],sp); for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; if(v!=son[u]&&v!=fa[u]) dfs2(v,v); } } int n,m,q; int A[maxn],C[maxn]; int lowbit(int x) { return x&(-x); } void update(int x,int val) { while(x<maxn) { C[x]+=val; x+=lowbit(x); } } int query(int x) { int sum=0; while(x>0) { sum+=C[x]; x-=lowbit(x); } return sum; } void change(int u,int v,int val) { int f1=top[u],f2=top[v]; int temp=0; while(f1!=f2) { if(deep[f1]<deep[f2]) { swap(f1,f2); swap(u,v); } update(p[f1],val); update(p[u]+1,-val); u=fa[f1];f1=top[u]; } if(deep[u]>deep[v]) swap(u,v); update(p[u],val); update(p[v]+1,-val); } int main() { int u,v,w;char op[2]; while(~scanf("%d%d%d",&n,&m,&q)) { init(); for(int i=1;i<=n;i++) scanf("%d",&A[i]); while(m--) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } dfs1(1,0,0); dfs2(1,1); CLEAR(C,0); for(int i=1;i<=n;i++) { update(p[i],A[i]); update(p[i]+1,-A[i]); } while(q--) { scanf("%s",op); if(op[0]=='Q') { scanf("%d",&u); printf("%d\n",query(p[u])); } else { scanf("%d%d%d",&u,&v,&w); if(op[0]=='D') w=-w; change(u,v,w); } } } return 0; }
相关文章推荐
- Spectral Algorithm
- UVA11971 - Polygon
- 51nod 1051 最大子矩阵和 (dp_good)
- HDU_3395_Special Fish(最大费用最大流)
- c++、c#、Python、GO语言基本语法比较
- HDU_2448_Mining Station on the Sea(最短路 + 最小费用流)
- GO 学习笔记(四)struct 结构、slice
- 系统的了解DJANGO中数据MODULES的相关性引用
- pd的django To do list教程-----(2)models模型的建立
- pd的django To do list 教程------(1)说明与展示
- django-pagination分页
- go语言 类型:字符串
- go语言 类型:整型
- go语言 类型:布尔类型
- LeetCode Algorithms #258 <Add Digits>
- django 中的ajax
- CF_#322(Div.2) D. Three Logos(greedy)
- 10.6上课——problem1切割木板(USACO 2006 November Gold)
- EGOTableViewPullRefresh 使用
- ubuntu14.04下安装indigo版ROS