边权操作起来也和点权一样,只要把边的权值映射到点上即可,要注意的地方是向上爬的过程中和点权不太一样,还有个特判(WA了几次。。)

完整代码

#include<cstring>
#include<cstdio>
#include<iostream>
using namespace std;
#define maxn 100005
struct E{int x,y;}e[maxn];
struct Edge{int to,nxt,w;}edge[maxn<<];
int head[maxn],tot,v[maxn],n,q,c;
void init(){memset(head,-,sizeof head);tot=;}
void addedge(int u,int v,int w){
edge[tot].to=v;edge[tot].w=w;edge[tot].nxt=head[u];head[u]=tot++;
}
int f[maxn],son[maxn],d[maxn],size[maxn];
void dfs1(int x,int pre,int deep){
size[x]=,d[x]=deep,f[x]=pre;
for(int i=head[x];i!=-;i=edge[i].nxt){
int y=edge[i].to;
if(y==pre)continue;
v[y]=edge[i].w;//注意这里将边权映射到点上
dfs1(y,x,deep+);
size[x]+=size[y];
if(size[y]>size[son[x]])son[x]=y;
}
}
int cnt,top[maxn],id[maxn],rk[maxn];
void dfs2(int x,int tp){
top[x]=tp;id[x]=++cnt;rk[cnt]=x;
if(son[x])dfs2(son[x],tp);
for(int i=head[x];i!=-;i=edge[i].nxt){
int y=edge[i].to;
if(y!=f[x] && y!=son[x])dfs2(y,y);
}
} #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[maxn<<];
void pushup(int rt){sum[rt]=sum[rt<<]+sum[rt<<|];}
void build(int l,int r,int rt){
if(l==r){sum[rt]=v[rk[l]];return;}
int m=l+r>>;
build(lson);build(rson);
pushup(rt);
}
void update(int pos,int l,int r,int rt,int v){
if(l==r){sum[rt]=v;return;}
int m=l+r>>;
if(pos<=m)update(pos,lson,v);
else update(pos,rson,v);
pushup(rt);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && R>=r)return sum[rt];
int m=l+r>>,res=;
if(L<=m)res+=query(L,R,lson);
if(R>m)res+=query(L,R,rson);
return res;
} int Query(int x,int y){
int res=;
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]])swap(x,y);
res+=query(id[top[x]],id[x],,n,);
x=f[top[x]];
}
if(x==y)return res;
if(id[x]>id[y])swap(x,y);
return res+query(id[son[x]],id[y],,n,);
} int main(){
init();int s;
scanf("%d%d%d",&n,&q,&s);
for(int i=;i<n;i++){
int x,y,z;scanf("%d%d%d",&x,&y,&z);
e[i].x=x;e[i].y=y;
addedge(x,y,z);addedge(y,x,z);
}
cnt=;dfs1(s,,);dfs2(s,s);
build(,n,);
while(q--){
int op,x,y;
scanf("%d",&op);
if(op==){scanf("%d",&x);
cout<<Query(s,x)<<'\n';s=x;}
if(op==){
scanf("%d%d",&x,&y);
int u=e[x].x,v=e[x].y;
if(d[u]<d[v])swap(u,v);
update(id[u],,n,,y);
}
}
}

不同之处:最后一次查询的时候不要最高的那个点权

特判:x==y的时候直接返回即可

int Query(int x,int y){
int res=;
while(top[x]!=top[y]){
if(d[top[x]]<d[top[y]])swap(x,y);
res+=query(id[top[x]],id[x],,n,);
x=f[top[x]];
}
if(x==y)return res;
if(id[x]>id[y])swap(x,y);
return res+query(id[son[x]],id[y],,n,);
}

树链剖分——边权poj2763的更多相关文章

  1. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  2. BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...

  3. 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)

    Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...

  4. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  5. HDU3669 Aragorn's Story 树链剖分 点权

    HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...

  6. BZOJ 1984: 月下“毛景树” [树链剖分 边权]

    1984: 月下“毛景树” Time Limit: 20 Sec  Memory Limit: 64 MBSubmit: 1728  Solved: 531[Submit][Status][Discu ...

  7. SPOJ 375 (树链剖分 - 边权剖分 - 修改单边权)

    题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=28982#problem/I 给你一棵有边权的树,有两个操作:一个操作是输出l到 ...

  8. 树链剖分边权模板spoj375

    树链剖分是树分解成多条链来解决树上两点之间的路径上的问题 如何求出树链:第一次dfs求出树上每个结点的大小和深度和最大的儿子,第二次dfs就能将最大的儿子串起来并hash(映射)到线段树上(或者其他数 ...

  9. 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释

    P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...

随机推荐

  1. Go语言里的slice

    1.切片是基于数组做的一层封装,灵活能够自动扩容. 2.切片的初始化方法 ①直接创建 ②基于已有的数组或切片 ③使用make来创建一个切片 第一个5是切片的大小 第二个5是切片的容量 3.基本操作 ① ...

  2. 题解:[JSOI2004]平衡点 / 吊打XXX

    这个题目算是一个模拟退火的板子题 物重一定,绳子越短,重物越低,势能越小,势能又与物重成正比 使得$\sum_{i=1}^nd[i]*w[i]$也就是总的重力势能最小,可以使得系统平衡 交了两面半.. ...

  3. 【学习笔记】JS知识点整理

    1 原型/原型链 1-1 原型 定义:原型是function对象的一个属性,定义了构造函数制造出的对象的公共祖先.通过该构造函数产生的对象,可以继承该原型的属性和方法. 原型是一个对象. 可以利用原型 ...

  4. 移动端适配单位rem

    0 写在前面 本周惊喜地发现,其他各个老师的软工班(罗杰老师班和欧阳老师班)的软工项目的alpha版本都已经发布了!(然而我们软工项目还没开始写代码…逃…) 十分好奇的我第一时间下载了一些他们的产品进 ...

  5. python之路day12--装饰器的进阶

    装饰器# 开发原则:开发封闭原则# 装饰器的作用:在不改变原函数的调用函数下,在函数的前后添加功能.# 装饰器的本质:闭包函数 import time def timmer(f): #func #ti ...

  6. 关于AI

    自己看着办吧 http://tieba.baidu.com/p/6008409988?fr=ala0&pstaala=1&tpl=5&fid=93764&isgod=0

  7. Centos6安装Percona-tools工具

    Centos6安装Percona-tools工具 环境:centos6.x yum -y install perl-DBI yum -y install perl-DBD-MySQL yum -y i ...

  8. 责任链模式-Chain of Responsibility(Java实现), 例1

    责任链模式-Chain of Responsibility, 例1 在这种模式中,通常每个接收者都包含对另一个接收者的引用.如果一个对象不能处理该请求,那么它会把相同的请求传给下一个接收者,依此类推. ...

  9. 078、Docker 最常用的监控方案(2019-04-25 周四)

    参考https://www.cnblogs.com/CloudMan6/p/7637361.html   当 Docker 部署规模逐步变大后,可视化监控容器环境的性能和健康状态会变得越来越重要.   ...

  10. 技术栈(technology stack)

    technology stack 技术栈: 产品实现上依赖的软件基础组件, 包括 1. 系统 2. 中间件 3. 数据库 4. 应用软件 5. 开发语言 6. 框架 https://en.wikipe ...