HDU3966-树链剖分(区间更新,点查询)
来源:互联网 发布:unix编程 编辑:程序博客网 时间:2024/06/07 10:27
Aragorn's Story
Time Limit: 10000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11303 Accepted Submission(s): 2948
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.
题目大意:
给你n个点m条边,保证输入为一棵树(m = n-1) q次操作,每个点都有一个初始权值,操作分三种
1, Q x 查询x点的权值
2,D x y w 将x到y的路径上的所有点增加w
3,I x y w 同上只是这里是减少
题目思路:
树链剖分+线段树模板
AC代码:
#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const int maxn = 5e4+100;/** 邻接表部分 */struct st{ int v,nex;}edge[maxn<<1];int head[maxn],e;void add(int u,int v){ edge[e].v = v,edge[e].nex = head[u],head[u]=e++;}/** 树链剖分部分 */int fa[maxn],dep[maxn],siz[maxn],top[maxn],son[maxn],pos[maxn],rak[maxn],val[maxn];;int tim;void dfs(int u,int pr,int d){ siz[u] = 1,dep[u]=d,fa[u] = pr; for(int i = head[u];~i;i=edge[i].nex) { int v = edge[i].v; if(v==pr)continue; dfs(v,u,d+1); siz[u]+=siz[v]; if(son[u]==-1||siz[son[u]]<siz[v]) son[u]=v; }}void create(int u,int tp){ top[u] = tp; pos[u] = ++tim; rak[pos[u]] = u; if(son[u]==-1)return ; create(son[u],tp); for(int i=head[u];~i;i=edge[i].nex) { int v = edge[i].v; if(v != fa[u]&&v != son[u])create(v,v); }}/** 线段树部分 */struct nod{ int l,r; int sum,c;}node[maxn<<3];void pushdown(int rt,int m){ node[rt<<1].c+=node[rt].c; node[rt<<1|1].c+=node[rt].c; node[rt<<1].sum+=node[rt].c*(m-m/2); node[rt<<1|1].sum+=node[rt].c*(m/2); node[rt].c = 0;}void pushup(int rt){ node[rt].sum = node[rt<<1].sum+node[rt<<1|1].sum;}void BuildTree(int l,int r,int rt){ node[rt].l = l,node[rt].r = r,node[rt].c =0; if(l==r) { node[rt].sum = val[rak[l]]; return ; } int mid = (l+r)>>1; BuildTree(l,mid,rt<<1); BuildTree(mid+1,r,rt<<1|1); pushup(rt);}void update(int l,int r,int rt,int w){ if(l<=node[rt].l&&r>=node[rt].r) { node[rt].sum+=(node[rt].r-node[rt].l+1)*w; node[rt].c+=w; return ; } if(node[rt].c)pushdown(rt,node[rt].r-node[rt].l+1); int mid = (node[rt].l+node[rt].r)>>1; if(l<=mid)update(l,r,rt<<1,w); if(r>mid)update(l,r,rt<<1|1,w); pushup(rt);}int quary(int l,int r,int x,int rt){ if(l==r)return node[rt].sum; int mid = (node[rt].l+node[rt].r)>>1; if(node[rt].c) { pushdown(rt,node[rt].r - node[rt].l +1); } if(x<=mid) return quary(l,mid,x,rt<<1); else return quary(mid+1,r,x,rt<<1|1);}/** 运行部分 */int n,m,p;void change(int x,int y,int w){ while(top[x]!=top[y]) { if(dep[top[x]]<dep[top[y]])swap(x,y); update(pos[top[x]],pos[x],1,w); x = fa[top[x]]; } if(dep[x]>dep[y])swap(x,y); update(pos[x],pos[y],1,w);}void init(){ tim = e = 0; memset(head,-1,sizeof(head)); memset(son,-1,sizeof(son));}void sove(){ for(int i=1;i<=n;i++)scanf("%d",&val[i]); while(m--) { int x,y;scanf("%d%d",&x,&y); add(x,y); add(y,x); } dfs(1,1,1); create(1,1); BuildTree(1,n,1); while(p--) { char s[5]; int x,y,w; scanf("%s",s); if(s[0]=='Q') { scanf("%d",&x); printf("%d\n",quary(1,n,pos[x],1)); } else { scanf("%d%d%d",&x,&y,&w); if(s[0]=='D')w = -w; change(x,y,w); } }}int main(){ while(~scanf("%d%d%d",&n,&m,&p)) { init(); sove(); } return 0;}
0 0
- HDU3966-树链剖分(区间更新,点查询)
- hdu3966 树链剖分(区间更新和单点求值)
- 树链剖分+点权+hdu3966
- hdu3966(数链剖分+线段树区间更新)
- hdu3966 树链剖分(入门,点)
- HDU 3966(树链剖分 + 区间更新 + 点查询)
- POJ 3237Tree(树链剖分-线段树点更新-区间更新-区间最值查询-入边)
- 树链剖分(线段树区间更新求和(lazy操作)hdu3966)
- POJ 3237 Tree(树链剖分 + 单点更新 + 区间更新 + 区间查询)
- POJ 2763Housewife Wind(树链剖分+线段树点更新-区间查询+入边)
- SPOJ QTREEQuery on a tree(树链剖分-点更新-区间最值查询-入边)
- HDU 3966 Aragorn's Story (树链剖分 区间更新,点查询)
- hdu 3966 Aragorn's Story 树链路剖分 区间更新点查询
- HDOJ-1166(线段树点更新 + 区间查询)
- HDOJ-1823(矩形树,点更新 + 区间查询)
- Codeforces295A Greg and Array 数据结构+两次区间更新+点查询
- HDU1166线段树的点更新区间查询
- HDU1166:敌兵布阵(线段树点更新,区间查询)
- JavaScript 获取数组的最后一个元素
- OkHttp简易使用教程
- parseInt()
- 基于CentOS7.2搭建Git私人服务器
- char型变量中能不能存贮一个中文汉字?为什么?
- HDU3966-树链剖分(区间更新,点查询)
- 【OpenGL】OpenGL系列——03堆栈操作
- springmvc响应json数据的时候报错406解决方案
- GitLab 与 Gerrit
- MySQL数据库使用mysqldump导出数据详解
- Eclipse 中svn的合并与同步
- Android如何预置apk,jar,lib等任意文件到系统中
- 看电脑是UEFI/bios启动以及mbrgpt分区
- spring-bean作用域