poj 2763(在线LCA+树状数组)
Housewife Wind
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
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
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+树状数组)的更多相关文章
- hdu 6203 ping ping ping(LCA+树状数组)
hdu 6203 ping ping ping(LCA+树状数组) 题意:给一棵树,有m条路径,问至少删除多少个点使得这些路径都不连通 \(1 <= n <= 1e4\) \(1 < ...
- POJ 2763 Housewife Wind(DFS序+LCA+树状数组)
Housewife Wind Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11419 Accepted: 3140 D ...
- POJ 2352 Stars(树状数组)
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 30496 Accepted: 13316 Descripti ...
- POJ 3321 Apple Tree (树状数组+dfs序)
题目链接:http://poj.org/problem?id=3321 给你n个点,n-1条边,1为根节点.给你m条操作,C操作是将x点变反(1变0,0变1),Q操作是询问x节点以及它子树的值之和.初 ...
- poj 2828 Buy Tickets(树状数组 | 线段树)
题目链接:poj 2828 Buy Tickets 题目大意:给定N,表示有个人,给定每一个人站入的位置,以及这个人的权值,如今按队列的顺序输出每一个人的权值. 解题思路:第K大元素,非常巧妙,将人入 ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数+离散化)
题目链接:http://poj.org/problem?id=2299 Description In this problem, you have to analyze a particular so ...
- HDU 6203 ping ping ping(dfs序+LCA+树状数组)
http://acm.hdu.edu.cn/showproblem.php?pid=6203 题意: n+1 个点 n 条边的树(点标号 0 ~ n),有若干个点无法通行,导致 p 组 U V 无法连 ...
- poj 2299 Ultra-QuickSort(树状数组求逆序数)
链接:http://poj.org/problem?id=2299 题意:给出n个数,求将这n个数从小到大排序,求使用快排的需要交换的次数. 分析:由快排的性质很容易发现,只需要求每个数的逆序数累加起 ...
- poj 3067 Japan(树状数组求逆序数)
链接:http://poj.org/problem?id=3067 题意:左边有n个城市,右边有m个城市,建k条道路,问有这k条道路中有多少个交点. 分析:将城市按x和y从小到大排序,对于每条道路,求 ...
随机推荐
- Android基础TOP4:Tost的使用
Activity: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xm ...
- STL之vector篇
#include<iostream> #include<cstdio> #include<cstring> #include<vector> #incl ...
- VMWare 安装Ubuntu 16.04
1.新建虚拟机 (1)点击文件-->新建虚拟机 (2)选择 自定义(高级)--> 下一步 (3)选择Workstation 12.0 --> 下一步 (4)选择 稍后安装操作系统 - ...
- html——特例
1.a标签与a标签之间有3px距离 2.标准流中的文字不会被浮动的盒子遮挡 <div style="width:150px;height:150px;background-color: ...
- CSS——宠物demo
注意:ul中自带padding值,需要清除. <!DOCTYPE html> <html lang="en"> <head> <meta ...
- Linux下的文件结构,及对应文件夹的作用
Linux下的文件结构,及对应文件夹的作用 /bin 二进制可执行命令 /dev 设备特殊文件 /etc 系统管理和配置文件 /etc/rc.d 启动的配置文件和脚本 /home 用户主目录的基点,比 ...
- 【VHDL】深度讲解二进制无符号和有符号加法处理溢出的问题
1.Unsigned adders 这个比较简单,只需在A.B前面扩展一位0防止溢出,溢出的数填到第n位cout,n-1到0位就是sum. , 2.Signed adders 一开始也搞不懂下图中为什 ...
- 使用python的几个小经验(查看文档)
好久没有水博客了,未来再过20天不到的时间又得参加软考,今天终于得好好水一发帖子 关于Python,很多人包括我之前都不知道怎么找文档,现在有一个好办法,就是在命令行模式下调用pydoc –p xxx ...
- Python之类方法,lambda,闭包简谈
类方法,lambda,闭包 类方法 lambda 闭包 类方法 classmethod staticmethod instancemethod 类方法 类方法,通过装饰器@classmethod来标明 ...
- 腾讯云,搭建python开发环境
准备工作 任务时间:5min ~ 10min Python是一种解释型.面向对象.动态数据类型的高级程序设计语言.首先我们来看看系统中是否已经存在 Python ,并安装一些开发工具包: 安装前准备 ...