树上路径区间更新,单点查询。

线段树和树状数组都可以用于本题的维护。

线段树:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define lson rt<<1
#define rson rt<<1|1
#define Lson l,m,lson
#define Rson m+1,r,rson
#define max(a,b) a>b? a:b
using namespace std;
const int maxn =5e4+;
struct Edge{
int to,next;
}E[*maxn];
int n,head[maxn],tot;
int cnt,idx,size[maxn],fa[maxn],son[maxn],dep[maxn],top[maxn],id[maxn],rnk[maxn];
int a[maxn];
struct Node{
int sum,add;
}tree[maxn<<];
void init()
{
cnt=idx=tot=;
memset(head,-,sizeof(head));
dep[]=,fa[]=,size[]=;
memset(son,,sizeof(son));
}
void AddEdge(int u,int v)
{
E[tot] = (Edge){v,head[u]};
head[u]=tot++;
}
void dfs1(int u)
{
size[u]=;
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=fa[u]){
fa[v]=u;
dep[v]=dep[u]+;
dfs1(v);
size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
} void dfs2(int u,int topu)
{
top[u]= topu;
id[u] = ++idx;
rnk[idx] = u;
if(!son[u]) return;
dfs2(son[u],top[u]);
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
} void pushup(int rt){ tree[rt].sum = tree[lson].sum + tree[rson].sum; } void pushdown(int l,int r,int rt){
if(tree[rt].add){
tree[lson].add += tree[rt].add;
tree[rson].add += tree[rt].add;
int m = (l+r)>>;
tree[lson].sum += (m-l+)*tree[rt].add;
tree[rson].sum += (r-m)*tree[rt].add;
tree[rt].add =;
}
} void build(int l,int r,int rt)
{
tree[rt].add = ;
if(l==r){
tree[rt].sum = a[rnk[l]];
return;
}
int m = (l+r)>>;
build(Lson);
build(Rson);
pushup(rt);
} void update(int L,int R,int v,int l,int r,int rt){
if(L<=l && R>=r){
tree[rt].sum +=(r-l+)*v;
tree[rt].add +=v;
return ;
}
pushdown(l,r,rt);
int m =(l+r)>>;
if(L<=m) update(L,R,v,Lson);
if(R>m) update(L,R,v,Rson);
pushup(rt);
} int query(int p,int l,int r,int rt){ //单点更新
if(l==r) return tree[rt].sum;
pushdown(l,r,rt);
int m = (l+r)>>,ans=;
if(p<=m) ans= query(p,Lson);
else ans =query(p,Rson);
pushup(rt);
return ans;
} void UPDATE(int u,int v,int w)
{
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update(id[top[u]],id[u],w,,n,);
u = fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
update(id[u],id[v],w,,n,);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int m,q,u,v;
char op[];
while(scanf("%d%d%d",&n,&m,&q)==){
init();
for(int i=;i<=n;++i)
scanf("%d",&a[i]);
while(m--){
scanf("%d%d",&u,&v);
AddEdge(u,v);
AddEdge(v,u);
}
dfs1();
dfs2(,);
build(,n,);
while(q--){
scanf("%s",op);
if(op[]=='Q'){
scanf("%d",&u);
printf("%d\n",query(id[u],,n,));
}
else{
int w;
scanf("%d%d%d",&u,&v,&w);
if(op[]=='D') w = -w;
UPDATE(u,v,w);
}
}
}
return ;
}

树状数组:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define lson rt<<1
#define rson rt<<1|1
#define Lson l,m,lson
#define Rson m+1,r,rson
#define max(a,b) a>b? a:b
using namespace std;
const int maxn =5e4+;
struct Edge{
int to,next;
}E[*maxn];
int n,head[maxn],tot;
int idx,size[maxn],fa[maxn],son[maxn],dep[maxn],top[maxn],id[maxn],rnk[maxn];
int a[maxn];
struct Node{
int sum,add;
}tree[maxn<<];
void init()
{
idx=tot=;
memset(head,-,sizeof(head));
dep[]=,fa[]=,size[]=;
memset(son,,sizeof(son));
}
void AddEdge(int u,int v)
{
E[tot] = (Edge){v,head[u]};
head[u]=tot++;
}
void dfs1(int u)
{
size[u]=;
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=fa[u]){
fa[v]=u;
dep[v]=dep[u]+;
dfs1(v);
size[u]+=size[v];
if(size[son[u]]<size[v]) son[u]=v;
}
}
} void dfs2(int u,int topu)
{
top[u]= topu;
id[u] = ++idx;
rnk[idx] = u; //建树用
if(!son[u]) return;
dfs2(son[u],top[u]);
for(int i=head[u];~i;i=E[i].next){
int v=E[i].to;
if(v!=fa[u]&&v!=son[u]) dfs2(v,v);
}
} void pushup(int rt){ tree[rt].sum = max(tree[lson].sum ,tree[rson].sum); } void pushdown(int l,int r,int rt){
if(tree[rt].add){
tree[lson].add += tree[rt].add;
tree[rson].add += tree[rt].add;
int m = (l+r)>>;
tree[lson].sum += (m-l+)*tree[rt].add;
tree[rson].sum += (r-m)*tree[rt].add;
tree[rt].add =;
}
} void build(int l,int r,int rt)
{
tree[rt].add = ;
if(l==r){
tree[rt].sum = a[rnk[l]];
return;
}
int m = (l+r)>>;
build(Lson);
build(Rson);
pushup(rt);
} void update(int L,int R,int v,int l,int r,int rt){
if(L<=l && R>=r){
tree[rt].sum +=v;
tree[rt].add +=v;
return ;
}
pushdown(l,r,rt);
int m =(l+r)>>;
if(L<=m) update(L,R,v,Lson);
if(R>m) update(L,R,v,Rson);
pushup(rt);
} int query(int p,int l,int r,int rt){ //单点更新
if(l==r) return tree[rt].sum;
pushdown(l,r,rt);
int m = (l+r)>>,ans=;
if(p<=m) ans= query(p,Lson);
else ans =query(p,Rson);
pushup(rt);
return ans;
} int bit[maxn];
void add(int pos,int val){
for(int i=pos;i<=n;i+= i&(-i)) bit[i]+=val;
} inline int sum(int pos){
int res=;
for(int i=pos;i;i-= i&(-i)) res+=bit[i];
return res;
} void UPDATE2(int u,int v,int w) //树状数组维护
{
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
add(id[top[u]],w);
add(id[u]+,-w);
u = fa[top[u]];
}
if(dep[u]>dep[v]) swap(u,v);
add(id[u],w);
add(id[v]+,-w);
} void UPDATE(int u,int v,int w) //线段树维护
{
while(top[u]!=top[v]){
if(dep[top[u]]<dep[top[v]]) swap(u,v);
update(id[top[u]],id[u],w,,n,);
u = fa[top[u]];
}
if(dep[u]>dep[v])swap(u,v);
update(id[u],id[v],w,,n,);
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int m,q,u,v;
char op[];
while(scanf("%d%d%d",&n,&m,&q)==){
init();
for(int i=;i<=n;++i) scanf("%d",&a[i]);
while(m--){
scanf("%d%d",&u,&v);
AddEdge(u,v);
AddEdge(v,u);
}
dfs1();
dfs2(,);
//build(1,n,1);
memset(bit,,sizeof(bit));
for(int i=;i<=n;++i){
add(id[i],a[i]);
add(id[i]+,-a[i]);
}
while(q--){
scanf("%s",op);
if(op[]=='Q'){
scanf("%d",&u);
printf("%d\n",sum(id[u]));
//printf("%d\n",query(id[u],1,n,1));
}
else{
int w;
scanf("%d%d%d",&u,&v,&w);
if(op[]=='D') w = -w;
UPDATE2(u,v,w);
//UPDATE(u,v,w);
}
}
}
return ;
}

HDU 3966 Aragorn's Story (树链剖分入门题)的更多相关文章

  1. HDU 3966 Aragorn's Story(树链剖分)(线段树区间修改)

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 3966 Aragorn's Story 树链剖分+树状数组 或 树链剖分+线段树

    HDU 3966 Aragorn's Story 先把树剖成链,然后用树状数组维护: 讲真,研究了好久,还是没明白 树状数组这样实现"区间更新+单点查询"的原理... 神奇... ...

  3. Hdu 3966 Aragorn's Story (树链剖分 + 线段树区间更新)

    题目链接: Hdu 3966 Aragorn's Story 题目描述: 给出一个树,每个节点都有一个权值,有三种操作: 1:( I, i, j, x ) 从i到j的路径上经过的节点全部都加上x: 2 ...

  4. HDU 3966 Aragorn's Story 树链剖分+BIT区间修改/单点询问

    Aragorn's Story Description Our protagonist is the handsome human prince Aragorn comes from The Lord ...

  5. HDU 3966 Aragorn's Story 树链剖分

    Link: http://acm.hdu.edu.cn/showproblem.php?pid=3966 这题注意要手动扩栈. 这题我交g++无限RE,即使手动扩栈了,但交C++就过了. #pragm ...

  6. hdu 3966 Aragorn's Story : 树链剖分 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://acm.hdu.edu.cn/showproblem.php?pid=3966 裸板 **/ #include<stdio.h> #include& ...

  7. hdu 3966 Aragorn's Story 树链剖分 按点

    Aragorn's Story Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  8. HDU 3966 Aragorn's Story (树链点权剖分,成段修改单点查询)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3966 树链剖分的模版,成段更新单点查询.熟悉线段树的成段更新的话就小case啦. //树链剖分 边权修 ...

  9. HDU 3966 Aragorn's Story 树链拋分

    一.写在前面 终于开始开坑link-cut-tree这个了,对于网上找到的大佬的前进路线,进行了一番研发,发现实际上可以实现对于树链拋分的制作.经历了若干长时间之后终于打了出来(为什么每次学什么东西都 ...

随机推荐

  1. asp.net调用系统设置字体文本框的方法

    本文实例展示了asp.net调用系统设置字体文本框的方法,是进行web开发中很实用的技巧.具体实现步骤如下: 一.调用系统字体文本框 首先在bin文件夹右击-->添加引用-->.net标签 ...

  2. VC++ 带界面的ActiveX控件

    一.新建MFC ActiveX工程OleHasInterface: 二.新建一个对话框资源,ID为 IDD_FORMVIEW,关联类CActXInterfaceDlg,基类CDialog: 三.设计对 ...

  3. mybatis 处理 mysql 表中的 text类型的 字段

    在mysql 中 text类型的字段: service_detail text NULL 服务描述   . 对应java文件中 model 中的 String:  private String ser ...

  4. java必备——经典的Hibernate

    在编程开发中,我们有非常多框架,他们有些非常方便,也非常有用,今天我们一起来认识一个java经典的框架Hibernate,Hibernate英文名称为"冬眠".这是个非常有意思的技 ...

  5. Android studio Unsupported major.minor version 52.0

    从目前以及我从网上搜索到的解决方案来说,出现此问题可以从以下两个方法入手: 1. JDK的版本和class版本不一致,通常是jdk版本过低解决方法: 1)使用Java -version和javac - ...

  6. 【IDEA】单元测试:项目中引入JUnit测试框架+Mock简单了解

    一.Junit 使用和说明: 参考:单元测试第三弹--使用JUnit进行单元测试-HollisChuang's Blog http://www.hollischuang.com/archives/17 ...

  7. 创建view,保存GROUP_CONCAT数据

    create view user_account_view asSELECT u.userId UserId ,u.userCode UserCode,GROUP_CONCAT(ac.id) Acco ...

  8. netty + Protobuf (整合二)

    [正文]Protobuf 消息设计 疯狂创客圈 死磕Netty 系列之12 [博客园 总入口 ] 本文说明 本篇是 netty+Protobuf 实战的第二篇,完成一个 基于Netty + Proto ...

  9. 微信公众号 openId 支付 php中file_get_contents与curl性能比较分析

    w http://www.jb51.net/article/57238.htm

  10. Java实现对List去重

    方式一,使用for循环遍历去除List中的重复元素代码如下 public static void main(String[] args) { Test07 test07 = new Test07(); ...