FZU Problem 2082 过路费 树链剖分
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
Sample Output
Source
FOJ有奖月赛-2012年4月(校赛热身赛)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 2e5+, M = 5e5+, inf = 2e9, mod = ; struct edge{int to,next,value;}e[N * ]; struct Lin {int u; int v; int w;
Lin(int u = , int v = , int w = ) : u(u), v(v), w(w) {}
}L[N*];
LL sum[N];
int head[N],t=,f[N],q,n,top[N],siz[N],son[N],pos[N],val[N],deep[N],tot;
void init() {
t = ;
memset(head,,sizeof(head));
deep[] = ;
siz[] = ;
f[] = ;
tot = ;
}
void add(int u,int v)
{e[t].next=head[u];e[t].to=v;head[u]=t++;} void dfs1(int u,int fa) {
siz[u] = ;
son[u] = ;
f[u] = fa;
deep[u] = deep[fa] + ;
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == fa) continue;
dfs1(to,u);
siz[u] += siz[to];
if(siz[to] > siz[son[u]]) son[u] = to;
}
}
void dfs2(int u,int chan) {
top[u] = son[chan]==u?top[chan]:u;
pos[u] = ++tot;
if(son[u]) dfs2(son[u],u);
for(int i = head[u]; i; i = e[i].next) {
int to = e[i].to;
if(to == son[u] || to == chan) continue;
dfs2(to,u);
}
}
void build(int i,int ll,int rr)
{
sum[i] = ;
if(ll == rr) {
sum[i] = val[ll];
return ;
}
build(ls,ll,mid), build(rs,mid+,rr);
sum[i] = sum[ls] + sum[rs];
}
void update(int i,int ll,int rr,int x,int c) {
if(x == ll && x == rr) {
sum[i] = c;
return ;
}
if(x <= mid) update(ls,ll,mid,x,c);
else update(rs,mid+,rr,x,c);
sum[i] = sum[ls] + sum[rs];
}
LL query(int i,int ll,int rr,int x,int y) {
if(x == ll && rr == y) {
return sum[i];
}
if(y <= mid) return query(ls,ll,mid,x,y);
else if(x > mid) return query(rs,mid+,rr,x,y);
else return query(ls,ll,mid,x,mid) + query(rs,mid+,rr,mid+,y);
}
LL sub_query(int x,int y,LL ret = ) {
while (top[x] != top[y])
{
if(deep[top[x]] < deep[top[y]]) swap(x,y);
ret += query(,,n,pos[top[x]],pos[x]);
x = f[top[x]];
}
if(x == y) return ret;
if(deep[x] > deep[y]) swap(x,y);
return ret + query(,,n,pos[x]+, pos[y]);
}
void solve() {
init();
for(int i = ; i <= n-; ++i) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
L[i] = Lin(a,b,c);
add(a,b);add(b,a);
}
dfs1(,);
dfs2(,);
val[] = ;
for(int i = ; i <= n-; ++i) {
if(deep[L[i].u] < deep[L[i].v]) swap(L[i].u, L[i].v);
val[pos[L[i].u]] = L[i].w;
}//cout<<1<<endl;
build(,,n); while(q--){
int op,x,y;
scanf("%d%d%d",&op,&x,&y);
if(op == )
update(,,n,pos[L[x].u],y);
else printf("%I64d\n",sub_query(x,y));
}
}
int main() {
while(~scanf("%d%d",&n,&q)) {solve();}return ;
}
FZU Problem 2082 过路费 树链剖分的更多相关文章
- FZU 2082 过路费 (树链剖分 修改单边权)
题目链接:http://acm.fzu.edu.cn/problem.php?pid=2082 树链剖分模版题,求和,修改单边权. #include <iostream> #include ...
- FZU 2082 过路费(树链剖分)
FZU 2082 过路费 题目链接 树链抛分改动边的模板题 代码: #include <cstdio> #include <cstring> #include <vect ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- Fzu Problem 2082 过路费 LCT,动态树
题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528 Submit: 1654Time Limit ...
- FZU Problem 2082 过路费
Problem 2082 过路费 Accept: 875 Submit: 2839Time Limit: 1000 mSec Memory Limit : 32768 KB Problem ...
- BZOJ 4679/Hdu5331 Simple Problem LCT or 树链剖分
4679: Hdu5331 Simple Problem 题意: 考场上,看到这道题就让我想起BZOJ4712洪水.然后思路就被带着飞起了,完全没去考虑一条链的情况,于是GG. 解法:先考虑一条链的做 ...
- fzu 2082 过路费 (树链剖分+线段树 边权)
Problem 2082 过路费 Accept: 887 Submit: 2881Time Limit: 1000 mSec Memory Limit : 32768 KB Proble ...
- FZU 2082 过路费(树链剖分)
树链剖分模板题. FZU炸了,等交上去AC了再贴代码.
- FZU2176---easy problem (树链剖分)
http://acm.fzu.edu.cn/problem.php?pid=2176 Problem 2176 easy problem Accept: 9 Submit: 32Time Lim ...
随机推荐
- MVC RadioButton
本文介绍如何创建radiobutton. Step 1: 创建一个类用于获取所有的选项. public class Company { public string SelectedDepartment ...
- POJ 2388(排序)
http://poj.org/problem?id=2388 题意:就N个数的中位数. 思路:用快排就行了.但我没用快排,我自己写了一个堆来做这个题.主要还是因为堆不怎么会,这个拿来练练手. #inc ...
- Debian上安装Apache+Django全过程
-->start sudo apt-get install apache2 libapache2-mod-wsgi #https://wiki.debian.org/zh_CN/Apache s ...
- CentOS更新yum源
CentOS更新yum源 1.备份 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2.下载 ...
- properties配置文件的读取和写入
/** * 类名:PropertiesUtil * 功能:提供对properties配置文件的读取和写入 * @author ChengTao */package com.xy.xyd.rest.bi ...
- ACM/ICPC 之 双向链表_构造列表-模拟祖玛 (TSH OJ-Zuma(祖玛))
这一题是TsingHua OJ上的一道题目,学堂在线的一位数据结构老师的题目(原创),所以我直接把题目先贴下来了,这道题对复习双向链表很有帮助,而且也对数据结构中List,也就是对列表的回顾也是很有帮 ...
- Android 6.0的运行时权限
原文 http://droidyue.com/blog/2016/01/17/understanding-marshmallow-runtime-permission/ 主题 安卓开发 Andr ...
- 【C语言】指针
错误一: 一种错误的写法: * sizeof(int)); * sizeof(int)); y = x; 没有必要为y开辟内存,因为y在开辟内存时 y内存储的地址时开辟的内存的位置, 但是后面又把x的 ...
- IOS-Gesture(手势识别)
手势识别——Gesture Recognizer •iOS3.2版本之后,苹果推出了手势识别(Gesture Recognizer),其目的是: –简化开发者的开发难度 –统一用户体验 • •iOS目 ...
- SQL语句题库
一. 填空题 Not Only SQL数据库 泛指 非关系型数据库 . SYS和SYSTEM用户都是Oracle 的系统用户,它们都使用SYSTEM表空间,其中 sys 拥有更大的权限. O ...