Aragorn's Story

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


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
 
基于点权 单点查询 修改路径上的点权 模板
  1 #include<cstdio>
  2 #include<cstring>
  3 #include<iostream>
  4 #include<algorithm>
  5 #include<map>
  6 using namespace std;
  7 #define ll long long
  8 #define mod 998244353
  9 const int N=50010;
 10 const int INF=0x3f3f3f3f;
 11 struct Edge
 12 {
 13     int to,next;
 14 } edge[2*N];
 15 int head[N];
 16 int top[N];
 17 int fa[N];
 18 int deep[N];
 19 int num[N];
 20 int p[N];
 21 int fp[N];
 22 int son[N];
 23 int pos;
 24 int tot;
 25 void init()
 26 {
 27     tot=0;
 28     memset(head,-1,sizeof(head));
 29     pos=1;
 30     memset(son,-1,sizeof(son));
 31 }
 32 void addedge(int u,int v)
 33 {
 34     edge[tot].to=v;
 35     edge[tot].next=head[u];
 36     head[u]=tot++;
 37 }
 38 void dfs1(int u,int pre,int d)
 39 {
 40     deep[u]=d;
 41     fa[u]=pre;
 42     num[u]=1;
 43     for(int i=head[u]; i!=-1; i=edge[i].next)
 44     {
 45         int v=edge[i].to;
 46         if(v!=pre)
 47         {
 48             dfs1(v,u,d+1);
 49             num[u]+=num[v];
 50             if(son[u]==-1||num[v]>num[son[u]])
 51                 son[u]=v;
 52         }
 53     }
 54 }
 55 void getpos(int u,int sp)
 56 {
 57     top[u]=sp;
 58     p[u]=pos++;
 59     fp[p[u]]=u;
 60     if(son[u]==-1) return ;
 61     getpos(son[u],sp);
 62     for(int i=head[u]; i!=-1; i=edge[i].next)
 63     {
 64         int v=edge[i].to;
 65         if(v!=son[u]&&v!=fa[u])
 66             getpos(v,v);
 67     }
 68 }
 69 
 70 int lowbit(int x)
 71 {
 72     return x&(-x);
 73 }
 74 int c[N];
 75 int n;
 76 int sum(int i)
 77 {
 78     int s=0;
 79     while(i>0)
 80     {
 81         s+=c[i];
 82         i-=lowbit(i);
 83     }
 84     return s;
 85 }
 86 void add(int i,int val)
 87 {
 88     while(i<=n)
 89     {
 90         c[i]+=val;
 91         i+=lowbit(i);
 92     }
 93 }
 94 void change(int u,int v,int val)
 95 {
 96     int f1=top[u],f2=top[v];
 97     int tmp=0;
 98     while(f1!=f2)
 99     {
100         if(deep[f1]<deep[f2])
101         {
102             swap(f1,f2);
103             swap(u,v);
104         }
105         add(p[f1],val);
106         add(p[u]+1,-val);
107         u=fa[f1];
108         f1=top[u];
109     }
110     if(deep[u]>deep[v]) swap(u,v);
111     add(p[u],val);
112     add(p[v]+1,-val);
113 }
114 int a[N];
115 int main()
116 {
117     int M,P;
118     while(scanf("%d %d %d",&n,&M,&P)!=EOF)
119     {
120         int u,v;
121         int C1,C2,K;
122         char op[10];
123         init();
124         for(int i=1; i<=n; i++)
125             scanf("%d",&a[i]);
126         while(M--)
127         {
128             scanf("%d %d",&u,&v);
129             addedge(u,v);
130             addedge(v,u);
131         }
132 
133         dfs1(1,0,0);
134         getpos(1,1);
135         memset(c,0,sizeof(c));
136         for(int i=1; i<=n; i++)
137         {
138             add(p[i],a[i]);
139             add(p[i]+1,-a[i]);
140         }
141 
142         while(P--)
143         {
144             scanf("%s",op);
145             if(op[0]=='Q')
146             {
147                 scanf("%d",&u);
148                 printf("%d\n",sum(p[u]));
149             }
150             else
151             {
152                 scanf("%d%d%d",&C1,&C2,&K);
153                 if(op[0]=='D')
154                     K=-K;
155                 change(C1,C2,K);
156             }
157         }
158     }
159     return 0;
160 }