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从小到大排序,对于每条道路,求 ...
随机推荐
- 定制UVM Messages(参考)
UVM的Messages机制有些时候很繁琐,很多时候希望能够在UVM messages的基础上做一些个人化的订制,这里给出来一个找到的例子作为参考. my_macros.sv: `define ...
- 使用whIle循环语句和变量打印九九乘法表
-设置i变量declare @i int --设置j变量declare @j int --设置乘法表变量declare @chengfabiao varchar(1000)--给i,j,@chengf ...
- 白盒-CNN纹理深度可视化: 使用MIT Place 场景预训练模型
MIT发文:深度视觉的量化表示................ Places2 是一个场景图像数据集,包含 1千万张 图片,400多个不同类型的场景环境,可用于以场景和环境为应用内容的视觉认知任务. ...
- 关于Python中的classmethod
Python 中的 classmethod classmethod: 作用是直接将自己的类对象,传给类方法. 一.classmethod 1)不用classmethod的时候 你的代码可能是这样写的, ...
- 原生js实现瀑布流效果
参考此篇:https://segmentfault.com/a/1190000012621936 以下为个人测试中: css: .masonry{ width:100%; } .item{ posit ...
- .htaccess使用
RewriteEngine on #请求替换 #test-zhangsan-20 替换为 test.php?name=zhangsan&age=20 RewriteRule test-([a- ...
- MVCHelper 请求检验
public class MVCHelper { //有 两 个ModelStateDictionary类,别弄混乱了要使用 System.Web.Mvc 下的 //如果添加引用中找不到System. ...
- 洛谷——P1759 通天之潜水
P1759 通天之潜水 题目背景 直达通天路·小A历险记第三篇 题目描述 在猴王的帮助下,小A终于走出了这篇荒山,却发现一条波涛汹涌的河拦在了自己的面前.河面上并没有船,但好在小A有n个潜水工具. ...
- flask之配置文件的加载和动态url的使用
七行代码实现一个flask app from flask import Flask app = Flask(__name__) @app.route('/') def helloworld(): re ...
- idea SSM里配置redis
参考文章 https://blog.csdn.net/qq_17635843/article/details/78522776