【HDU 3966 】Aragorn's Story 【树链剖分 基于点权,查询单点值,修改路径上的点权】
来源:互联网 发布:竞价软件哪个好 编辑:程序博客网 时间:2024/06/05 07:06
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;#define LL long long#define fread() freopen("in.txt","r",stdin)#define fwrite() freopen("out.txt","w",stdout)#define CLOSE() ios_base::sync_with_stdio(false)const int MAXN = 50000+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;int n;int lowbit(int x) { return x&(-x); }int c[MAXN];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); }} // 以上的为树状数组的模板struct Edge { int from ,to,nexts;}edge[MAXN*2];int head[MAXN],topp;int son[MAXN];int pos;void init(){ memset(head,-1,sizeof(head)); topp=0; pos=1; memset(son,-1,sizeof(son));}void addedge(int a,int b){ Edge e={a,b,head[a]}; edge[topp]=e;head[a]=topp++;}int top[MAXN],fa[MAXN];int deep[MAXN],num[MAXN];int p[MAXN],fp[MAXN];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].nexts){ 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; } }}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].nexts){ int v=edge[i].to; if(v!=son[u]&&v!=fa[u]) getpos(v,v); }}void change(int u,int v,int val){ int f1=top[u],f2=top[v]; int temp=0; 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);}int a[MAXN];int main(){ CLOSE();// fread();// fwrite(); int M,P; while(scanf("%d%d%d",&n,&M,&P)!=EOF){ init(); for(int i=1;i<=n;i++) scanf("%d",&a[i]); while(M--){ int a,b; scanf("%d%d",&a,&b); addedge(a,b); addedge(b,a); } 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]); } char op[10]; while(P--){ scanf("%s",&op); if(op[0]=='Q') { int an;scanf("%d",&an); printf("%d\n",sum(p[an])); }else { int a,b,c;scanf("%d%d%d",&a,&b,&c); if(op[0]=='D') c=-c; change(a,b,c); } } } return 0;}
- 【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 (树链剖分 树状数组 区间修改 点查询)(2011 Multi-University Training Contest 13)
- HDU 3966Aragorn's Story(树链剖分)点权更新模板
- HDU 3966 Aragorn's Story [树链剖分(点权)+树状数组]【数据结构】
- hdu 3966 Aragorn's Story(树链剖分对点编号)
- HDU 3966 Aragorn's Story (树链剖分 区间更新,点查询)
- hdu3966 Aragorn's Story(基于点权的树链剖分模板题(模板是基于已完善的边权树剖模板修改的,模板较较完善))
- hdu 3966 Aragorn's Story树剖成段更新单点查询模板
- HDU3966.Aragorn's Story——树链剖分+树状数组(基于点权)
- HDU 3966 Aragorn's Story(树剖-点)
- HDOJ 题目3966 Aragorn's Story(Link Cut Tree成段加减点权,查询点权)
- HDU 3966Aragorn's Story(树链剖分+线段树+入点)
- HDU 3966 Aragorn's Story 树链剖分+线段树(区间更新 单点查值)
- [ACM模板] BigInt高精度加/减/除法
- Java菜鸟栏目之九九乘法表
- Java第一课
- 统计语言模型
- bzoj2330 [SCOI2011]糖果(差分约束+spfa)
- 【HDU 3966 】Aragorn's Story 【树链剖分 基于点权,查询单点值,修改路径上的点权】
- cs231n作业配置
- 嵌入式多路温湿度监控系统(八网络客户端程序)
- HTML5教程之details展开收缩标签的应用
- HDU 5029Relief grain(树链剖分+线段树)
- 设计模式之Builder模式的简单实现
- sizeof使用规则及陷阱分析
- 数据结构——第三讲、树(上)(3)
- 常用的svn和git命令