HDOJ 3966 Aragorn's Story
来源:互联网 发布:php类中定义一个结构体 编辑:程序博客网 时间:2024/06/18 17:16
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4698 Accepted Submission(s): 1272
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.
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 51 2 32 12 3I 1 3 5Q 2D 1 2 2Q 1 Q 3
Sample Output
748Hint1.The number of enemies may be negative.2.Huge input, be careful.
Source
2011 Multi-University Training Contest 13 - Host by HIT
#include <cstdio>#include <algorithm>#include <cstring>#include <vector>using namespace std;const int N = 50010;struct node{ int v, next;};int tree[N], V[N], head[N];int dep[N], size[N], son[N], pre[N];int lct[N], top[N];vector<node> E;int n, pos;int lowbit(int x) { return x & (-x);}void update(int i, int x) { while (i) { tree[i] += x; i -= lowbit(i); }}int query(int i) { int ans = 0; while (i <= n) { ans += tree[i]; i += lowbit(i); } return ans;}void add_edge(int u, int v) { node tmp; tmp.v = v; tmp.next = head[u]; head[u] = E.size(); E.push_back(tmp);}void Init() { memset(head, -1, sizeof(head)); memset(tree, 0, sizeof(tree)); E.clear(); tree[0] = 0; lct[0] = 0; dep[1] = 1; pre[1] = 1; size[0] = 0; pos = 0;}void dfs(int u) { size[u] = 1; son[u] = 0; for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].v; if (v != pre[u]) { pre[v] = u; dep[v] = dep[u] + 1; dfs(v); size[u] += size[v]; if (size[son[u]] < size[v]) son[u] = v; } }}void build_tree(int u, int tp) { lct[u] = ++pos; top[u] = tp; if (son[u]) build_tree(son[u], tp); for (int i = head[u]; i != -1; i = E[i].next) { int v = E[i].v; if (v != son[u] && v != pre[u]) build_tree(v, v); }}void modify(int a, int b, int c) { int p1 = top[a], p2 = top[b]; while (p1 != p2) { if (dep[p1] > dep[p2]) { swap(a, b); swap(p1, p2); } update(lct[b], c); update(lct[p2] - 1, -c); b = pre[p2]; p2 = top[b]; } if (dep[a] > dep[b]) swap(a, b); update(lct[b], c); update(lct[a] - 1, -c);}int main() { int m, p; while (~scanf("%d%d%d", &n, &m, &p)) { Init(); for (int i = 1; i <= n; ++i) scanf("%d", V + i); int a, b, c; for (int i = 1; i < n; ++i) { scanf("%d%d", &a, &b); add_edge(a, b); add_edge(b, a); } dfs(1); build_tree(1, 1); for (int i = 1; i <= n; ++i) { update(lct[i], V[i]); update(lct[i] - 1, -V[i]); } char str[5]; while (p--){ scanf("%s", str); if (str[0] == 'Q') { scanf("%d", &c); printf("%d\n", query(lct[c])); } else { scanf("%d%d%d", &a, &b, &c); if (str[0] == 'D') c = -c; modify(a, b, c); } } } return 0;}
0 0
- HDOJ 3966 Aragorn's Story
- HDOJ 3966 Aragorn's Story
- 【树链剖分】 HDOJ 3966 Aragorn's Story
- hdoj 3966 Aragorn's Story 【树链剖分】
- HDOJ 题目3966 Aragorn's Story(树链剖分,树状数组)
- hdoj 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's Story
- HDU 3966 Aragorn's Story 树链剖分模板
- hdu 3966 Aragorn's Story(树连剖分)
- hdu 3966 Aragorn's Story(树链剖分)
- 给iOS工程增加Daily Build
- setBackgroundDrawable和setBackgroundColor的用法
- MediaStore.Images.Thumbnails
- shmget报EEXIST错误
- 第三部分 高级套接字编程 ipv4与ipv6的互操作性, 守护进程编写出错,暂时未解决。
- HDOJ 3966 Aragorn's Story
- Hadoop之——Java操作HBase
- Wps 2013 拼音标注两种方式分析
- 背景图延迟加载(lazyload)技术
- Android ActionBar的使用
- 新得
- Android开发之Toast
- MyBatis插入数据后如何获得主键值
- iOS6和iOS7的导航栏区别