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. C#命名规则和编码规范

    用Pascal规则来命名属性.方法.事件和类名. public class HelloWorld { public void SayHello(string name) { } } Pascal规则是 ...

  2. [转]完美洗牌(Perfect Shuffle)问题

    [转]原博文地址:https://github.com/julycoding/The-Art-Of-Programming-By-July/blob/master/ebook/zh/02.09.md ...

  3. 我是这么给娃娃取名的(使用 node.js )

    依据: 81 命理,需要让五格都为大吉(吉).五格命理请自行谷歌. 我的是单姓复名.姓是固定的. 废话不说,上代码: Array.prototype.contains = function (k) { ...

  4. mysql权限与安全

    一.MySQL权限系统通过两个阶段进行认证: (A) 对用户进行身份认证,IP地址和用户名联合, (B) 对合法用户赋予相应权限,权限表在数据库启动的时候载入内存中. 二.在权限的存取过程中,会用到& ...

  5. [Java入门笔记] Java语言基础(三):运算符

    简介 运算符是一种特殊的符号,运算符是通过一定的运算规则操作一个或多个操作数,并生成结果的特定符号,运算符和操作数的有效组合称为表达式. Java中运算符主要分为以下几类: 赋值运算符 算术运算符 关 ...

  6. 看php手册2015-03-19版后备注

    类与对象->基本概念:1,#############################::class 自 PHP 5.5 起,关键词 class 也可用于类名的解析.使用 ClassName::c ...

  7. git 常规使用小结

    总结下 git 的常规使用: 一般我们使用 git 来维护项目代码. 前提背景: 远程服务器上代码库,包含分支: 1.master - 版本发布分支 2.dev - 平时开发用的分支 一般操作流程: ...

  8. Chrome插件: 网站收藏

      在工作中我们会收藏很多网址.以前一直都是用的chrome里面的收藏夹.后面觉得一点都不方便.看一下Chrome插件开发挺容易入手的所以自己写了一个Chrome插件. 基于:Angularjs + ...

  9. 运用PCA进行降维的好处

    运用PCA对高维数据进行降维,有一下几个特点: (1)数据从高维空间降到低维,因为求方差的缘故,相似的特征会被合并掉,因此数据会缩减,特征的个数会减小,这有利于防止过拟合现象的出现.但PCA并不是一种 ...

  10. [转]中国最大的Webshell后门箱子调查,所有公开大马全军覆没

    起因 对这件事情的起因是某天我渗透了一个大站,第二天进webshell时就发现,当前目录出现了新的后门,仔细一查,发现是博彩团伙干的,网站被全局劫持黑帽程序如下代码 set_time_limit(); ...