Aragorn‘s Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2526 Accepted Submission(s): 709
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
Sample Output
7 4 8 Hint 1.The number of enemies may be negative. 2.Huge input, be careful.
Source
2011 Multi-University Training Contest 13 - Host by HIT
Recommend
We have carefully selected several similar problems for you: 3964 3965 3962 3963 3967
题意:
给你一棵树。你可以对树进行三种操作。
1.给树上的路径u->v上所有点加上一个值。
2.。。。。。。。。。。。。减去一个值。
3.询问树上某个节点v的值。
思路:
裸的树链剖分。
详细见代码:
#include<algorithm> #include<iostream> #include<string.h> #include<stdio.h> using namespace std; const int INF=0x3f3f3f3f; const int maxn=50010; typedef long long ll; #define lson L,mid,ls #define rson mid+1,R,rs int sz[maxn],dep[maxn],fa[maxn],son[maxn],id[maxn],top[maxn]; int add[maxn<<2],val[maxn],arr[maxn],cnt,ptr; struct node { int v; node *next; } ed[maxn<<1],*head[maxn]; void adde(int u,int v) { ed[cnt].v=v; ed[cnt].next=head[u]; head[u]=&ed[cnt++]; } void build(int L,int R,int rt) { add[rt]=0; if(L==R) { add[rt]=val[L]; return; } int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; build(lson); build(rson); } void update(int L,int R,int rt,int l,int r,int d) { if(l<=L&&R<=r) { add[rt]+=d; return; } int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; if(l<=mid) update(lson,l,r,d); if(r>mid) update(rson,l,r,d); } int qu(int L,int R,int rt,int p) { if(L==R) return add[rt]; int ls=rt<<1,rs=ls|1,mid=(L+R)>>1; if(p<=mid) return qu(lson,p)+add[rt]; else return qu(rson,p)+add[rt]; } void dfs(int u) { int v,ms=0; sz[u]=1,son[u]=-1; for(node *p=head[u];p!=NULL;p=p->next) { v=p->v; if(v==fa[u]) continue; dep[v]=dep[u]+1; fa[v]=u; dfs(v); if(sz[v]>ms) ms=sz[v],son[u]=v; sz[u]+=sz[v]; } } void getid(int u,int ft) { id[u]=++ptr,top[u]=ft; val[ptr]=arr[u]; if(son[u]!=-1) getid(son[u],top[u]); for(node *p=head[u];p!=NULL;p=p->next) { if(p->v==son[u]||p->v==fa[u]) continue; getid(p->v,p->v); } } void init(int rt) { fa[rt]=-1; dep[rt]=ptr=0; dfs(rt); getid(rt,rt); } void uppath(int u,int v,int d) { int f1=top[u],f2=top[v]; while(f1!=f2) { if(dep[f1]<dep[f2]) swap(f1,f2),swap(u,v); update(1,ptr,1,id[f1],id[u],d); u=fa[f1],f1=top[u]; } if(dep[u]>dep[v]) swap(u,v); update(1,ptr,1,id[u],id[v],d); } int main() { int n,m,p,i,u,v,w,rt; char cmd[100]; while(~scanf("%d%d%d",&n,&m,&p)) { for(i=1;i<=n;i++) scanf("%d",&arr[i]); cnt=0; memset(head,0,sizeof head); for(i=1;i<n;i++) { scanf("%d%d",&u,&v); adde(u,v); adde(v,u); } rt=(n+1)/2; init(rt); build(1,ptr,1); while(p--) { scanf("%s%d",cmd,&u); if(cmd[0]=='Q') printf("%d\n",qu(1,ptr,1,id[u])); else { scanf("%d%d",&v,&w); if(cmd[0]=='D') w=-w; uppath(u,v,w); } } } return 0; }
hdu 3966 Aragorn's Story(树链剖分对点编号)