HDU 3966 树链剖分
来源:互联网 发布:sql培训机构 编辑:程序博客网 时间:2024/05/16 05:09
点击打开链接
题意:给一个树,三个操作,一个询问点的权值,一个将u到v路径上的点的权值增加,一个是减少
思路:还是比较裸的模版题,线段树改为区间更新就好了
#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>#include <algorithm>using namespace std;typedef long long ll;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const ll INF=0x3f3f3f3f3f3f3f3fll;const int maxn=50010;int fa[maxn],siz[maxn],son[maxn],w[maxn],p[maxn],dep[maxn],fp[maxn];//fa为父节点,siz为子节点中siz最大的,dep为深度,son为重儿子,w表示在线段树中的位置int num[maxn<<2],lazy[maxn<<2];int tree_id,n;vector<int>G[maxn];void dfs1(int u,int ff,int deep){ son[u]=0;fa[u]=ff;siz[u]=1;dep[u]=deep; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v==ff) continue; dfs1(v,u,deep+1); siz[u]+=siz[v]; if(siz[v]>siz[son[u]]) son[u]=v; }}void dfs2(int u,int ff){ w[u]=++tree_id;p[u]=ff; if(son[u]) dfs2(son[u],ff); else return ; for(unsigned int i=0;i<G[u].size();i++){ int v=G[u][i]; if(v!=fa[u]&&v!=son[u]) dfs2(v,v); }}void pushdown(int node){ if(lazy[node]){ lazy[node<<1]+=lazy[node]; lazy[node<<1|1]+=lazy[node]; num[node<<1]+=lazy[node]; num[node<<1|1]+=lazy[node]; lazy[node]=0; }}void update(int l,int r,int val,int le,int ri,int node){ if(l<=le&&ri<=r){ num[node]+=val; lazy[node]+=val; return ; } pushdown(node); int t=(le+ri)>>1; if(l<=t) update(l,r,val,le,t,node<<1); if(r>t) update(l,r,val,t+1,ri,node<<1|1);}int query(int pos,int le,int ri,int node){ if(le==ri) return num[node]; pushdown(node); int t=(le+ri)>>1; if(pos<=t) query(pos,le,t,node<<1); else query(pos,t+1,ri,node<<1|1);}void getans(int u,int v,int val){ int f1=p[u],f2=p[v]; while(f1!=f2){ if(dep[f1]<dep[f2]){ swap(f1,f2); swap(u,v); } update(w[f1],w[u],val,1,n,1); u=fa[f1];f1=p[u]; } if(dep[u]>dep[v]) swap(u,v); update(w[u],w[v],val,1,n,1);}int A[maxn];int main(){ char str[10]; int m,q,u,v; while(scanf("%d%d%d",&n,&m,&q)!=-1){ for(int i=1;i<=n;i++) scanf("%d",&A[i]); for(int i=0;i<maxn;i++) G[i].clear(); memset(son,0,sizeof(son));tree_id=0; memset(lazy,0,sizeof(lazy)); for(int i=0;i<m;i++){ scanf("%d%d",&u,&v); G[u].push_back(v); G[v].push_back(u); } dfs1(1,1,0); dfs2(1,1); memset(num,0,sizeof(num)); while(q--){ scanf("%s",str); if(str[0]=='Q'){ scanf("%d",&u); printf("%d\n",A[u]+query(w[u],1,n,1)); }else if(str[0]=='I'){ scanf("%d%d%d",&u,&v,&m); getans(u,v,m); }else{ scanf("%d%d%d",&u,&v,&m); getans(u,v,-m); } } } return 0;}
0 0
- HDU 3966 树链剖分
- hdu 3966 树链剖分模版
- hdu 3966 树链剖分
- hdu--3966(树链剖分)
- HDU 3966 树链剖分学习
- HDU 3966 树链剖分模板
- hdu 3966(树链剖分)
- HDU 3966 树链剖分
- HDU 3966 树链剖分
- hdu 3966(树链剖分)
- HDU 3966 树链剖分
- HDU 3966 [树链剖分]
- 树链剖分 HDU 3966
- HDU-3966-树链剖分
- hdu 3966 树链剖分
- hdu 3966 树链剖分
- hdu 3966 (树链剖分+线段树)
- hdu 3966 树链剖分+线段树
- CSS实现单行、多行文本溢出显示省略号(…)
- 图像特征提取之局部秩
- 华硕笔记本X450JB拆机及加装固态硬盘
- HDOJ 1811 Rank of Tetris (并查集+topo)
- javascript基础:表单处理2
- HDU 3966 树链剖分
- CrackingtheCodeInterview之中等难题
- 前端攻城-开章篇
- make的时候会报错"g++: Command not found"
- ajax实体类属性数据传输总结
- 从尾到头打印链表
- VVDocumenter-Xcode安装及遇到问题解决方案
- Iterator、listIterator、Iterable
- 最少拦截系统(贪心)