A-aragorn ' s StoryTime
limit:3000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 3966
Description
Our protagonist are 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 have N camps out of his kingdom and M edges connect them. It is the guaranteed that for any of the 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 would increase or decrease the number of soldiers in camps. Every time the enemy change the number of soldiers, they'll set of both camps C1 and C2. Then, for C1, C2 and all camps on the path from C1 to C2, they would 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 would 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 and camp-i has Ai enemies.
The next M lines contains the integers u and V for each, denotes that there are an edge connects Camp-u and Camp-v.
The next P lines would 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 are a query and means Aragorn wants to know the number of enemies in Camp C at that T Ime.
Output
For each query, you need to output the actually number of enemies in the specified camp.
Sample Input
3 2 2 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
Sample Output
748
Hint
#pragma COMMENT (linker, "/stack:1024000000,1024000000") #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <algorithm> #include <string># include<queue> #include <stack> #include <vector>using namespace std; #define N 50010#define Lson L,mid , Rt<<1#define Rson mid+1,r,rt<<1|1int sum[n<<2],cal[n<<2];vector<int> e[N];int n,m,p; int Tim;char s[5];int num[n],siz[n],top[n],son[n];int deep[n],tid[n],rnk[n],fa[n];void init () {memset (son,-1,sizeof ( son)); tim=0; for (int i=0;i<=n;i++) e[i].clear ();} void dfs1 (int u,int father,int d) {deep[u]=d; Fa[u]=father; Siz[u]=1; for (int i=0;i<e[u].size (); i++) {int v=e[u][i]; if (v!=father) {DFS1 (v,u,d+1); SIZ[U]+=SIZ[V]; if (son[u]==-1| | Siz[v]>siz[son[u]]) son[u]=v; }}}void dfs2 (int u,int tp) {TOP[U]=TP; Tid[u]=++tim; Rnk[tid[u]]=u; if (son[u]==-1) return; DFS2 (SON[U],TP); for (int i=0;i<e[u].size (); i++) {int v=e[u][i]; if (V!=son[u]&&v!=fa[u]) {DFS2 (v,v); }}}void pushdown (int rt,int m) {if (Cal[rt]) {cal[rt<<1]+=cal[rt]; cal[rt<<1|1]+=cal[rt]; sum[rt<<1]+= (M (m>>1)) *CAL[RT]; sum[rt<<1|1]+= (m>>1) *cal[rt]; cal[rt]=0; }}void Build (int l,int R,int RT) {cal[rt]=0; if (l==r) {sum[rt]=num[rnk[l]]; return; } int mid= (L+R) >>1; Build (Lson); Build (Rson);} void update (int l,int r,int c,int l,int r,int RT) {if (l<=l&&r<=r) {cal[rt]+=c; sum[rt]+= (r-l+1) *c; return; } pushdown (rt,r-l+1); int mid= (L+R) >>1; if (l<=mid) update (L,r,c,lson); if (r>mid) update (L,r,c,rson);} int query (int pos,int l,int R,int rt) {if (l==r) {return SUM[RT]; } PushdowN (rt,r-l+1); int mid= (L+R) >>1,ret=0; if (pos<=mid) ret=query (Pos,lson); else Ret=query (Pos,rson); return ret;} void Change (int. x,int Y,int val) {while (Top[x]!=top[y]) {if (Deep[top[x]]<deep[top[y]) swap (x, y); Update (tid[top[x]],tid[x],val,1,n,1); X=FA[TOP[X]]; } if (Deep[x]>deep[y]) swap (x, y); Update (tid[x],tid[y],val,1,n,1);} int main () {int a,b,c; while (scanf ("%d%d%d", &n,&m,&p)!=eof) {init (); for (int i=1;i<=n;i++) scanf ("%d", &num[i]); for (int i=1;i<=m;i++) {scanf ("%d%d", &a,&b); E[a].push_back (b); E[b].push_back (a); } DFS1 (1,0,1); DFS2 (a); Build (1,n,1); while (p--) {scanf ("%s", s); if (s[0]== ' Q ') {scanf ("%d", &a); printf ("%d\n", Query (tid[a],1,n,1)); } else {SCANF ("%d%d%d", &a,&b,&c); if (s[0]== ' D ') c=-c; Change (A,B,C); }}} return 0;}
(First tree chain split!!!!!) ) Hdu 3966