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 ;
}

最新文章

  1. 怎么在MVC中使用自定义Membership
  2. js中array的filter用法
  3. ES6 .Set数据结构去除重复元素
  4. Hash_集合
  5. iOS项目重构日记
  6. 怎么在手机浏览器上访问电脑本地的文件,局域网内,自建WiFi也可以
  7. Map集合案例
  8. HDU 1041 Computer Transformation
  9. 【笔试】T实习生2014 总结
  10. ZOJ-3721 Final Exam Arrangement 贪心
  11. Android 基础组件
  12. CODEVS 2451 互不侵犯
  13. JS弄ASP.NET(C#)在页GridView信息选择行
  14. Centos环境下给PHP7.0安装yaf扩展
  15. 使用bootstrap table 数据绑定
  16. collections和collection 还有集合
  17. JavaScript if 条件语句
  18. [CB转帖]台湾晶圆厂产能居全球第一 大陆排名第五但增长最多
  19. vue 踩坑-事件修饰符
  20. SSL证书/TLS证书是什么

热门文章

  1. C#命名规则和编码规范
  2. 关于reids
  3. Android 框架学习之 第一天 okhttp &amp; Retrofit
  4. C++的单例模式与线程安全单例模式(懒汉/饿汉)
  5. Openstack api 学习文档 &amp; restclient使用文档
  6. Linux LVM学习总结&mdash;&mdash;删除卷组VG
  7. MS SQL专用管理员连接DAC
  8. 浅谈Linux中的信号处理机制(二)
  9. [转]jquery遍历table的tr获取td的值
  10. Java中单例