自己写的比原来的板子常数小了不少嘻嘻,边权处理起来比点权要复杂一下

由于根节点没有被映射的边,其被访问到的顺序是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树链剖分边权+区间和的更多相关文章

  1. poj3237树链剖分边权+区间取负

    树链剖分+线段树lazy-tag在树链上操作时千万不要写错.. /* 树链剖分+线段树区间变负 */ #include<iostream> #include<cstring> ...

  2. 计蒜客 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 ...

  3. POJ2763 Housewife Wind 树链剖分 边权

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

  4. POJ3237 Tree 树链剖分 边权

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

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

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

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

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

  7. POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12247   Accepted: 3151 Descriptio ...

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

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

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

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

随机推荐

  1. Linux中如何安装RAR

    在Windows下的winrar几乎一统压缩软件的市场占有率,winrar只是RAR在Windows环境下的图形界面而已,核心功能还是RAR,那么如何在Linux中安装RAR呢? 1.下载RAR下载地 ...

  2. 为什么推荐InnoDB引擎使用自增主键?

    索引使用时遇到的问题(顺丰)--InnoDB引擎不使用自增主键导致性能问题,也可答最左前缀 InnoDB自增主键 InnoDB主索引(同时也是数据文件)的示意图: 上文讨论过InnoDB的索引实现,I ...

  3. POJ - 1836 Alignment (动态规划)

    https://vjudge.net/problem/POJ-1836 题意 求最少删除的数,使序列中任意一个位置的数的某一边都是递减的. 分析 任意一个位置的数的某一边都是递减的,就是说对于数h[i ...

  4. POJ 2443 Set Operation

    Set Operation Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 3558   Accepted: 1479 Des ...

  5. Spring RedisTemplate操作-注解缓存操作(11)

    @Service @CacheConfig(cacheNames="user") public class RedisAn { public Map<String, User ...

  6. 转--python 黑魔法2

    Python 高效编程小技巧 个人博客:临风|刀背藏身 Python 一直被我拿来写算法题,小程序,因为他使用起来太方便了,各种niubi闪闪的技能点也在写算法的过程中逐渐被挖掘到,感谢万能的谷哥度娘 ...

  7. 作业引擎quartz.net --- 监听链

    针对多个作业:如何描述各个跑批任务之间的顺序,紧前.紧后关系,实现灵活调度.例如:A完成则B开始,B完成C开始. 对quartz.net 进行了查阅,能实现如上业务,如下图: 测试代码: using ...

  8. ping不同网站总结

    用高级设置法预防Ping 用网络防火墙阻隔Ping 启用IP安全策略防Ping IP安全机制(IP Security)即IPSec策略,用来配置IPSec安全服务.这些策略可为多数现有网络中地多数通信 ...

  9. Java SE 之 DAO层接口设计思想

    用图说话 好处 1.只需要定义好IBaseDao的接口方法,并只需要实现BaseDaoImpl的方法,而具体的业务类和业务类/接口的方法的基本方法(IBaseDao已定义的)并不需要再考虑实现. 2. ...

  10. Java SE之反射技术[Class](三)

    /** * * @author Zen Johnny * */ package com.cpms.test; import java.lang.reflect.Field; import java.u ...