hdu3966 树链剖分(区间更新和单点求值)
来源:互联网 发布:淘宝新卖家如何推广 编辑:程序博客网 时间:2024/05/18 02:04
http://acm.hdu.edu.cn/showproblem.php?pid=3966
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.
/**hdu3966 树链剖分(区间更新和单点求值)题目大意:给定一棵树,对指定的连点之间的点的权进行更新,过程中询问指定点的当前值解题思路:树链剖分,区间更新维护的题目*/#include <stdio.h>#include <string.h>#include <algorithm>#include <iostream>using namespace std;const int maxn=50005;typedef long long LL;int n,m,q,z;LL a[maxn],sum[maxn*4],col[maxn*4];int fa[maxn],Hash[maxn],dep[maxn],son[maxn],siz[maxn];int num[maxn],top[maxn];int head[maxn],ip;void init(){ memset(head,-1,sizeof(head)); memset(col,0,sizeof(col)); memset(sum,0,sizeof(sum)); ip=0;}struct note{ int v,next;}edge[maxn*4];void addedge(int u,int v){ edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;}void dfs(int u,int pre){ son[u]=0,siz[u]=1,dep[u]=dep[pre]+1,fa[u]=pre; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==pre)continue; dfs(v,u); if(siz[son[u]]<siz[v]) { son[u]=v; } siz[u]+=siz[v]; } ///printf("%d fa,dep,siz,son %d %d %d %d\n",u,fa[u],dep[u],siz[u],son[u]);}void set_que(int u,int tp){ top[u]=tp,num[u]=++z,Hash[z]=u; if(son[u]) { set_que(son[u],tp); } for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(v==fa[u]||v==son[u])continue; set_que(v,v); } /// printf("%d top num %d %d\n",u,top[u],num[u]);}void push_up(int root){ sum[root]=sum[root<<1]+sum[root<<1|1];}void build(int root,int l,int r){ col[root]=0; if(l==r) { sum[root]=a[Hash[l]]; return; } int mid=(l+r)>>1; build(root<<1,l,mid); build(root<<1|1,mid+1,r); push_up(root);}void push_down(int root,int len){ if(col[root]) { col[root<<1]+=col[root]; col[root<<1|1]+=col[root]; sum[root<<1]+=col[root]*(len-(len>>1)); sum[root<<1|1]+=col[root]*(len>>1); col[root]=0; }}void update(int root,int l,int r,int x,int y,int z){ if(l>y||r<x)return; if(x<=l&&r<=y) { col[root]+=z; sum[root]+=(r-l+1)*z; return; } push_down(root,r-l+1); int mid=(l+r)>>1; update(root<<1,l,mid,x,y,z); update(root<<1|1,mid+1,r,x,y,z); push_up(root);}LL query(int root,int l,int r,int loc){ if(l>loc||r<loc)return 0; if(l==r) { return sum[root]; } push_down(root,r-l+1); int mid=(l+r)>>1; if(loc<=mid) return query(root<<1,l,mid,loc); else return query(root<<1|1,mid+1,r,loc);}int main(){ //freopen("data.txt","r",stdin); while(~scanf("%d%d%d",&n,&m,&q)) { init(); for(int i=1;i<=n;i++) { scanf("%I64d",&a[i]); } for(int i=0;i<m;i++) { int u,v; scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } int root=(n+1)>>1; z=0,dep[0]=0,siz[0]=0; dfs(root,0); set_que(root,root); build(1,1,z); while(q--) { char s[20]; int x,y,zz; scanf("%s",s); if(s[0]=='Q') { scanf("%d",&x); printf("%I64d\n",query(1,1,z,num[x])); } else { scanf("%d%d%d",&x,&y,&zz); if(s[0]=='D')zz=-zz; int f1=top[x],f2=top[y]; while(f1!=f2) { if(dep[f1]<dep[f2]) { swap(f1,f2); swap(x,y); } update(1,1,z,num[f1],num[x],zz); x=fa[f1],f1=top[x]; } if(dep[x]>dep[y]) { swap(x,y); } update(1,1,z,num[x],num[y],zz); } } } return 0;}
0 0
- hdu3966 树链剖分(区间更新和单点求值)
- FZU2082 树链剖分(单点更新区间求值)
- poj 2763 树链剖分(单点更新,区间求值)
- light oj 1348 树链剖分(单点更新区间求值)
- HDU3966-树链剖分(区间更新,点查询)
- hdu3966(数链剖分+线段树区间更新)
- hdu1166敌兵布阵(线段树---单点更新,区间求值)
- hdu1754I Hate It(线段树---单点更新,区间求值)
- hdu3074Multiply game(线段树---单点更新,区间求值)
- 1005(三维树状数组+区间更新,单点求值)
- 树链剖分(线段树区间更新求和(lazy操作)hdu3966)
- spoj375 树链剖分(单点更新,区间查询)
- 1036: [ZJOI2008]树的统计Count 【树链剖分】【点权:单点更新区间求值】
- HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值)
- HDU1556 Color the ball 树状数组(区间更新单点求值)
- [HDU]1556 Color the ball (树状数组--区间更新,单点求值)
- Color the ball ---树状数组(区间更新及单点求值)
- poj 2155 Matrix 树状数组 区间更新单点求值
- 新手上路,scrollview 嵌套listview scrollview嵌套scrollview滑动冲突等问题的学习
- springmvc常用注解之@Controller和@RequestMapping
- 逊克身份证号码户籍地址信息查询
- 揭秘Sponge:统一Hadoop、Spark、SDS、Swift的大数据操作系统
- JAVA调用GDAL处理shapefile文件中文属性值乱码解决方法
- hdu3966 树链剖分(区间更新和单点求值)
- 怎么把图片文字转换成文字
- 感恩身边的风景
- Linux 进程调度管理
- UImage
- 查找排序实战---荷兰国旗问题(leetcode 75)
- Win程序处理繁忙而控件没刷新假死解决办法
- 【转】[译] 深入理解 Promise 五部曲:1. 异步问题
- Java压缩技术