Housewife Wind

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

题意很简单 k为0时:求s到u的距离 k为1时 把第i条边的权值变为w
求a b两点的距离 用LCA 设d[i]为i到根节点的距离 l(a.b)=d[a]+d[b]-2*d[lca(a,b)];(很容易想到)
修改权值的话 我们用L[i],R[i]分别表示在DFS中第一次经过i节点的时间戳和回溯到该点的时间戳
节点u到根节点的距离就是[0,L[u]]的和
对于更新每一条边时 DFS序较大的的节点为i 将l[i]的权值加上一个w R[i]+1的权值减去一个w 这样l{i]-R[i]区间内所有的顶点在求和的时候都加了w
 #include<iostream>
#include<cstdio>
#include<cmath>
#include<map>
#include<cstdlib>
#include<vector>
#include<set>
#include<queue>
#include<cstring>
#include<string.h>
#include<algorithm>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int N=+;
int head[N];
int cnt,n;
int vis[N];
int d[N],dp[N][];
struct node{
int to,next,w;
}edge[*N];
int t1,t2;
int pos[N],dep[N],f[N],G[N];
int w[N];
int L[N],R[N];
void init(){
memset(vis,,sizeof(vis));
cnt=;
memset(head,-,sizeof(head));
memset(pos,-,sizeof(pos));
memset(d,,sizeof(d));
t1=t2=;
}
void add(int u,int v,int w){
edge[cnt].to=v;
edge[cnt].w=w;
edge[cnt].next=head[u];
head[u]=cnt++;
}
int lowbit(int x){
return x&-x;
}
void update(int x,int y){
while(x<=n){
d[x]=d[x]+y;
x=x+lowbit(x);
}
}
int sum(int x){
int ans=;
while(x){
ans=ans+d[x];
x=x-lowbit(x);
}
return ans;
}
void init_RMQ(int n){
for(int i=;i<=n;i++)dp[i][]=i;
for(int j=;(<<j)<=n;j++)
for(int i=;i+(<<j)-<=n;i++)
if(dep[dp[i][j-]]<dep[dp[i+(<<j-)][j-]])dp[i][j]=dp[i][j-];
else
dp[i][j]=dp[i+(<<j-)][j-];
//dp[i][j]=min(dp[i][j-1],dp[i+(1<<j-1)][j-1]);
}
int RMQ(int l,int r){
int k=;
while((<<k+)<=r-l+)k++;
if(dep[dp[l][k]]<dep[dp[r-(<<k)+][k]])return dp[l][k];
else
return dp[r-(<<k)+][k];
//return min(dep[dp[l][k]],dep[dp[r-(1<<k)+1][k]]);
}
int lca(int u,int v){
if(pos[u]>pos[v])return f[RMQ(pos[v],pos[u])];
else
return f[RMQ(pos[u],pos[v])];
}
void DFS(int x,int deep){
f[t1]=x;
dep[t1]=deep;
pos[x]=t1++;
L[x]=++t2;
for(int i=head[x];i!=-;i=edge[i].next){
//cout<<4<<endl;
int v=edge[i].to;
if(pos[v]==-){
//cout<<3<<endl;
G[edge[i].w]=v;
DFS(v,deep+);
f[t1]=x;
dep[t1++]=deep;
} }
R[x]=t2;
}
int main(){
int q,s;
while(scanf("%d%d%d",&n,&q,&s)!=EOF){
init();
int u,v,W;
for(int i=;i<n;i++){
scanf("%d%d%d",&u,&v,&W);
add(u,v,i);
add(v,u,i);
w[i]=W;
}
DFS(,);
/*for(int i=1;i<=n;i++){
cout<<L[i]<<" "<<R[i]<<endl;
}
cout<<endl;
for(int i=1;i<=2*n-1;i++)cout<<f[i]<<" ";
cout<<endl;*/
init_RMQ(*n-);
for(int i=;i<n;i++){
update(L[G[i]],w[i]);
update(R[G[i]]+,-w[i]);
}
u=s;
while(q--){
int k;
scanf("%d",&k);
if(k==){
scanf("%d%d",&u,&W);
update(L[G[u]],W-w[u]);
update(R[G[u]]+,-W+w[u]);
w[u]=W;
}
else{
scanf("%d",&v);
printf("%d\n",sum(L[s])+sum(L[v])-*sum(L[lca(s,v)]));
s=v;
}
}
}
return ;
}

poj 2763(在线LCA+树状数组)的更多相关文章

  1. hdu 6203 ping ping ping(LCA+树状数组)

    hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...

  2. POJ 2763 Housewife Wind(DFS序+LCA+树状数组)

    Housewife Wind Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 11419   Accepted: 3140 D ...

  3. POJ 2352 Stars(树状数组)

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 30496   Accepted: 13316 Descripti ...

  4. POJ 3321 Apple Tree (树状数组+dfs序)

    题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...

  5. poj 2828 Buy Tickets(树状数组 | 线段树)

    题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...

  6. poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)

    题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...

  7. HDU 6203 ping ping ping(dfs序+LCA+树状数组)

    http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意: n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连 ...

  8. poj 2299 Ultra-QuickSort(树状数组求逆序数)

    链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...

  9. poj 3067 Japan(树状数组求逆序数)

    链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...

随机推荐

  1. 3.用Redis Desktop Manager连接Redis(CentOS)

    Redis Desktop Manager是Redis图形化管理工具,方便管理人员更方便直观地管理Redis数据. 然而在使用Redis Desktop Manager之前,有几个要素需要注意: 一. ...

  2. 关于 实体类中 时间字段 为string 类型和 datatime类型 比较

    经发现, 数据库中保存时间格式数据  可以正常 排序, 数据中保存时间格式字符串 排序出现问题 /// <summary> /// 修改时间 /// </summary> pu ...

  3. post发送 ArrayBuffer

    // 用 POST 方法将 ArrayBuffer 发送到服务器 ); var longInt8View = new Uint8Array(myArray); ; i< longInt8View ...

  4. cstringlist不完全用法

    CStringList是CString链表,在MFC编程中STL之外的另一选择,用起来更加简洁. 插入数据:AddTail();AddHead() 删除数据:RemoveAll();RemoveAt( ...

  5. .net core 使用 textSharp生成pdf

    引入Nuget包 using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.IO; namespace ...

  6. ARM异常中断返回的几种情况

    在学习韦老师视频中中断异常部分时候,对于发生中断时需要执行的#保存异现场 #恢复现场 中的“返回”弄不清楚,查阅网络文章后,发现一篇概述我觉得我能理解的一篇如下:   重要基础知识:R15(PC)总是 ...

  7. 一个ROS的服务,使机器人向前移动指定距离

    源代码有点长,放文末链接里了. 服务描述及代码现在的服务是:请求时携带要前进的距离,然后底盘前进相应距离.代码如下,改动很小: #!/usr/bin/env python import rospyfr ...

  8. C语言指针与指向指针的指针

    #include <stdio.h> #include <string.h> int main() { char a[]="hello world"; ch ...

  9. Vue中数据的处理

    临时变量参数的存储 1.引用类型和值类型的差异,及存储 2.在项目中做的绑定值,首先v-model 存储到临时变量,同时把数据在提交到api之前,需要做处理,因为是引用类型的数据,在处理之前,会修改掉 ...

  10. Spring MVC学习总结(9)——Spring MVC整合swagger自动生成api接口文档

    Swagger 号称:世界最流行的API框架,官网:http://swagger.io/,Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总 ...