Aragorn’s Story

Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 15842 Accepted Submission(s): 4159


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.


树链剖分点权的模板题:

#include <bits/stdc++.h>

using namespace std;
/*
* 基于点权,查询单点值,修改路径的上的点权
*/
const int MAXN = 50010;
struct Edge
{
int to, next;
} edge[MAXN * 2]; int head[MAXN], tot;
int top[MAXN]; // top[v]表示v所在的重链的顶端节点
int fa[MAXN]; // 父亲节点
int deep[MAXN]; // 深度
int num[MAXN]; // num[v]表示以v为根的子树的节点数
int p[MAXN]; // p[v]表示v对应的位置
int fp[MAXN]; // fp和p数组相反
int son[MAXN]; // 重儿子
int pos; void init()
{
tot = 0;
memset(head, -1, sizeof(head));
pos = 1; // 使用树状数组,编号从头1开始
memset(son, -1, sizeof(son));
return ;
} void addedge(int u, int v)
{
edge[tot].to = v;
edge[tot].next = head[u];
head[u] = tot++;
return ;
} void dfs1(int u, int pre, int d)
{
deep[u] = d;
fa[u] = pre;
num[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (v != pre)
{
dfs1(v, u, d + 1);
num[u] += num[v];
if (son[u] == -1 || num[v] > num[son[u]])
{
son[u] = v;
}
}
}
return ;
} void getpos(int u, int sp)
{
top[u] = sp;
p[u] = pos++;
fp[p[u]] = u;
if (son[u] == -1)
{
return ;
}
getpos(son[u], sp);
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (v != son[u] && v != fa[u])
{
getpos(v, v);
}
}
return ;
} //树状数组
int lowbit(int x)
{
return x & (-x);
} int c[MAXN];
int n; int sum(int i)
{
int s = 0;
while (i > 0)
{
s += c[i];
i -= lowbit(i);
}
return s;
} void add(int i, int val)
{
while (i <= n)
{
c[i] += val;
i += lowbit(i);
}
return ;
} void Change(int u, int v, int val) // u->v的路径上点的值改变val
{
int f1 = top[u], f2 = top[v];
while (f1 != f2)
{
if (deep[f1] < deep[f2])
{
swap(f1, f2);
swap(u, v);
}
add(p[f1], val);
add(p[u] + 1, -val);
u = fa[f1];
f1 = top[u];
}
if (deep[u] > deep[v])
{
swap(u, v);
}
add(p[u], val);
add(p[v] + 1, -val);
return ;
} int a[MAXN]; int main()
{
int M, P;
while (scanf("%d%d%d", &n, &M, &P) == 3)
{
int u, v;
int C1, C2, K;
char op[10];
init();
for (int i = 1; i <= n; i++)
{
scanf("%d", &a[i]);
}
while(M--)
{
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
dfs1(1, 0, 0);
getpos(1, 1);
memset(c, 0, sizeof(c));
for (int i = 1; i <= n; i++)
{
add(p[i],a[i]);
add(p[i] + 1, -a[i]);
}
while (P--)
{
scanf("%s", op);
if (op[0] == 'Q')
{
scanf("%d", &u);
printf("%d\n", sum(p[u]));
}
else
{
scanf("%d%d%d", &C1, &C2, &K);
if (op[0] == 'D')
{
K = -K;
}
Change(C1, C2, K);
}
}
}
return 0;
}

最新文章

  1. maven 的使用
  2. java 常见关键字的使用
  3. 【wikioi】1269 匈牙利游戏(次短路+spfa)
  4. Gold Coins 分类: POJ 2015-06-10 15:04 16人阅读 评论(0) 收藏
  5. 8 Types Of Friends You Need To Have in Your Life
  6. cadence16.6 中orcad导出网表时ERROR (ORCAP-5004)(win7 旗舰版32位)
  7. 项目经历——EasyUI的检索和更新操作
  8. 50一个Android开发技巧(01 利用好layout_weight属性)
  9. C++_运算符重载
  10. 浅试 Webview 一app 加载 H5小游戏
  11. Hbase_02、Hbase的常用的shell命令&amp;Hbase的DDL操作&amp;Hbase的DML操作(转)
  12. &lt;记录&gt; PHP监控进程状态,完成掉线自动重启
  13. javascript实现异步编程的4种方法
  14. iOS 自己封装的网络请求,json解析的类
  15. idea ssm项目出现日志中文乱码,封装的json中的msg字段中文乱码(但是json封装的bean中的字段不乱码)等其他各种项目下的中文乱码解决方案
  16. Zookeeper原理架构与搭建
  17. 1024 Palindromic Number (25)(25 point(s))
  18. PHP获取机器mac代码
  19. pm2-web
  20. SQL Server基础知识三十三问 (1-7)

热门文章

  1. Error Code: 2006 - MySQL 鏈嶅姟鍣ㄥ凡绂荤嚎
  2. Python中的列表,元组,字符串之间的相互转化
  3. notepad++ 查找引用(Find Reference)(适用于c c++及各类脚本比方lua、python等)
  4. python3 base64模块代码分析
  5. Python开发【第2节】【Python运算符】
  6. MVC为Html对象建立一个扩展方法,使用自己的控件就像使用TextBox一样方便
  7. high-level operations on files and collections of files
  8. css class嵌套
  9. HTML表单常用标签
  10. Buildroot构建指南--快速上手与实用技巧【转】