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距离为 ...
随机推荐
- JVM-内存管理
都说搞C的牛叉,那是因为C解决问题,全靠程序员自己,他们对自己的程序在内存中是什么样了如指掌.而Java呢不需要有太多操作系统的知识,不用时刻注意内存的问题,但这不代表我们就不用去了解它背后的原理.J ...
- Coding道场:第一次
10/23日,我在部门内部进行了一次内部学习,使用目前流行的Coding Dojo(道场)方式,进行了TDD开发的演练.演练的题目如下: 有关Coding道场的介绍,请自行百度一下,我就不再多 ...
- SQLite学习笔记(八)&&sqlite实现架构
该系列的前面一些文章我重点讲了sqlite的核心功能,比如封锁机制,共享缓存,以及事务管理等.但对于sqlite的整体没有作一个全面的介绍,本文将从实现的层面,整体介绍sqlite的框架.各个核心模块 ...
- SQL Server高级查询
简介 关于数据库,我们经常会听说"增查删改"之类的词语,听起来很简单,但是如果想要准确的获取到需要的数据的话,还是要花点功夫的.下面由我来和大家谈谈高级查询的用法以及和普通查询的区 ...
- ComboBox(下拉列表框)实现省、市、县三级联动,用hibernate连接数据库
package com.hanqi.web; import java.io.IOException; import java.util.List; import javax.servlet.Servl ...
- 一则因为numa引发的mysqldump hang住
新买的dell r430服务器,双CPU,64G内存,单CPU32g,swap 3G 出现故障现像:mysqldump时会hang住,innodb_buffer_pool_size = ...
- Forbidden You don't have permission to access / on this server PHP
在新安装的谷歌游览器里,打不了PHP网站了,错误显示: Forbidden You don't have permission to access / on this server. 原因还是配置权限 ...
- 使用Jackson解析Json示例
原文http://blog.csdn.net/gebitan505/article/details/17005735 custom.json: { "country":&q ...
- android 学习中的一些问题记录 主要是概念问题
一些问题记录 应用程序 res 目录常见的目录有哪些,分别放置什么类型的资源? animator/ 和anim/ 放的都是定义动画的XML文件,两个地方的动画类型不同. color/ XML文件:定义 ...
- Entity Framework 6 with MySql
MySQL Connector/Net 6.8.x MySQL Server 5.1 or above Entity Framework 6 assemblies .NET Framework ...