hdoj 3966 Aragorn's Story 【树链剖分】
来源:互联网 发布:ok软件 编辑:程序博客网 时间:2024/06/05 17:25
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7271 Accepted Submission(s): 1909
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.
题意:给定一棵树以及每个节点的权值,有q次查询。
I C1 C2 k表示C1-C2路径上所有节点的权值加k,D C1 C2 k表示C2-C2路径上所有节点权值减k。
Q C1 查询节点C1的权值。
思路:在链上跑线段树区间更新就可以了。注意需要把点i当做pos[i]位置的边,查询时查询第pos[]条边的权值,当两个点在一条链上时更新不能从节点的重儿子更新,需要提前一位把边界节点也算上。
AC代码:
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <set>#include <vector>#include <string>#define INF 0x3f3f3f3f#define eps 1e-8#define MAXN (50000+10)#define MAXM (300000+10)#define Ri(a) scanf("%d", &a)#define Rl(a) scanf("%lld", &a)#define Rf(a) scanf("%lf", &a)#define Rs(a) scanf("%s", a)#define Pi(a) printf("%d\n", (a))#define Pf(a) printf("%.2lf\n", (a))#define Pl(a) printf("%lld\n", (a))#define Ps(a) printf("%s\n", (a))#define W(a) while((a)--)#define CLR(a, b) memset(a, (b), sizeof(a))#define MOD 1000000007#define LL long long#define lson o<<1, l, mid#define rson o<<1|1, mid+1, r#define ll o<<1#define rr o<<1|1#define PI acos(-1.0)#pragma comment(linker, "/STACK:102400000,102400000")#define fi first#define se secondusing namespace std;struct Tree{ int l, r, len, val, lazy;};Tree tree[MAXN<<2];void PushUp(int o){ tree[o].val = tree[ll].val + tree[rr].val;}void PushDown(int o){ if(tree[o].lazy) { tree[ll].lazy += tree[o].lazy, tree[rr].lazy += tree[o].lazy; tree[ll].val += tree[o].lazy * tree[ll].len; tree[rr].val += tree[o].lazy * tree[rr].len; tree[o].lazy = 0; }}void Build(int o, int l, int r){ tree[o].l = l; tree[o].r = r; tree[o].len = r - l + 1; tree[o].val = 0; tree[o].lazy = 0; if(l == r) return ; int mid = (l + r) >> 1; Build(lson); Build(rson);}void Update(int o, int L, int R, int v){ if(tree[o].l == L && R == tree[o].r) { tree[o].lazy += v; tree[o].val += tree[o].len * v; return ; } PushDown(o); int mid = (tree[o].l + tree[o].r) >> 1; if(R <= mid) Update(ll, L, R, v); else if(L > mid) Update(rr, L, R, v); else {Update(ll, L, mid, v), Update(rr, mid+1, R, v);} PushUp(o);}int Query(int o, int pos){ if(tree[o].l == tree[o].r) return tree[o].val; PushDown(o); int mid = (tree[o].l + tree[o].r) >> 1; if(pos <= mid) return Query(ll, pos); else return Query(rr, pos);}struct Edge{ int from, to, next;};Edge edge[MAXN<<1];int head[MAXN], edgenum;int s[MAXN], e[MAXN], c[MAXN];void init(){ edgenum = 0; CLR(head, -1);}void addEdge(int u, int v){ Edge E = {u, v, head[u]}; edge[edgenum] = E; head[u] = edgenum++;}int son[MAXN], num[MAXN];int top[MAXN], pos[MAXN], id;int dep[MAXN], pre[MAXN];void DFS1(int u, int fa, int d){ dep[u] = d; pre[u] = fa; num[u] = 1; son[u] = -1; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == fa) continue; DFS1(v, u, d+1); num[u] += num[v]; if(son[u] == -1 || num[son[u]] < num[v]) son[u] = v; }}void DFS2(int u, int T){ top[u] = T; pos[u] = ++id; if(son[u] == -1) return ; DFS2(son[u], T); for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == pre[u] || v == son[u]) continue; DFS2(v, v); }}void Change(int u, int v, int z){ int f1 = top[u], f2 = top[v]; while(f1 != f2) { if(dep[f1] < dep[f2]) { swap(u, v); swap(f1, f2); } Update(1, pos[f1], pos[u], z); u = pre[f1], f1 = top[u]; } if(dep[u] > dep[v]) swap(u, v); Update(1, pos[u], pos[v], z);}int a[MAXN];int main(){ int n, m, q; while(scanf("%d%d%d", &n, &m, &q) != EOF) { for(int i = 1; i <= n; i++) Ri(a[i]); init(); W(m) { int x, y; Ri(x); Ri(y); addEdge(x, y); addEdge(y, x); } DFS1(1, 0, 1); id = 0; DFS2(1, 1); Build(1, 1, id); for(int i = 1; i <= n; i++) Update(1, pos[i], pos[i], a[i]); W(q) { char op[3]; Rs(op); int x, y, z; if(op[0] == 'Q') { Ri(x); Pi(Query(1, pos[x])); } else { Ri(x); Ri(y); Ri(z); if(op[0] == 'D') z = -z; Change(x, y, z); } } } 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(树链剖分)
- 工具:查看函数图象/SVG编辑/JSON->JAVA/Scalpel手机端三维视图/RxJava
- 基于图片驱动的C/S架构自动化测试技术 - Sikuli
- Maven管理Oracle JDBC驱动
- 固定表头可以纵向滚动的html表格
- DownloadHttpClient
- hdoj 3966 Aragorn's Story 【树链剖分】
- scala学习笔记(基础知识)
- Android之服务(三)服务的生命周期
- VI的操作方式
- DevStack安装时报“download of get-pip.py failed”
- HDU 3533 —— Escape
- 有道云协作支持Markdown了,云笔记也快了吧,哈哈
- jQuery 实现模块的复制和删除
- 从软件工程的角度写机器学习1——机器学习的思想