POJ2763 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
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
题意:
给一棵树,边之间有权值,给初始起点,然后两种操作,第一种:求起点到终点的权值和,而且这次的终点作为下次的起点。第二中,修改树上边的权值。
思路:
比较常规的树剖,就不说了。
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=;
int Laxt[maxn],Next[maxn],To[maxn],e[maxn][],cnt;
int opt,n,q,S,T;
struct TreeCut
{
int dpt[maxn],top[maxn],son[maxn],fa[maxn],sz[maxn],tot;
int tid[maxn],Rank[maxn],tim;
int Sum[maxn<<];
void init()
{
cnt=; tim=;
memset(Laxt,,sizeof(Laxt));
memset(Sum,,sizeof(Sum));
}
void add_edge(int u,int v)
{
Next[++cnt]=Laxt[u];
Laxt[u]=cnt; To[cnt]=v;
}
void dfs1(int u,int pre)
{
fa[u]=pre;dpt[u]=dpt[pre]+;sz[u]=;son[u]=;
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(v==pre) continue;
dfs1(v,u);sz[u]+=sz[v];
if(!son[u]||sz[v]>sz[son[u]]) son[u]=v;
}
}
void dfs2(int u,int Top)
{
top[u]=Top; tid[u]=tim++;Rank[tid[u]]=u;
if(!son[u]) return ; dfs2(son[u],Top);
for(int i=Laxt[u];i;i=Next[i])
if(To[i]!=fa[u]&&To[i]!=son[u]) dfs2(To[i],To[i]);
}
void update(int Now,int L,int R,int pos,int val)
{
if(L==R){ Sum[Now]=val; return; }
int Mid=(L+R)>>;
if(Mid>=pos) update(Now<<,L,Mid,pos,val);
else update(Now<<|,Mid+,R,pos,val);
Sum[Now]=Sum[Now<<]+Sum[Now<<|];
}
int getsum(int Now,int L,int R,int l,int r)
{
if(L>=l&&R<=r) return Sum[Now];
int Mid=(L+R)>>;
if(r<=Mid) return getsum(Now<<,L,Mid,l,r);
if(l>Mid) return getsum(Now<<|,Mid+,R,l,r);
else return getsum(Now<<,L,Mid,l,Mid)+getsum(Now<<|,Mid+,R,Mid+,r);
}
void Make_Tree()
{
for(int i=;i<n;i++){
scanf("%d%d%d",&e[i][],&e[i][],&e[i][]);
add_edge(e[i][],e[i][]);add_edge(e[i][],e[i][]);
} dfs1(,); dfs2(,);
for(int i=;i<n;i++){
if(dpt[e[i][]]<dpt[e[i][]]) swap(e[i][],e[i][]);
update(,,n-,tid[e[i][]],e[i][]);
}
}
int query(int u,int v)
{
int f1=top[u],f2=top[v],ans=;
while(f1!=f2){
if(dpt[f1]<dpt[f2]) swap(f1,f2),swap(u,v);
ans+=getsum(,,n-,tid[f1],tid[u]);
u=fa[f1]; f1=top[u];
}
if(u!=v){
if(dpt[u]>dpt[v]) swap(u,v);
ans+=getsum(,,n-,tid[son[u]],tid[v]);
} printf("%d\n",ans);
}
void Query()
{
while(q--) { scanf("%d",&opt);
if(opt==){
scanf("%d",&T);
query(S,T); S=T;
} else { int x,y;
scanf("%d%d",&x,&y);
update(,,n-,tid[e[x][]],y);
}
}
}
}Tc;
int main()
{
while(~scanf("%d%d%d",&n,&q,&S)){
Tc.init();
Tc.Make_Tree();
Tc.Query();
} return ;
}
POJ2763 Housewife Wind(树剖+线段树)的更多相关文章
- BZOJ_2238_Mst_树剖+线段树
BZOJ_2238_Mst_树剖+线段树 Description 给出一个N个点M条边的无向带权图,以及Q个询问,每次询问在图中删掉一条边后图的最小生成树.(各询问间独立,每次询问不对之后的询问产生影 ...
- BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树
BZOJ_4551_[Tjoi2016&Heoi2016]树_树剖+线段树 Description 在2016年,佳媛姐姐刚刚学习了树,非常开心.现在他想解决这样一个问题:给定一颗有根树(根为 ...
- BZOJ_2157_旅游_树剖+线段树
BZOJ_2157_旅游_树剖+线段树 Description Ray 乐忠于旅游,这次他来到了T 城.T 城是一个水上城市,一共有 N 个景点,有些景点之间会用一座桥连接.为了方便游客到达每个景点但 ...
- 【BZOJ5210】最大连通子块和 树剖线段树+动态DP
[BZOJ5210]最大连通子块和 Description 给出一棵n个点.以1为根的有根树,点有点权.要求支持如下两种操作: M x y:将点x的点权改为y: Q x:求以x为根的子树的最大连通子块 ...
- [LNOI2014]LCA(树剖+线段树)
\(\%\%\% Fading\) 此题是他第一道黑题(我的第一道黑题是蒲公英) 一直不敢开,后来发现是差分一下,将询问离线,树剖+线段树维护即可 \(Code\ Below:\) #include ...
- [CF1007D]Ants[2-SAT+树剖+线段树优化建图]
题意 我们用路径 \((u, v)\) 表示一棵树上从结点 \(u\) 到结点 \(v\) 的最短路径. 给定一棵由 \(n\) 个结点构成的树.你需要用 \(m\) 种不同的颜色为这棵树的树边染色, ...
- LOJ#3088. 「GXOI / GZOI2019」旧词(树剖+线段树)
题面 传送门 题解 先考虑\(k=1\)的情况,我们可以离线处理,从小到大对于每一个\(i\),令\(1\)到\(i\)的路径上每个节点权值增加\(1\),然后对于所有\(x=i\)的询问查一下\(y ...
- BZOJ3531-[Sdoi2014]旅行(树剖+线段树动态开点)
传送门 完了今天才知道原来线段树的动态开点和主席树是不一样的啊 我们先考虑没有宗教信仰的限制,那么就是一个很明显的树剖+线段树,路径查询最大值以及路径和 然后有了宗教信仰的限制该怎么做呢? 先考虑暴力 ...
- 【bzoj4699】树上的最短路(树剖+线段树优化建图)
题意 给你一棵 $n$ 个点 $n-1$ 条边的树,每条边有一个通过时间.此外有 $m$ 个传送条件 $(x_1,y_1,x_2,y_2,c)$,表示从 $x_1$ 到 $x_2$ 的简单路径上的点可 ...
- POJ3237 Tree(树剖+线段树+lazy标记)
You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edges are numbe ...
随机推荐
- Java IDL与javaRMI
Registry registry = LocateRegistry.getRegistry(); registry.rebind(RemoteService.name, stub); Java 平台 ...
- Git小玩
早就听说了GitHub的强大. 一直没有机会去看, 在公司实习的几个月里也没机会接触SVN和Git, 可是抱着对Linus大神的崇敬, 和开源的崇敬之情. 趁着不忙的几天, 来学习一下Git. 希 ...
- XMPP系列(三)---获取好友列表、加入好友
1.心跳检測.掉线重连功能 client和server端都能够设置多久发送一次心跳包,假设对方没有返回正确的pong信息,则会断开连接,而加入掉线重连功能,则会自己主动进行连接. 假设自己写聊天功能还 ...
- 并发insert情况下数据重复插入问题的解决方案
背景介绍 通常我们在接口里要保存一条数据时,会先判断该条记录在数据库里是否存在,如果不存在就插入,如果存在就返回已经存在. 就拿常见的工单来举例 Order order = orderService. ...
- 记录下关于ejabberd及XMPP的官网链接
ejabberd中文翻译 ——http://wiki.jabbercn.org/Ejabberd2:安装和操作指南 XMPP中文翻译: http://wiki.jabbercn.org/XEP-012 ...
- 用HttpClient模拟HTTP的GET和POST请求(转)
本文转自:http://blog.csdn.net/xiazdong/article/details/7724349 一.HttpClient介绍 HttpClient是用来模拟HTTP请求的,其 ...
- Flyweight Design Pattern 共享元设计模式
就是利用一个类来完毕多种任务.不用每次都创建一个新类. 个人认为这个设计模式在C++里面,好像能够就使用一个函数取代,利用重复调用这个函数完毕任务和重复利用这个类,好像几乎相同. 只是既然是一个设计模 ...
- LeetCode222——Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...
- Android重写FragmentTabHost来实现状态保存
近期要做一个类似QQ底部有气泡的功能,试了几个方案不太好.我想非常多开发人员使用TabHost都会知道它不保存状态.每次都要又一次载入布局.为了保存状态,使用RadioGroup来实现.状态是能够保存 ...
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position xxx ordinal not in range(12
python在安装时,默认的编码是ascii,当程序中出现非ascii编码时,python的处理常常会报这样的错UnicodeDecodeError: 'ascii' codec can't deco ...