Aragorn ' s Story
Time limit:10000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 5505 Accepted Submission (s): 1441
Problem Descriptionour 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 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.
Inputmultiple 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.
Outputfor 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
748Hint
Source2011 multi-university Training Contest 13-host by hits
Recommendwe carefully selected several similar problems for you:3965 3962 3963 3967 3968
Instantaneous seconds a a~~
The main idea: a figure, each point has a bit of power, 3 kinds of operations, I a b,a to B point right plus a value, D is minus a value, Q is the point of query this point right
Most of the web is a tree chain to write, submitted the two found that the speed is not faster than mine, and really do not know the front 300 milliseconds of how to write
#include <stdio.h> #include <string.h> #include <queue> #include <iostream> #d Efine INF 0x7fffffff #define MAX (A, B) (A>B?A:B) using namespace std; int vis[50050]; struct LCT {int bef[50050],pre[50050],next[50050][2],key[50050],add[50050]; void Init () {memset (pre,0,sizeof (pre)); memset (Next,0,sizeof (next)); memset (key,0,sizeof (key)); Memset (add,0,sizeof (add)); } void Pushdown (int x) {if (add[x]) {int A, B; A=NEXT[X][0]; B=NEXT[X][1]; if (a)//do not add, in fact, with the bottom of the B<span id= "Transmark" ></span> {add[a]+=add[x]; KEY[A]+=ADD[X]; } if (b) {add[b]+=add[x]; KEY[B]+=ADD[X]; } add[x]=0; }} void rotate (int x,int kind) { int y,z; Y=PRE[X]; Z=pre[y]; Pushdown (y); Pushdown (x); Next[y][!kind]=next[x][kind]; Pre[next[x][kind]]=y; Next[z][next[z][1]==y]=x; Pre[x]=z; Next[x][kind]=y; Pre[y]=x; } void Splay (int x) {int rt; for (Rt=x;pre[rt];rt=pre[rt]); if (X!=RT) {BEF[X]=BEF[RT]; bef[rt]=0; Pushdown (x); while (Pre[x]) {if (next[pre[x]][0]==x) { Rotate (x,1); } else rotate (x,0); }}}} void access (int x) {int fa; for (Fa=0;x;x=bef[x]) { Splay (x); Pushdown (x); pre[next[x][1]]=0; Bef[next[x][1]]=x; NEXT[X][1]=FA; Pre[fa]=x; bef[fa]=0; Fa=x; }} void Change (int x,int y,int val) {access (Y); for (Y=0;x;x=bef[x]) {splay (x); if (!bef[x]) {key[x]+=val; Key[y]+=val; Key[next[x][1]]+=val; Add[next[x][1]]+=val; Add[y]+=val; Return } pushdown (x); pre[next[x][1]]=0; Bef[next[x][1]]=x; Next[x][1]=y; Pre[y]=x; bef[y]=0; Y=x; }} int query (int x) {splay (x); return key[x]; }}LCT; struct S {int u,v,next; }edge[100020<<1]; int head[100020],cnt; void Add (int u,int v) {edge[cnt].u=u; Edge[cnt].v=v; Edge[cnt].next=head[u]; head[u]=cnt++; } void BFs (int u) {queue<int>q; memset (vis,0,sizeof (VIS)); Vis[u]=1; Q.push (U); while (!q.empty ()) {U=q.front (); Q.pop (); for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v; if (!vis[v]) {lct.bef[v]=u; Vis[v]=1; Q.push (v); }}}} int main () {int n,m,q; while (scanf ("%d%d%d", &n,&m,&q)!=eof) { int i; Lct.init (); cnt=0; memset (head,-1,sizeof (head)); for (i=1;i<=n;i++) {scanf ("%d", &lct.key[i]); } for (i=0;i<m;i++) {int u,v; scanf ("%d%d", &u,&v); Add (U,V); Add (V,u); } BFS (1); while (q--) {char op[2]; scanf ("%s", op); if (op[0]== ' I ') {int x,y,val; scanf ("%d%d%d", &x,&y,&val); Lct.change (X,y,val); } else if (op[0]== ' D ') {int x,y,val; scanf ("%d%d%d", &x,&y,&val); Lct.change (X,y,-val); } else if (op[0]== ' Q ') {int x; scanf ("%d", &x); printf ("%d\n", Lct.query (x)); } } }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Hdoj topic 3966 Aragorn ' s story (Link cut tree into paragraph plus minus points right, query point right)