POJ2763 Housewife Wind
Time Limit: 4000MS | Memory Limit: 65536K | |
Total Submissions: 9701 | Accepted: 2661 |
Description
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 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
Sample Input
3 3 1
1 2 1
2 3 2
0 2
1 2 3
0 3
Sample Output
1
3
Source
/*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的更多相关文章
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- POJ2763 Housewife Wind(树剖+线段树)
After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy lif ...
- 【lct】poj2763 Housewife Wind
题意:给你一棵树,边带权,支持两种操作:修改某条边的权值:查询两点之间的最短路. lct主要实现单点修改和路径和. 修改x结点的值只需将x Splay到其所在辅助树的根,然后修改其值,再maintai ...
- POJ2763 Housewife Wind(DFS序)
题目:单边修改,树链查询. 这题是边权,不是点权,不过也可以看作是点权. 然后其实就和BZOJ2819一样. #include<cstdio> #include<cstring> ...
- POJ2763 Housewife Wind (树链剖分)
差不多是模板题,不过要注意将边权转化为点权,将边的权值赋给它所连的深度较大的点. 这样操作过后,注意查询ask()的代码有所改变(见代码注释) 1 #include<cstdio> 2 # ...
- Housewife Wind
Housewife Wind 参考博客:POJ2763 Housewife Wind(树剖+线段树) 差不多是直接套线段树+树剖的板子,但是也有一些需要注意的地方 建树: void build() { ...
- Housewife Wind(边权树链剖分)
Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS Memory Limit: 65536K Total Submis ...
- POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )
POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
随机推荐
- ionic day01教程第一天之多平台运行(ios & android)
一.创建项目 创建项目 ionic start myApp 运行项目 (1)通过浏览器运行项目 进入项目,后运行ionic serve cd myApp ionic serve 浏览器运行效果 二.多 ...
- mysqldump: Error: Binlogging on server not active
在学习使用mysqldump时,使用mysqldump备份时,遇到了下面两个错误: [root@DB-Server backup]# ./mysql_dump_back.sh Warning: Usi ...
- ElasticSearch大数据分布式弹性搜索引擎使用
阅读目录: 背景 安装 查找.下载rpm包 .执行rpm包安装 配置elasticsearch专属账户和组 设置elasticsearch文件所有者 切换到elasticsearch专属账户测试能否成 ...
- dom4j读取某个元素的某个属性
一.dom4j介绍 dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的.dom4j是一个非常非常优秀的Java XML API,具有性能优异.功能强大和极端易用使用的特点, ...
- 每天一个linux命令目录
出处:http://www.cnblogs.com/peida/archive/2012/12/05/2803591.html 开始详细系统的学习linux常用命令,坚持每天一个命令,所以这个系列为每 ...
- Linux find命令的用法实践
一.find命令简介 Linux下find命令在目录结构中搜索文件,并执行指定的操作.Linux下find命令提供了相当多的查找条件,功能很强大.由于find具有强大的功能,所以它的选项也很多,其中大 ...
- [WPF系列]从基础起步学习系列计划
引言 WPF技术已经算不什么新技术,一搜一大把关于WPF基础甚至高级的内容.之前工作中一直使用winform所以一直没有深入学习WPF,这次因项目中使用了WPF技术来实现比较酷的展示界面.我在这里只是 ...
- 虚拟机linux上网问题
VMware中虚拟机网络的三种设置 1.桥接(bridged)(设置上网比较繁琐,但是在嵌入式开发中比较有用)2.NAT(上网比较简单,但是不能用于嵌入式开发中)3.Host only (该模式下仅主 ...
- oracle 错误代码大全
oracle错误代码大全(超详细) ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最 ...
- Object.assign方法复制或合并对象
Object.assign() 方法可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象 var obj = { a: 1 }; var copy = Object.assign({ ...