hdu3966 Aragorn ' s story

Source: Internet
Author: User

Time limit:10000/3000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4217 Accepted Submission (s): 1164


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

Test instructions: Give a tree, and given the value of each point, then there are 3 operations:

I C1 C2 k: Add a bit of weight to the path of C1 and C2 with K

D C1 C2 k: Subtract K from a bit of weight on the path of C1 and C2

Q C: The weighted value of the query node number C


Be bold to submit it in C + +!

#include <map> #include <string> #include <cstring> #include <cstdio> #include <cstdlib># include<cmath> #include <queue> #include <vector> #include <iostream> #include <algorithm > #include <bitset> #include <climits> #include <list> #include <iomanip> #include <stack > #include <set>using namespace std;int head[50010],tail;struct edge{int to,next;} edge[50000*2+10];void Add (int from,int to) {edge[tail].to=to;edge[tail].next=head[from];head[from]=tail++;} int siz[50010],dep[50010],fa[50010],son[50010];void dfs1 (int from) {siz[from]=1;for (int i=head[from];i!=-1;i=edge[i ].next) {int to=edge[i].to;if (To!=fa[from]) {FA[TO]=FROM;DEP[TO]=DEP[FROM]+1;DFS1 (to); Siz[from]+=siz[to];if (siz[ To]>siz[son[from]]) son[from]=to;}}} int w[50010],top[50010],cnt;void DFS2 (int from,int tp) {w[from]=++cnt;top[from]=tp;if (son[from]==0) RETURN;DFS2 (son[ FROM],TP); for (int i=head[from];i!=-1;i=edge[i].next) {int to=edge[i].to;if (to!=son[from]&&to!=fA[from]) DFS2 (to,to);}} struct Tree{int l,r,m,v;} tree[50000*4+10];void Build (int l,int r,int k) {tree[k].l=l;tree[k].r=r;tree[k].m=l+r>>1;tree[k].v=0;if (l==r) Return;build (l,tree[k].m,k<<1); build (tree[k].m+1,r,k<<1|1);} void update (int l,int r,int val,int k) {if (L==TREE[K].L&AMP;&AMP;R==TREE[K].R) {Tree[k].v+=val;return;} if (tree[k].v!=0) {tree[k<<1].v+=tree[k].v;tree[k<<1|1].v+=tree[k].v;tree[k].v=0;} if (R&LT;=TREE[K].M) update (l,r,val,k<<1); else if (L&GT;TREE[K].M) update (l,r,val,k<<1|1); Else{update (l , tree[k].m,val,k<<1); update (tree[k].m+1,r,val,k<<1|1);}} int seek (int x,int k) {if (tree[k].l==tree[k].r&&tree[k].l==x) return tree[k].v;if (tree[k].v!=0) {tree[k< <1].v+=tree[k].v;tree[k<<1|1].v+=tree[k].v;tree[k].v=0;} if (X&LT;=TREE[K].M) return Seek (x,k<<1); return Seek (x,k<<1|1);} void update (int x,int y,int val) {while (Top[x]!=top[y]) {if (Dep[top[x]]<dep[top[y]) swap (x, y); Update (w[top[x]],w[ x],val,1); x=fa[top[x]];} int l=W[x],r=w[y];if (l>r) swap (L,R); update (l,r,val,1);} int Num[50010];char Od[2];int Main () {int n,m,p;while (scanf ("%d%d%d", &n,&m,&p)!=eof) {for (int i=1;i<=n ; i++) scanf ("%d", Num+i), Tail=0;memset (Head,-1,sizeof (head)), while (m--) {int from,to;scanf ("%d%d",&from,& To); Add (from,to); add (To,from);} memset (son,0,sizeof (son));d FS1 (1); CNT=0;DFS2 (n); build (1,cnt,1); for (int i=1;i<=n;i++) update (i,i,num[i]); while (p--) {scanf ("%s", OD), if (od[0]== ' Q ') {int x;scanf ("%d", &x);p rintf ("%d\n", Seek (w[x],1));} Else{int l,r,v;scanf ("%d%d%d", &l,&r,&v); if (od[0]== ' d ') v=-v;update (L,R,V);}}}


hdu3966 Aragorn ' s story

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.