hdu3966(树链剖分)
2015-01-22 23:20
218 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966
题意:一颗树上,每个点有权值,定义三种操作:
1)I操作表示从a到b节点之间的节点都加上一个值
2)D操作表示从a到b节点之间的节点的都减去一个权值
3)Q操作询问a节点当前的值。
分析:树链剖分裸题,在树上修改点权及询问点权,将树上信息映射到线段上后,就是一道水线段树题了。
View Code
题意:一颗树上,每个点有权值,定义三种操作:
1)I操作表示从a到b节点之间的节点都加上一个值
2)D操作表示从a到b节点之间的节点的都减去一个权值
3)Q操作询问a节点当前的值。
分析:树链剖分裸题,在树上修改点权及询问点权,将树上信息映射到线段上后,就是一道水线段树题了。
#include <cstdio> #include <cstring> #include <string> #include <cmath> #include <iostream> #include <algorithm> #include <queue> #include <cstdlib> #include <stack> #include <vector> #include <set> #include <map> #define LL long long #define mod 10007 #define inf 0x3f3f3f3f #define N 100010 #define FILL(a,b) (memset(a,b,sizeof(a))) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 using namespace std; struct edge { int to,next; edge(){} edge(int to,int next):to(to),next(next){} }e[N<<1]; int head[N<<1],tot; int top ;//top[v]表示v所在的重链的顶端节点 int fa ;//父亲节点 int dep ;//深度 int sz ;//si[v]表示以v为根节点的子树的节点数 int son ;//重儿子 int p ;//p[v]表示v与其父亲节点的连边在线段树中的位置 int fp ;//与p数组相反 int pos;//所有链构成的线段树总长度 int col[N<<2],a ; void addedge(int u,int v) { e[tot]=edge(v,head[u]); head[u]=tot++; } void init() { tot=0;FILL(head,-1); pos=0;FILL(son,-1); } void dfs(int u,int f,int d) { dep[u]=d;sz[u]=1;fa[u]=f;//printf("%d %d %d\n",u,f,d); for(int i=head[u];~i;i=e[i].next) { int v=e[i].to;//printf("%d\n\n",v); if(v==f)continue; dfs(v,u,d+1); sz[u]+=sz[v]; if(son[u]==-1||sz[son[u]]<sz[v])son[u]=v; } } void getpos(int u,int sp) { top[u]=sp; p[u]=++pos; fp[pos]=u; if(son[u]==-1)return; getpos(son[u],sp); for(int i=head[u];~i;i=e[i].next) { int v=e[i].to; if(v!=son[u]&&v!=fa[u]) { getpos(v,v); } } } void Pushdown(int rt) { int ls=rt<<1,rs=ls|1; if(col[rt]) { col[rt<<1]+=col[rt]; col[rt<<1|1]+=col[rt]; col[rt]=0; } } void build(int l,int r,int rt) { if(l==r) { col[rt]=a[fp[l]]; return; } int m=(l+r)>>1; build(lson); build(rson); } void update(int L,int R,int c,int l,int r,int rt) { if(L<=l&&r<=R) { col[rt]+=c; return; } Pushdown(rt); int m=(l+r)>>1; if(L<=m)update(L,R,c,lson); if(m<R)update(L,R,c,rson); } int query(int ps,int l,int r,int rt) { if(l==r) return col[rt]; Pushdown(rt); int m=(l+r)>>1; if(ps<=m)return query(ps,lson); if(ps>m)return query(ps,rson); } void lca(int u,int v,int c) { int fu=top[u],fv=top[v]; while(fu!=fv) { if(dep[fu]<dep[fv]) { swap(fu,fv); swap(u,v); } update(p[fu],p[u],c,1,pos,1); u=fa[fu];fu=top[u]; } if(dep[u]>dep[v])swap(u,v); update(p[u],p[v],c,1,pos,1); } int main() { int n,m,t,u,v,w; while(scanf("%d%d%d",&n,&m,&t)>0) { init(); for(int i=1;i<=n;i++)scanf("%d",&a[i]); for(int i=1;i<=m;i++) { scanf("%d%d",&u,&v); addedge(u,v); addedge(v,u); } dfs(1,0,0); getpos(1,1); FILL(col,0); build(1,pos,1); char op[10]; while(t--) { scanf("%s",op); if(op[0]=='Q') { scanf("%d",&u); printf("%d\n",query(p[u],1,pos,1)); } else { scanf("%d%d%d",&u,&v,&w); if(op[0]=='D')w=-w; lca(u,v,w); } } } }
View Code
相关文章推荐
- hdu3966 or hdu5044 树链剖分
- HDU3966(树链剖分)
- hdu3966 Aragorn's Story 树链剖分
- 树链剖分+线段树 hdu3966 Aragorn's Story
- HDU3966 Aragorn's Story【树链剖分】
- HDU3966(树链剖分(点权)入门)链分治
- hdu3966 树链剖分+成段更新
- HDU3966 树链剖分
- HDU3966【树链剖分】。
- [HDU3966]Aragorn's Story(树链剖分)
- HDU3966-树链剖分(区间更新,点查询)
- hdu3966_树链剖分
- hdu3966 Aragorn's Story【树链剖分+线段树】
- HDU3966-Aragorn's Story-树链剖分-点权
- 树链剖分 hdu3966 (kuangbin)
- 树链剖分(线段树区间更新求和(lazy操作)hdu3966)
- HDU3966 Aragorn's Story(树链剖分 点权 模版题)
- HDU3966(树链剖分) 模板题
- HDU3966-Aragorn's Story(树链剖分)
- hdu3966 树链剖分(入门,点)