Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 9701   Accepted: 2661

Description

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

Source

 
 
树链剖分。
 
↑然而并不会写树剖。
 
 
LCA+树状数组。
用树状数组维护差分数组的前缀和,差分数组里存边的权值(按DFS序存储,每个结点的进入时间戳加权值,退出时间戳减权值),这样修改边权会很方便。
求从一个点到另一个点的路程用LCA,差分数组前缀和可以得到“从根节点到当前节点”的距离dis,那么从x到y需要dis(x)+dis(y)-2*dis(LCA(x,y))
 
看嘛,挺简单的。
但是神TM我半夜肝这道题忘了加LCA的初始化,WA了一个多小时查不出原因??!!
深夜不能肝难题!
深夜不能肝难题!
 
 /*by SilverN*/
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
const int mxn=;
//read
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
//bas
int n,q,s;
int in[mxn],out[mxn];
int cnt=; //edge
struct edge{
int v,next;
int dis;
int id;
}e[mxn];
int hd[mxn],mcnt=; // hd[u]
void add_edge(int u,int v,int dis,int id){
e[++mcnt].v=v;e[mcnt].next=hd[u];e[mcnt].dis=dis;e[mcnt].id=id;hd[u]=mcnt;
}
int tto[mxn];//第i条边的去向
//tree
int t[mxn];
int tree_lmn;
int lowbit(int x){return x&-x;}
void add(int p,int v){
while(p<=tree_lmn){t[p]+=v;p+=lowbit(p);}
return;
}
int smm(int x){
int res=;
while(x){res+=t[x];x-=lowbit(x);}
return res;
}
//DFS
int dep[mxn];//int dis[mxn];
int disto[mxn];
int fa[mxn][];
void DFS(int u,int father){
int i,j;
in[u]=++cnt;
for(i=hd[u];i;i=e[i].next){
int v=e[i].v;
if(v==father)continue;
tto[e[i].id]=v;
disto[v]=e[i].dis;
dep[v]=dep[u]+;//深度
fa[v][]=u;
DFS(v,u);
}
out[u]=++cnt;
}
//LCA
void initLCA(){
int i,j;
for(i=;i<=;i++){
for(j=;j<=n;j++){
fa[j][i]=fa[fa[j][i-]][i-];
}
}
return;
}
int LCA(int x,int y){
if(dep[x]<dep[y])swap(x,y);
int i;
for(i=;i>=;i--){
if(dep[fa[x][i]]>=dep[y])
x=fa[x][i];
}
if(x==y) return y;
for(i=;i>=;i--)
if(fa[x][i]!=fa[y][i]){
x=fa[x][i];
y=fa[y][i];
}
return fa[x][];
}
//calc
int tmp;
int dist(int x,int y){//算书上路径
tmp=LCA(x,y);
return smm(in[x])+smm(in[y])-*smm(in[tmp]);
}
//main
int main(){
n=read();q=read();s=read();
tree_lmn=mxn-;
int i,j;
int u,v,d;
for(i=;i<n;i++){
u=read();v=read();d=read();
add_edge(u,v,d,i);
add_edge(v,u,d,i);
}
//
dep[]=;
DFS(,);
initLCA();//调了一晚上没有加这个初始化!初始化!初始化!初始化!初始化!
for(i=;i<=n;i++){//预处理dfs序数组
add(in[i],disto[i]);
add(out[i],-disto[i]);
}
int opr;
while(q--){
opr=read();
if(opr==){//从s去v点
v=read();
printf("%d\n",dist(s,v));
s=v;
}
else{//改权值
v=read();d=read();
v=tto[v];
add(in[v],d-disto[v]);
add(out[v],-d+disto[v]);
disto[v]+=d-disto[v];
}
}
return ;
}

POJ2763 Housewife Wind的更多相关文章

  1. POJ2763 Housewife Wind 树链剖分 边权

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

  2. POJ2763 Housewife Wind(树剖+线段树)

    After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy lif ...

  3. 【lct】poj2763 Housewife Wind

    题意:给你一棵树,边带权,支持两种操作:修改某条边的权值:查询两点之间的最短路. lct主要实现单点修改和路径和. 修改x结点的值只需将x Splay到其所在辅助树的根,然后修改其值,再maintai ...

  4. POJ2763 Housewife Wind(DFS序)

    题目:单边修改,树链查询. 这题是边权,不是点权,不过也可以看作是点权. 然后其实就和BZOJ2819一样. #include<cstdio> #include<cstring> ...

  5. POJ2763 Housewife Wind (树链剖分)

    差不多是模板题,不过要注意将边权转化为点权,将边的权值赋给它所连的深度较大的点. 这样操作过后,注意查询ask()的代码有所改变(见代码注释) 1 #include<cstdio> 2 # ...

  6. Housewife Wind

    Housewife Wind 参考博客:POJ2763 Housewife Wind(树剖+线段树) 差不多是直接套线段树+树剖的板子,但是也有一些需要注意的地方 建树: void build() { ...

  7. Housewife Wind(边权树链剖分)

    Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS   Memory Limit: 65536K Total Submis ...

  8. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  9. POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新

    题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...

随机推荐

  1. iOS开发之功能模块--用runtime给UIView类别拓展PressMenu工具

    这是个很有用的列别工具类,在聊天对话框添加和QQ一样的"复制.粘贴.取消"等选项,而且使用起来很方便,只要找到聊天泡泡内部的某个View,比如Label或者背景冒泡的UIImage ...

  2. ORACLE会话连接进程三者总结

    概念介绍 通俗来讲,会话(Session) 是通信双方从开始通信到通信结束期间的一个上下文(Context).这个上下文是一段位于服务器端的内存:记录了本次连接的客户端机器.通过哪个应用程序.哪个用户 ...

  3. 深入理解Linux修改hostname

    当我觉得对Linux系统下修改hostname已经非常熟悉的时候,今天碰到了几个个问题,这几个问题给我好好上了一课,很多知识点,当你觉得你已经掌握的时候,其实你了解的还只是皮毛.技术活,切勿浅尝则止! ...

  4. 【转】Java并发编程注意事项

    保证线程安全的三种方法: 不要跨线程访问共享变量 使共享变量是final类型的 将共享变量的操作加上同步 一开始就将类设计成线程安全的, 比在后期重新修复它,更容易. 编写多线程程序, 首先保证它是正 ...

  5. Nova reboot 和 lock 操作 - 每天5分钟玩转 OpenStack(32)

    前面 CloudMan 通过日志详细分析了 nova 的 launch, shut off 和 start 操作.不知道大家现在是否已经掌握了日志分析的技能? 今天咱们就来检验一下.本节讨论的是 no ...

  6. C++STL - 函数模板

    模板主要是为了泛型编程,做到与类型无关 模板有函数模板和类模板,本文主要整理的是函数模板 1.函数模板定义 template<typename 类型形参1,typename 类型形参2,...& ...

  7. 标题栏Menu

    标题栏menu就是指下图中红框里面的菜单按钮. 标题栏上所有的按钮或者其他元素都定义在xml文件里面,这些文件资源称为menu resource.要在标题栏添加按钮,需要在项目的/res/menu/路 ...

  8. JQuery判断元素是否存在

    JQuery判断元素是否存在的原理与javascript略有不同,因为$选择器选择的元素无论是否存在都不会返回null或undefined,要使用JQuery判断元素是否存在,只能使用length属性 ...

  9. 域用户执行金蝶K/3报错解决方法

    自从上星期测试加域意外将公司考勤系统整出事后,就再也不敢在物理机测试了. 装好虚拟机和装好金蝶K/3系统后,被这条报错信息折腾了好几天,一方面是不熟悉该软件,另一方面是几乎搜不到相关的文章. 一.问题 ...

  10. [WPF系列]-DynamicResource与StaticResource的区别

    探讨: 1.当引用资源时,选择StaticResource还是DynamicResource的考虑因素: (1)在哪里创建资源?(资源的范围或层级) a. 资源是在一个Page/Canvas/Wind ...