poj2763树链剖分边权+区间和
自己写的比原来的板子常数小了不少嘻嘻,边权处理起来比点权要复杂一下
由于根节点没有被映射的边,其被访问到的顺序是0,直接排除在线段树外
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100005
struct Edge{
int to,next;
}edge[maxn<<];
int head[maxn],tot,e[maxn][];
int fa[maxn],son[maxn],num[maxn],deep[maxn];
int top[maxn],p[maxn],fp[maxn],pos;
inline void addedge(int u,int v){
edge[tot].to=v;
edge[tot].next=head[u];
head[u]=tot++;
}
void dfs1(int u,int pre,int dep){
fa[u]=pre;deep[u]=dep;num[u]=;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==pre) continue;
dfs1(v,u,dep+);
num[u]+=num[v];
if(son[u]==- || num[son[u]]<num[v]) son[u]=v;
}
}
void getpos(int u,int sp){
top[u]=sp;p[u]=pos++;fp[p[u]]=u;
if(son[u]==-) return;
getpos(son[u],sp);
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].to;
if(v==fa[u] || v==son[u]) continue;
getpos(v,v);
}
} #define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int sum[maxn<<];
inline void pushup(int rt){sum[rt]=sum[rt<<]+sum[rt<<|];}
void update(int pos,int val,int l,int r,int rt){
if(l==r) {sum[rt]=val;return;}
int m=l+r>>;
if(pos<=m) update(pos,val,lson);
else update(pos,val,rson);
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 query2(int u,int v){
int f1=top[u],f2=top[v],res=;
while(f1!=f2){
if(deep[f1]<deep[f2]) swap(f1,f2),swap(u,v);
res+=query(p[f1],p[u],,pos,);
u=fa[f1],f1=top[u];
}
if(u==v) return res;
if(deep[u]>deep[v]) swap(u,v);
res+=query(p[son[u]],p[v],,pos,);
return res;
}
void init(){
tot=pos=;
memset(head,-,sizeof head);
memset(son,-,sizeof head);
memset(sum,,sizeof sum);
}
int main(){
int n,q,now,op,a,b;
while(scanf("%d%d%d",&n,&q,&now)!=EOF){
init();
for(int i=;i<n;i++){
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
addedge(e[i][],e[i][]);addedge(e[i][],e[i][]);
}
dfs1(,,);getpos(,);
for(int i=;i<n;i++){
if(deep[e[i][]]>deep[e[i][]]) swap(e[i][],e[i][]);
update(p[e[i][]],e[i][],,pos,);
}
while(q--){
scanf("%d",&op);
if(op==) {scanf("%d",&a);printf("%d\n",query2(now,a));now=a;}
else {scanf("%d%d",&a,&b);update(p[e[a][]],b,,pos,);}//第a条边改为b
}
}
return ;
}
poj2763树链剖分边权+区间和的更多相关文章
- poj3237树链剖分边权+区间取负
树链剖分+线段树lazy-tag在树链上操作时千万不要写错.. /* 树链剖分+线段树区间变负 */ #include<iostream> #include<cstring> ...
- 计蒜客 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 ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- BZOJ 1036 [ZJOI2008]树的统计Count (树链剖分 - 点权剖分 - 单点权修改)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1036 树链剖分模版题,打的时候注意点就行.做这题的时候,真的傻了,单词拼错检查了一个多小时 ...
- HDU3669 Aragorn's Story 树链剖分 点权
HDU3669 Aragorn's Story 树链剖分 点权 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=3966 题意: n个点的,m条边,每个点都 ...
- POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12247 Accepted: 3151 Descriptio ...
- 洛谷 P3384 【模板】树链剖分-树链剖分(点权)(路径节点更新、路径求和、子树节点更新、子树求和)模板-备注结合一下以前写的题目,懒得写很详细的注释
P3384 [模板]树链剖分 题目描述 如题,已知一棵包含N个结点的树(连通且无环),每个节点上包含一个数值,需要支持以下操作: 操作1: 格式: 1 x y z 表示将树从x到y结点最短路径上所有节 ...
- BZOJ 1984: 月下“毛景树” [树链剖分 边权]
1984: 月下“毛景树” Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 1728 Solved: 531[Submit][Status][Discu ...
随机推荐
- Spark记录-SparkSQL相关学习
$spark-sql --help 查看帮助命令 $设置任务个数,在这里修改为20个 spark-sql>SET spark.sql.shuffle.partitions=20; $选择数据 ...
- Hive记录-Hive介绍(转载)
1.Hive是什么? Hive 是基于 Hadoop 的一个数据仓库工具,可以将结构化的数据文件映射为一张数据库表,并提供完整的 SQL 查询功能,将类 SQL 语句转换为 MapReduce 任务执 ...
- Access restriction: The constructor SunJCE() is not accessible 错误
Access restriction: The type 'SunJCE' is not API (restriction on required library 'C:\Program Files\ ...
- HDU - 4370 0 or 1
0 or 1 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HostAliases向Pod中添加hosts解析
前言 根据公司同一个项目需要不同的de/te/pe环境,由于相应环境调用的数据库等配置信息存在不同等因素,需要向Kubernetes集群中的Pod添加对应的hosts解析. 解决 以下以yaml文件自 ...
- 1.springboot:入门程序
一.Spring Boot 简介 官网英文: Spring Boot makes it easy to create stand-alone, production-grade Spring base ...
- shell反弹总结
NC反弹 常用的命令: -l 监听模式 -n 指定数字的IP地址 -p port(本地端口) -s addr 本地源地址 -v 详细输出 -i secs 延时的间隔 -e filename ...
- TwemProxy Redis架构
TwemProxy 1.twemproxy是twitter开发的一个redis代理proxy. 通过Twemproxy可以使用多台服务器来水平扩张redis服务,可以有效的避免redis单点故障问题. ...
- 属性动画QPropertyAnimation
属性动画QPropertyAnimation 改变大小.颜色或位置是动画中的常见操作,而QPropertyAnimation类可以修改控件的属性值 大小改变动画: import sys from Py ...
- html 替换元素
参考博客:http://www.cnblogs.com/wkylin/archive/2011/05/12/2044328.html 可替换元素也是行内元素 替换元素是浏览器根据其标签的元素与属性来判 ...