hdu 3966 Aragorn's Story 树链剖分
2014-08-08 15:27
435 查看
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2251 Accepted Submission(s): 614
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 5 1 2 3 2 1 2 3 I 1 3 5 Q 2 D 1 2 2 Q 1 Q 3
题意:给一棵树,树上的节点有权值,
给三种操作I x y k 在x到y的路径上每个点增加k
d x y k 在x到y的路径上每个节点减少k
q x 查询x节点的值
很明显的树链剖分,
树链剖分的内容查看:http://blog.sina.com.cn/s/blog_7a1746820100wp67.html
#pragma comment(linker,"/STACK:100000000,100000000") #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<vector> using namespace std; #define maxn 70007 #define lc u<<1 #define rc u<<1|1 #define ll long long struct Node{ int l,r,lz; }; Node node[maxn<<2]; struct Edge{ int v,next; }; Edge edge[maxn*4]; int cnt,head[maxn]; void add_edge(int u,int v){ edge[cnt].v = v, edge[cnt].next=head[u]; head[u] = cnt++; edge[cnt].v = u, edge[cnt].next=head[v]; head[v] = cnt++; } int num[maxn],res[maxn],flag,check[maxn]; int fa[maxn],son[maxn],id[maxn],height[maxn],top[maxn]; void dfs1(int u,int f){ if(check[u]) return ; check[u] =1; height[u] = height[f]+1; fa[u] = f; for(int i = head[u];i != -1; i=edge[i].next){ if(check[edge[i].v]!=0)continue; dfs1(edge[i].v,u); if(height[edge[i].v] > height[son[u]]) son[u]=edge[i].v; } } void dfs2(int u,int t){ if(check[u]) return ; check[u] = 1; top[u] = t; id[u] = flag++; res[id[u]]=num[u]; if(son[u] != 0) dfs2(son[u],t); for(int i=head[u];i!=-1;i=edge[i].next){ dfs2(edge[i].v,edge[i].v); } } void build(int u,int l,int r){ node[u].l = l,node[u].r = r; node[u].lz = 0; if(l == r) { node[u].lz = res[l]; return ; } int mid=(l+r)/2; build(lc,l,mid); build(rc,mid+1,r); } void push(int u){ node[lc].lz+=node[u].lz; node[rc].lz+=node[u].lz; node[u].lz=0; } void add(int u,int l,int r,int d){ if(node[u].l == l && node[u].r == r){ node[u].lz+=d; return ; } int mid = (node[u].l+node[u].r)/2; push(u); if(mid < l) add(rc,l,r,d); else if(mid>=r)add(lc,l,r,d); else add(lc,l,mid,d),add(rc,mid+1,r,d); } int getsum(int u,int p){ if(node[u].l == p && node[u].r==p) return node[u].lz; push(u); int mid=(node[u].l+node[u].r)/2; if(mid<p) return getsum(rc,p); else return getsum(lc,p); } int main(){ int n,m,p,v,u,l,r,k,f1,f2;; char x[10]; while(scanf("%d%d%d",&n,&m,&p)!=EOF){ memset(head,-1,sizeof(head)); cnt=0; for(int i = 1;i <= n; i++) scanf("%d",&num[i]); for(int i = 0;i < m; i++){ scanf("%d%d",&u,&v); add_edge(u,v); } memset(height,0,sizeof(height)); memset(son,0,sizeof(son)); memset(check,0,sizeof(check)); int root = (1+n)/2; dfs1(root,root); flag = 1; memset(check,0,sizeof(check)); dfs2(root,root); build(1,1,flag-1); while(p--){ scanf("%s",x); if(x[0]=='Q'){ scanf("%d",&l); printf("%d\n",getsum(1,id[l])); } else { scanf("%d%d%d",&l,&r,&k); if(x[0]=='D') k=-k; f1=top[l],f2=top[r]; while(f1!=f2){ if(height[f1]<height[f2]){ add(1,id[f2],id[r],k); r=fa[f2]; } else { add(1,id[f1],id[l],k); l=fa[f1]; } f1=top[l],f2=top[r]; } u=id[l],v=id[r]; if(u > v) swap(u,v); add(1,u,v,k); } } } return 0; }
相关文章推荐
- HDU 3966 Aragorn's Story(树链剖分)
- Hdu 3966 Aragorn's Story (树链剖分)
- hdu 3966 Aragorn's Story(树链剖分)
- HDU 3966 Aragorn's Story(树链剖分)
- [HDU 3966] Aragorn's Story 树链剖分
- HDU 3966 Aragorn's Story 树链剖分
- hdu 3966 Aragorn's Story 树链剖分
- HDU 3966 Aragorn's Story(树链剖分)
- hdu 3966 Aragorn's Story(树链剖分+树状数组)
- HDU 3966 Aragorn&#39;s Story --树链剖分
- HDU 3966 Aragorn's Story 树链剖分模板
- HDU 3966 Aragorn's Story(树链剖分+线段树区间更新+手动扩大内存)
- HDU 3966 Aragorn's Story(树链剖分 (基于点权,查询单点值,修改路径的上的点权)模板题)
- hdu 3966 Aragorn's Story 树链剖分
- hdu-3966 Aragorn's Story(树链剖分,点权)
- HDU 3966 Aragorn's Story 树链剖分
- 【HDU】3966 Aragorn's Story 树链剖分
- HDU Aragorn's Story -树链剖分
- Aragorn's Story - HDU 3966 树链刨分
- 【HDU】3966 Aragorn's Story(树链剖分+线段树)