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 ...
随机推荐
- Unity3d《Shader篇》描边
Shader "Custom/OutLine" {Properties { _Color ("Main Color", Color) = (.5,.5,.5,1 ...
- WPS Office文档未保存怎么恢复
有时候用WPS Office时,文档还没保存,因为电脑卡死或者关机,再次打开时编辑的内容都不见了,这个时候可以利用WPS自带的备份功能来恢复文档,表格.幻灯片.文档都是可以的. 首先单击WPS左上角的 ...
- ACM/ICPC 之 一道不太简单的DP面试题(Geeksforgeeks)
题面来源:geeksforgeeks/1993 题解:geeksforgeeks 题目简述:给一个m*n的矩阵,计算从(1,1)到(m,n)的所有不回退路径中,经过k次转向后的路径有多少条 输入T个样 ...
- Django ~module index
https://docs.djangoproject.com/en/1.9/py-modindex/ django.conf.urls django.contrib.admin django.db.m ...
- Class和ClassLoader的getResourceAsStream区别
这两个方法还是略有区别的, 以前一直不加以区分,直到今天发现要写这样的代码的时候运行 错误, 才把这个问题澄清了一下. 基本上,两个都可以用于从 classpath 里面进行资源读取, classp ...
- 用户登录流程详解 +volley(StringRequest)
在实习期间由于要求使用volley,所以第一次开始接触volley,从一开始的迷茫陌生,到疯狂的查找各种资料,通过在项目中用到的实际问题,我想做一些总结,所以写了这篇文章.下面我将介绍我理解的用户登录 ...
- POJ 1222 EXTENDED LIGHTS OUT (高斯消元)
题目链接 题意:5*6矩阵中有30个灯,操作一个灯,周围的上下左右四个灯会发生相应变化 即由灭变亮,由亮变灭,如何操作使灯全灭? 题解:这个问题是很经典的高斯消元问题.同一个按钮最多只能被按一次,因为 ...
- 【Excel 4.0 函数】REGISTER
REGISTER.ID 返回指定的 DLL 或 代码资源注册过的函数 ID.如果 DLL 或 代码资源没有注册,这个函数将会注册它们,并返回 注册ID. REGISTER.ID 可以用于工作表(不同于 ...
- Redis事件管理(三)
Redis的事件管理和定时器的管理都是自己来实现的,Redis的事件管理分为两部分,一部分是封装了系统的异步事件API,还有一部分是在这基础上封装了一个通用的事件管理器,根据具体的系统来决定具体使用哪 ...
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(零) 前言
前端时间听一个技术朋友说 LayIM 2.0 发布了,听到这个消息抓紧去官网看了一下.(http://layim.layui.com/)哎呀呀,还要购买授权[大家支持一下哦],果断买了企业版,喜欢钻研 ...