After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy life. People in XX Village lived in beautiful huts. There are some pairs of huts connected by bidirectional roads. We say that huts in the same pair directly connected. XX Village is so special that we can reach any other huts starting from an arbitrary hut. If each road cannot be walked along twice, then the route between every pair is unique.

Since Jiajia earned enough money, Wind became a housewife. Their children loved to go to other kids, then make a simple call to Wind: 'Mummy, take me home!'

At different times, the time needed to walk along a road may be different. For example, Wind takes 5 minutes on a road normally, but may take 10 minutes if there is a lovely little dog to play with, or take 3 minutes if there is some unknown strange smell surrounding the road.

Wind loves her children, so she would like to tell her children the exact time she will spend on the roads. Can you help her?

Input

The first line contains three integers n, q, s. There are n huts in XX Village, q messages to process, and Wind is currently in hut s. n < 100001 , q < 100001.

The following n-1 lines each contains three integers a, b and w. That means there is a road directly connecting hut a and b, time required is w. 1<=w<= 10000.

The following q lines each is one of the following two types:

Message A: 0 u 
A kid in hut u calls Wind. She should go to hut u from her current position. 
Message B: 1 i w 
 The time required for i-th road is changed to w. Note that the time change will not happen when Wind is on her way. The changed can only happen when Wind is staying somewhere, waiting to take the next kid. 

Output

For each message A, print an integer X, the time required to take the next child.

Sample Input

3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3

Sample Output

1
3

题意:

给一棵树,边之间有权值,给初始起点,然后两种操作,第一种:求起点到终点的权值和,而且这次的终点作为下次的起点。第二中,修改树上边的权值。

思路:

比较常规的树剖,就不说了。

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],e[maxn][],cnt;
int opt,n,q,S,T;
struct TreeCut
{
int dpt[maxn],top[maxn],son[maxn],fa[maxn],sz[maxn],tot;
int tid[maxn],Rank[maxn],tim;
int Sum[maxn<<];
void init()
{
cnt=; tim=;
memset(Laxt,,sizeof(Laxt));
memset(Sum,,sizeof(Sum));
}
void add_edge(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
void dfs1(int u,int pre)
{
fa[u]=pre;dpt[u]=dpt[pre]+;sz[u]=;son[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(v==pre) continue;
dfs1(v,u);sz[u]+=sz[v];
if(!son[u]||sz[v]>sz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int Top)
{
top[u]=Top; tid[u]=tim++;Rank[tid[u]]=u;
if(!son[u]) return ; dfs2(son[u],Top);
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=fa[u]&&To[i]!=son[u]) dfs2(To[i],To[i]);
}
void update(int Now,int L,int R,int pos,int val)
{
if(L==R){ Sum[Now]=val; return; }
int Mid=(L+R)>>;
if(Mid>=pos) update(Now<<,L,Mid,pos,val);
else update(Now<<|,Mid+,R,pos,val);
Sum[Now]=Sum[Now<<]+Sum[Now<<|];
}
int getsum(int Now,int L,int R,int l,int r)
{
if(L>=l&&R<=r) return Sum[Now];
int Mid=(L+R)>>;
if(r<=Mid) return getsum(Now<<,L,Mid,l,r);
if(l>Mid) return getsum(Now<<|,Mid+,R,l,r);
else return getsum(Now<<,L,Mid,l,Mid)+getsum(Now<<|,Mid+,R,Mid+,r);
}
void Make_Tree()
{
for(int i=;i<n;i++){
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
add_edge(e[i][],e[i][]);add_edge(e[i][],e[i][]);
} dfs1(,); dfs2(,);
for(int i=;i<n;i++){
if(dpt[e[i][]]<dpt[e[i][]]) swap(e[i][],e[i][]);
update(,,n-,tid[e[i][]],e[i][]);
}
}
int query(int u,int v)
{
int f1=top[u],f2=top[v],ans=;
while(f1!=f2){
if(dpt[f1]<dpt[f2]) swap(f1,f2),swap(u,v);
ans+=getsum(,,n-,tid[f1],tid[u]);
u=fa[f1]; f1=top[u];
}
if(u!=v){
if(dpt[u]>dpt[v]) swap(u,v);
ans+=getsum(,,n-,tid[son[u]],tid[v]);
} printf("%d\n",ans);
}
void Query()
{
while(q--) { scanf("%d",&opt);
if(opt==){
scanf("%d",&T);
query(S,T); S=T;
} else { int x,y;
scanf("%d%d",&x,&y);
update(,,n-,tid[e[x][]],y);
}
}
}
}Tc;
int main()
{
while(~scanf("%d%d%d",&n,&q,&S)){
Tc.init();
Tc.Make_Tree();
Tc.Query();
} return ;
}

POJ2763 Housewife Wind(树剖+线段树)的更多相关文章

  1. BZOJ_2238_Mst_树剖+线段树

    BZOJ_2238_Mst_树剖+线段树 Description 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影 ...

  2. BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树

    BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...

  3. BZOJ_2157_旅游_树剖+线段树

    BZOJ_2157_旅游_树剖+线段树 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但 ...

  4. 【BZOJ5210】最大连通子块和 树剖线段树+动态DP

    [BZOJ5210]最大连通子块和 Description 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块 ...

  5. [LNOI2014]LCA(树剖+线段树)

    \(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...

  6. [CF1007D]Ants[2-SAT+树剖+线段树优化建图]

    题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...

  7. LOJ#3088. 「GXOI / GZOI2019」旧词(树剖+线段树)

    题面 传送门 题解 先考虑\(k=1\)的情况,我们可以离线处理,从小到大对于每一个\(i\),令\(1\)到\(i\)的路径上每个节点权值增加\(1\),然后对于所有\(x=i\)的询问查一下\(y ...

  8. BZOJ3531-[Sdoi2014]旅行(树剖+线段树动态开点)

    传送门 完了今天才知道原来线段树的动态开点和主席树是不一样的啊 我们先考虑没有宗教信仰的限制,那么就是一个很明显的树剖+线段树,路径查询最大值以及路径和 然后有了宗教信仰的限制该怎么做呢? 先考虑暴力 ...

  9. 【bzoj4699】树上的最短路(树剖+线段树优化建图)

    题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...

  10. POJ3237 Tree(树剖+线段树+lazy标记)

    You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...

随机推荐

  1. Python Flask 在Sina App Engine (SAE)上安家

    早就听说了Python的大名,随着的编程语言的理解加深,越发认为动态语言的威力--真大呀. 趁这段时间不忙,我也用Python写了一个应用,而且将其部署到Sina App Engine (SAE).S ...

  2. 26:IPMaskCheck识别有效的ip地址和掩码并分类统计

    题目描述 请解析IP地址和对应的掩码,进行分类识别.要求按照A/B/C/D/E类地址归类,不合法的地址和掩码单独归类. 所有的IP地址划分为 A,B,C,D,E五类 A类地址1.0.0.0~126.2 ...

  3. python学习(七)字典学习

    #!/usr/bin/python # 字典 # 当时学java的时候, 语言基础就学了好久, 然后是各种API, 最后才是集合 # 键值对, 可变 # 1. 映射操作 D = {'food' : ' ...

  4. linux SPI驱动——spi协议(一)

    一:SPI简介以及应用 SPI, Serial Perripheral Interface, 串行外围设备接口, 是 Motorola 公司推出的一种同步串行接口技术. SPI 总线在物理上是通过接在 ...

  5. WinDbg调试分析 net站点 CPU100%问题

    WinDbg调试分析 asp.net站点 CPU100%问题 公司为了节省成本,最近有一批服务器降了配置,CPU从8核降到了2核.本身是小站点,访问量也不高,CPU总是会飙到100%而且可以一直持续几 ...

  6. C++钩子程序浅析

    在网上搜索“键盘记录C++”实现可以找到很多相关文章,我也是照着上面的介绍去研究去试着做的,从懂到不懂.那么为什么有那么多材料我还要去写这样一篇 文章,我想这个是我个人需要关心的问题,我不是那种ctr ...

  7. POJ3182 The Grove[射线法+分层图最短路]

    The Grove Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 904   Accepted: 444 Descripti ...

  8. 动态库对外暴露api的方法

    1 windows的动态库 在要export的函数声明的前面加上__declspec(dllexport)标识这个函数是从该dll中export出来给其它模块使用的. declspec是declare ...

  9. Webpack探索【8】--- 模块热替换详解

    本文主要讲模块热替换相关内容.

  10. 题解 P3389 【【模板】高斯消元法】

    题解 P3389 [[模板]高斯消元法] 看到大家都没有重载运算符,那我就重载一下运算符给大家娱乐一下 我使用的是高斯-约旦消元法,这种方法是精度最高的(相对地) 一句话解释高斯约旦消元法: 通过加减 ...