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 ...
随机推荐
- yum安装所需要的开发库
yum groupinstall "Development tools" -y yum install zlib-devel bzip2-devel openssl-devel n ...
- Delphi 多步操作产生错误,请检查每一步的状态值
需检查是否是以下这些情况: 1. 字段是不是精度不够. 2. 无主键 3. 字段允许为空 4. 字段类型不匹配 5. ADO控件 CursorLocation的属性,默认值为"cl ...
- ACM/ICPC 之 DFS范例(ZOJ2412-ZOJ1008)
通过几道例题简单阐述一下DFS的相关题型 ZOJ2412-Farm Irrigation 直观的DFS题型,稍加变化,记录好四个方向上的通路就能够做出来 题目和接水管类似,问最少要灌溉几次,即求解最少 ...
- MySQL 5.6 my.cnf 模版
[client] port = socket = /var/run/mysqld/mysqld.sock [mysqld_safe] thp-setting=never socket = /var/r ...
- ffmpeg-20160325-snapshot-static-bin
ffmpeg-20160325-snapshot-static.7z ./configure \ --enable-static \ --disable-shared \ --enable-gpl \ ...
- a byte of python(摘04)
a byte of python 第十章 输入/输出 如何使程序和用户进行交互?(用 raw_input 和 print语句来完成) 对于输出,你也可以使用多种多样的 str(字符串)类.使用 rju ...
- Effective C++ -----条款13:以对象管理资源
为防止资源泄漏,请使用RAII(Resource Acquisiton Is Initialization) 对象,它们在构造函数中获得资源并在析构函数中释放资源. 两个常被使用的RAII class ...
- windows服务
.net windows 服务创建.安装.卸载和调试 原文:http://www.cnblogs.com/hfliyi/archive/2012/08/12/2635290.html 我对例子做了 ...
- ajax传值方式为数组
js: function responseJson1(){ var array=[1001,1002]; var str=""; //获取table对象 ...
- WinForm相关注意点
1. //this.dgvEmployees.ColumnHeadersDefaultCellStyle.ForeColor = Color.Blue; //dgvEmployees.RowHeade ...