fzu 2082 过路费 (树链剖分+线段树 边权)
Accept: 887 Submit: 2881
Time Limit: 1000 mSec Memory Limit : 32768 KB
Problem Description
Input
有多组样例,每组样例第一行输入两个正整数n,m(2 <= n<=50000,1<=m <= 50000),接下来n-1行,每行3个正整数a b c,(1 <= a,b <= n , a != b , 1 <= c <= 1000000000).数据保证给的路使得任意两座城市互相可达。接下来输入m行,表示m个操作,操作有两种:一. 0 a b,表示更新第a条路的过路费为b,1 <= a <= n-1 ; 二. 1 a b , 表示询问a到b最少要花多少过路费。
Output
Sample Input
1 2 1
1 1 2
0 1 2
1 2 1
Sample Output
2
#include<iostream>
#include<cstring>
using namespace std;
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mid ll m = (l + r) >> 1;
const ll M = 1e5+;
ll sum[M<<],son[M],siz[M],head[M],top[M],fa[M],dep[M],tid[M<<],rk[M<<];
ll u[M],v[M],cnt1,cnt,wt[M],n,m;
struct node{
ll to,next,w;
}e[M]; void add(ll u,ll v,ll c){
e[++cnt1].to=v;e[cnt1].next=head[u];e[cnt1].w=c;head[u]=cnt1;
e[++cnt1].to=u;e[cnt1].next=head[v];e[cnt1].w=c;head[v]=cnt1;
}
void dfs1(ll u,ll faz,ll deep){
dep[u] = deep;
siz[u] = ;
fa[u] = faz;
for(ll i = head[u];i ;i=e[i].next){
ll v = e[i].to;
if(v != fa[u]){
wt[v] = e[i].w;
dfs1(v,u,deep+);
siz[u] += siz[v];
if(son[u]==-||siz[v]>siz[son[u]])
son[u] = v;
}
}
} void dfs2(ll u,ll t){
top[u] = t;
tid[u] = cnt;
rk[cnt] = wt[u];
//cout<<1<<endl;
cnt++;
if(son[u] == -) return ;
dfs2(son[u],t);
for(ll i = head[u];i;i=e[i].next){
ll v = e[i].to;
if(v != son[u]&&v != fa[u])
dfs2(v,v);
}
} void pushup(ll rt){
sum[rt] = sum[rt<<] + sum[rt<<|];
} void update(ll p,ll c,ll l,ll r,ll rt){
if(l == r){
sum[rt] = c;
return;
}
mid;
if(p <= m) update(p,c,lson);
else update(p,c,rson);
pushup(rt);
} void build(ll l,ll r,ll rt){
if(l == r){
sum[rt] = rk[l];
return ;
}
mid;
build(lson);
build(rson);
pushup(rt);
} ll query(ll L,ll R,ll l,ll r,ll rt){
if(L <= l&&R >= r){
return sum[rt];
}
mid;
ll ret = ;
if(L <= m) ret += query(L,R,lson);
if(R > m) ret += query(L,R,rson);
return ret;
} ll ask(ll x,ll y){
ll ans = ;
ll fx = top[x],fy = top[y];
while(fx != fy){
if(dep[fx] < dep[fy]) swap(fx,fy),swap(x,y);
if(fx == ) ans += query(tid[fx]+,tid[x],,n,);
else ans += query(tid[fx],tid[x],,n,);
x = fa[fx]; fx = top[x];
}
//当x,y top相同时,因为当前这个点代表的是它与父节点之前的边的权值
//top相同时有两种情况:1。两点重合,2.两点在同一重链上
if(x==y) return ans; //两点重合时直接返回当前的值就好了。
if(dep[x] > dep[y]) swap(x,y);
ans += query(tid[x]+,tid[y],,n,); //当两点在同一重链上时,dep较小的边需要+1。
return ans;
} void init()
{
memset(son,-,sizeof(son));
for(ll i = ; i <= n;i ++){
e[i].to = ;e[i].w = ;e[i].next = ;head[i] = ;
}
}
int main()
{
ios::sync_with_stdio();
cin.tie(); cout.tie();
ll c,x,a,b;
while(cin>>n>>m){
init();
cnt1 = ;cnt = ;
for(ll i = ;i < n;i ++){
cin>>u[i]>>v[i]>>c;
add(u[i],v[i],c);
}
dfs1(,,);
dfs2(,);
//cout<<1<<endl;
build(,n,);
while(m--){
cin>>x>>a>>b;
if(x==){
if(dep[u[a]] < dep[v[a]]) swap(u[a],v[a]);
update(tid[u[a]],b,,n,);
}
else{
cout<<ask(a,b)<<endl;
}
}
}
return ;
}
fzu 2082 过路费 (树链剖分+线段树 边权)的更多相关文章
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
- POJ3237 (树链剖分+线段树)
Problem Tree (POJ3237) 题目大意 给定一颗树,有边权. 要求支持三种操作: 操作一:更改某条边的权值. 操作二:将某条路径上的边权取反. 操作三:询问某条路径上的最大权值. 解题 ...
- bzoj4034 (树链剖分+线段树)
Problem T2 (bzoj4034 HAOI2015) 题目大意 给定一颗树,1为根节点,要求支持三种操作. 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x 为根的子 ...
- HDU4897 (树链剖分+线段树)
Problem Little Devil I (HDU4897) 题目大意 给定一棵树,每条边的颜色为黑或白,起始时均为白. 支持3种操作: 操作1:将a->b的路径中的所有边的颜色翻转. 操作 ...
- Aizu 2450 Do use segment tree 树链剖分+线段树
Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show ...
- 【POJ3237】Tree(树链剖分+线段树)
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- HDU 2460 Network(双连通+树链剖分+线段树)
HDU 2460 Network 题目链接 题意:给定一个无向图,问每次增加一条边,问个图中还剩多少桥 思路:先双连通缩点,然后形成一棵树,每次增加一条边,相当于询问这两点路径上有多少条边,这个用树链 ...
随机推荐
- C#可空类型(转载)
在程序开发中,有时候需要值类型也为可空类型,比如,在数据库中,我们可以把一个日期Datetime设置为null. 在C# 2.0中就出现了可空类型,允许值类型也可以为空(null),可空类型的实现基于 ...
- 20155318 《网络攻防》Exp4 恶意代码分析
20155318 <网络攻防>Exp4 恶意代码分析 基础问题 如果在工作中怀疑一台主机上有恶意代码,但只是猜想,所有想监控下系统一天天的到底在干些什么.请设计下你想监控的操作有哪些,用什 ...
- 20155334 《网络攻防》Exp5 MSF基础应用
一.基础问题回答 解释exploit,payload,encode是什么: 项目 作用 exploit 是负载有用代码的交通工具,让代码到达目的地,并作用 payload 是有具体功能的代码,能够完成 ...
- Selenium 爬取全国水质周报Word
很久没写爬虫了 ,昨天有个学姐说需要爬取水质的一些数据,给了个网站( http://xxfb.hydroinfo.gov.cn/ssIndex.html?type=2&tdsourcetag= ...
- mac终端将本地代码push到github总结
1.创建一个github账号 2.在本地目录下创建一个本地仓库,用来存放代码 mkdir prepass_repository (/Users/gejuncheng/文件/prepass_reposi ...
- java.lang.IllegalStateException: Cannot forward after response has been committe
参考:https://blog.csdn.net/lewky_liu/article/details/79845655 加上 return 搞定
- stl源码剖析 详细学习笔记 算法(5)
//---------------------------15/04/01---------------------------- //inplace_merge(要求有序) template< ...
- Unity角色对话
对话类------------------------------------------------------------------------------------------------- ...
- unity中利用纯物理工具制作角色移动跳跃功能
using System.Collections;using System.Collections.Generic;using UnityEngine; public class Player : M ...
- 转载别人的一篇关于git的文章
转载网址:https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000