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 ...
随机推荐
- POJ 1002
#include <stdio.h> #include <string.h> #include <stdlib.h> struct In{ int a; ]; }p ...
- ACM/ICPC 之 SPFA-兑换货币(POJ1860)
//水题-SPFA解法 //套汇是指兑换货币后能使本金上升 //给定本金货币编号,货币间的汇率和手续费,求能否套汇成功 //Time:16Ms Memory:200K #include<iost ...
- ACM/ICPC 之 数据结构-邻接表+DP+队列+拓扑排序(TSH OJ-旅行商TSP)
做这道题感觉异常激动,因为在下第一次接触拓扑排序啊= =,而且看了看解释,猛然发现此题可以用DP优化,然后一次A掉所有样例,整个人激动坏了,哇咔咔咔咔咔咔咔~ 咔咔~哎呀,笑岔了- -|| 旅行商(T ...
- vs2010:fatal error LNK1123: 转换到 COFF 期间失败
解决方法: 项目\属性\配置属性\清单工具\输入和输出\嵌入清单:原来是“是”,改成“否”.
- HDU 5833 Zhu and 772002 (数论+高斯消元)
题目链接 题意:给定n个数,这n个数的素因子值不超过2000,从中取任意个数使其乘积为完全平方数,问有多少种取法. 题解:开始用素筛枚举写了半天TLE了,后来队友说高斯消元才想起来,果断用模板.赛后又 ...
- 118. Pascal's Triangle
题目: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5, ...
- HTML标记语法之列表元素
1.无序列表 <ul> <li type=”项目符号类型”></li> <li type=”项目符号类型”></li> <li typ ...
- September 17th 2016 Week 38th Saturday
Our eyes do not show a lack of beauty, but a lack of observation. 世上不是缺少美,而是缺少发现美的眼睛. Don't pay too ...
- 如何激活webstorm 11
以前使用webstorm 10,可以在网上搜个注册码进行激活.后来升级了webstorm 11,发现原来的注册码和注册机已经不能激活了.查询后,才知道WebStorm 11改变了注册方法,可以在Lic ...
- Gson 解析列表
JsonArray jsonArray = new JsonParser().parse(resp).getAsJsonObject().getAsJsonArray("list" ...