首页 > 代码库 > hdu3966 Aragorn's Story

hdu3966 Aragorn's Story

地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3966

题目:

Aragorn‘s Story

Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 11512    Accepted Submission(s): 3028


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

思路: 树链修改,单点查询。

  方法有以下两种

  1.树链剖分

    模板题,不解释;

  2.dfs+树状数组

    具体见我另外一篇文章的树链修改,单点查询问题。http://www.cnblogs.com/weeping/p/6847112.html

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define MP make_pair
 6 #define PB push_back
 7 typedef long long LL;
 8 typedef pair<int,int> PII;
 9 const double eps=1e-8;
10 const double pi=acos(-1.0);
11 const int K=5e4+7;
12 const int mod=1e9+7;
13 
14 vector<int>mp[K];
15 int top[K],sz[K],fa[K],son[K],id[K],hid[K],deep[K];
16 int cnt,add[4*K],a[K];
17 
18 int query(int o,int l,int r,int x)
19 {
20     if(l==r)    return add[o];
21     int mid=l+r>>1;
22     if(x<=mid) return query(o<<1,l,mid,x)+add[o];
23     return query(o<<1|1,mid+1,r,x)+add[o];
24 }
25 int update(int o,int l,int r,int nl,int nr,int v)
26 {
27     if(l==nl&&r==nr)
28        return add[o]+=v;
29     int mid=l+r>>1;
30     if(nr<=mid) update(o<<1,l,mid,nl,nr,v);
31     else if(nl>mid) update(o<<1|1,mid+1,r,nl,nr,v);
32     else update(o<<1,l,mid,nl,mid,v),update(o<<1|1,mid+1,r,mid+1,nr,v);
33 }
34 void dfs1(int x,int f)
35 {
36     sz[x]=1,fa[x]=f,son[x]=-1,deep[x]=deep[f]+1;
37     for(int i=0;i<mp[x].size();i++)
38     if(mp[x][i]!=f)
39     {
40         dfs1(mp[x][i],x);
41         sz[x]+=sz[mp[x][i]];
42         if(son[x]==-1||sz[son[x]]<sz[mp[x][i]])
43             son[x]=mp[x][i];
44     }
45 }
46 void dfs2(int x,int f)  ///每条边用深度低的节点的序号表示
47 {
48     top[x]=f,id[x]=++cnt,hid[id[x]]=x;
49     if(son[x]!=-1) dfs2(son[x],f);
50     for(int i=0;i<mp[x].size();i++)
51     if(mp[x][i]!=fa[x]&&mp[x][i]!=son[x])
52         dfs2(mp[x][i],mp[x][i]);
53 }
54 void tree_update(int x,int y,int v)
55 {
56     while(top[x]!=top[y])
57     {
58         if(deep[top[x]]<deep[top[y]]) swap(x,y);
59         update(1,1,cnt,id[top[x]],id[x],v);//更新x-top[x]-fa[top[x]]
60         x=fa[top[x]];//所以直接fa[top[x]]
61     }
62     //if(x==y) return;
63     if(deep[x]>deep[y]) swap(x,y);
64     update(1,1,cnt,id[x],id[y],v);///注意id[son[x]]
65 }
66 int tree_query(int x)
67 {
68     return query(1,1,cnt,id[x])+a[x];
69 }
70 int main(void)
71 {
72     //freopen("in.acm","r",stdin);
73     int n,m,q;
74     while(~scanf("%d%d%d",&n,&m,&q))
75     {
76         cnt=0;
77         memset(add,0,sizeof add);
78         for(int i=1;i<=n;i++)
79             scanf("%d",a+i),mp[i].clear();
80         for(int i=1,x,y;i<n;i++)
81             scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
82         dfs1(1,0),dfs2(1,1);
83         int x,y,v;
84         char op[10];
85         while(q--)
86         {
87             scanf("%s%d",op,&x);
88             if(op[0]==Q)
89                 printf("%d\n",tree_query(x));
90             else
91             {
92                 scanf("%d%d",&y,&v);
93                 if(op[0]==D)v=-v;
94                 tree_update(x,y,v);
95             }
96         }
97     }
98     return 0;
99 }

 

hdu3966 Aragorn's Story