Problem 2082 过路费
 

 Problem Description

有n座城市,由n-1条路相连通,使得任意两座城市之间可达。每条路有过路费,要交过路费才能通过。每条路的过路费经常会更新,现问你,当前情况下,从城市a到城市b最少要花多少过路费。

 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

2 3 1 2 1 1 1 2 0 1 2 1 2 1

 Sample Output

1 2

 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 过路费 树链剖分的更多相关文章

  1. FZU 2082 过路费 (树链剖分 修改单边权)

    题目链接:http://acm.fzu.edu.cn/problem.php?pid=2082 树链剖分模版题,求和,修改单边权. #include <iostream> #include ...

  2. FZU 2082 过路费(树链剖分)

    FZU 2082 过路费 题目链接 树链抛分改动边的模板题 代码: #include <cstdio> #include <cstring> #include <vect ...

  3. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  4. Fzu Problem 2082 过路费 LCT,动态树

    题目:http://acm.fzu.edu.cn/problem.php?pid=2082 Problem 2082 过路费 Accept: 528    Submit: 1654Time Limit ...

  5. FZU Problem 2082 过路费

    Problem 2082 过路费 Accept: 875    Submit: 2839Time Limit: 1000 mSec    Memory Limit : 32768 KB Problem ...

  6. BZOJ 4679/Hdu5331 Simple Problem LCT or 树链剖分

    4679: Hdu5331 Simple Problem 题意: 考场上,看到这道题就让我想起BZOJ4712洪水.然后思路就被带着飞起了,完全没去考虑一条链的情况,于是GG. 解法:先考虑一条链的做 ...

  7. fzu 2082 过路费 (树链剖分+线段树 边权)

    Problem 2082 过路费 Accept: 887    Submit: 2881Time Limit: 1000 mSec    Memory Limit : 32768 KB  Proble ...

  8. FZU 2082 过路费(树链剖分)

    树链剖分模板题. FZU炸了,等交上去AC了再贴代码.

  9. FZU2176---easy problem (树链剖分)

    http://acm.fzu.edu.cn/problem.php?pid=2176 Problem 2176 easy problem Accept: 9    Submit: 32Time Lim ...

随机推荐

  1. 一个不错的定位API网站

    2015年5月2日 15:36:31 星期六 http://www.haoservice.com/

  2. 实验二 PHP基本语法实验

    实验二 PHP基本语法实验 0 实验准备 0.1实验环境和相关工具软件 具体到的机房环境,请在Windowsxp环境下做本实验: l  操作系统:Windowsxp l  Web服务器:Apache ...

  3. SolrCloud的官方配置方式

    前面写过生产过程中的SolrCloud集群配置,实际上官方给出的是免安装配置,启动时采用命令行参数的方式启动,这样相对简单,并且官方文档也给出了外部Zookeeper的配置,和前面说的基本一致,这个不 ...

  4. Maven 3.3.3 Win10环境下的使用实例(中)

    继上一篇文章介绍了Maven在Windows中的安装,本文将介绍 Maven 的核心概念. POM (Project Object Model) Maven 插件 Maven 生命周期 Maven 依 ...

  5. win8开发wpf程序遇到的无语问题

    在设置wpf程序全屏后,点击某个listbox列表,发现程序下面出现了任务栏. 查找解决答案未果.仔细一想可能是win8系统的问题. 最后试着把listbox的滚动条去掉了,问题解决. 原因:当程序中 ...

  6. SAP 工厂日生产计划待排维护

    *&---------------------------------------------------------------------* *& Report  ZPPR0024 ...

  7. 【leetcode】Number of 1 Bits (easy)

    做太多遍了,秒杀. class Solution { public: int hammingWeight(uint32_t n) { ; ), num++); return num; } };

  8. HDU 5831 Rikka with Parenthesis II (贪心) -2016杭电多校联合第8场

    题目:传送门. 题意:T组数据,每组给定一个长度n,随后给定一个长度为n的字符串,字符串只包含'('或')',随后交换其中两个位置,必须交换一次也只能交换一次,问能否构成一个合法的括号匹配,就是()( ...

  9. 22中编程语言的HelloWorld

    C:printf("HelloWorld"); C++ : cout<<"HelloWorld"; QBasic : Print "Hel ...

  10. 第K 小数

    [问题描述]有两个正整数数列,元素个数分别为N和M.从两个数列中分别任取一个数相乘,这样一共可以得到N*M个数,询问这N*M个数中第K小数是多少.[输入格式]输入文件名为number.in.输入文件包 ...