题目链接:传送门

题目大意:中文题,略

题目思路:树链剖分(注意要把边上的权值转移到深度较大的点上来维护)

          最后当top[x]==top[y]注意id[x]+1因为是维护的点而题目是边

          如果不+可能会出现重复加的情况。

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <stack>
#include <cctype>
#include <queue>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <climits>
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define fi first
#define se second
#define ping(x,y) ((x-y)*(x-y))
#define mst(x,y) memset(x,y,sizeof(x))
#define mcp(x,y) memcpy(x,y,sizeof(y))
using namespace std;
#define gamma 0.5772156649015328606065120
#define MOD 1000000007
#define inf 0x3f3f3f3f
#define N 50005
#define maxn 30010
typedef pair<int,int> PII;
typedef long long LL;
LL read(){
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=(x<<)+(x<<)+ch-'';ch=getchar();}
return x*f;
}
int head[N],hcnt;
int n,m,flag,L,R;
int son[N],siz[N],fa[N],top[N];
int id[N],tid,dep[N],posi[N];
LL seg[N<<];
struct Node{int to,nxt;LL v;}node[N<<];
struct Edge{int x,y;LL v;}edge[N];
void dfs1(int u,int f,int deep){
fa[u]=f,dep[u]=deep,siz[u]=;
for(int i=head[u];~i;i=node[i].nxt){
int e=node[i].to;if(e==f)continue;
dfs1(e,u,deep+);siz[u]+=siz[e];
if(!son[u]||siz[son[u]]<siz[e])son[u]=e;
}
}
void dfs2(int u,int tp){
top[u]=tp,id[u]=++tid,posi[tid]=u;
if(!son[u])return;dfs2(son[u],tp);
for(int i=head[u];~i;i=node[i].nxt){
int e=node[i].to;
if(!id[e])dfs2(e,e);
}
}
LL query(int rt,int l,int r){
if(L<=l&&r<=R)return seg[rt];
int mid=l+r>>;LL temp=;
if(L<=mid)temp+=query(lson);
if(R>mid) temp+=query(rson);
return temp;
}
void update(int rt,int l,int r,LL v){
if(l==r){seg[rt]=v;return;}
int mid=l+r>>;
if(L<=mid)update(lson,v);
else update(rson,v);
seg[rt]=seg[rt<<]+seg[rt<<|];
}
void lca(int x,int y){
LL res=;
while(top[x]!=top[y]){
if(dep[top[x]]<dep[top[y]])swap(x,y);
L=id[top[x]],R=id[x];
res+=query(,,n);
x=fa[top[x]];
}
if(dep[x]<dep[y])swap(x,y);
L=id[y]+,R=id[x]; ///注意L +1
if(x!=y)res+=query(,,n);
printf("%lld\n",res);
}
void init(){
mst(head,-);hcnt=tid=;
mst(siz,);mst(son,);mst(id,);
}
int main(){
int i,j,group,x,y,v,Case=;
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(i=;i<n;++i){
x=read(),y=read(),v=read();
edge[i].x=x,edge[i].y=y,edge[i].v=v;
node[hcnt].v=v,node[hcnt].to=y,node[hcnt].nxt=head[x],head[x]=hcnt++;
node[hcnt].v=v,node[hcnt].to=x,node[hcnt].nxt=head[y],head[y]=hcnt++;
}
dfs1(,,);dfs2(,);
for(i=;i<n;++i){
if(dep[edge[i].x]<dep[edge[i].y])swap(edge[i].x,edge[i].y);
L=id[edge[i].x];
update(,,n,edge[i].v);
}
while(m--){
x=read();
if(x==){
x=read(),y=read();
L=id[edge[x].x];
update(,,n,y);
}
else{
x=read(),y=read();
lca(x,y);
}
}
}
return ;
}

FZU 2082(过路费)的更多相关文章

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

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

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

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

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

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

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

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

  5. FZU 2082 过路费

    树链剖分模板题 #include <cstdio> #include <iostream> #include <cstring> #include <algo ...

  6. FZU 2082 过路费(树链剖分 边权)题解

    题意:给出每条边权值,可以更新每条边权值,询问两个点路径的最小权值 思路:重链剖分边权化点权,让每个儿子节点继承边权. 插点权的时候比较边的两个节点的深度,插进儿子节点中. 代码: #include& ...

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

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

  8. FZU Problem 2082 过路费 树链剖分

    Problem 2082 过路费    Problem Description 有n座城市,由n-1条路相连通,使得任意两座城市之间可达.每条路有过路费,要交过路费才能通过.每条路的过路费经常会更新, ...

  9. FZU Problem 2082 过路费

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

  10. FZU oj Problem 2082 过路费

                                                                                    Problem 2082 过路费 Pro ...

随机推荐

  1. u-boot可ping通PC,PC不可ping通u-boot

    http://blog.csdn.net/ce123_zhouwei/article/details/7339134 开发板运行U-Boot,在终端下使用Ping命令是能Ping通PC机,但PC机Pi ...

  2. SpringMVC经典系列-15对SpringMVC的总结---【LinusZhu】

    注意:此文章是个人原创,希望有转载须要的朋友们标明文章出处,假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

  3. hibernate、struts、spring mvc的作用

    Hibernate工作原理及为什么要用?原理:1.读取并解析配置文件2.读取并解析映射信息,创建SessionFactory3.打开Sesssion4.创建事务Transation5.持久化操作6.提 ...

  4. 【转】【Java/Android】Intent的简介以及属性的详解

    一.Intent的介绍 Intent的中文意思是“意图,意向”,在Android中提供了Intent机制来协助应用间的交互与通讯,Intent负责对应用中一次操作的动作.动作涉及数据.附加数据进行描述 ...

  5. WinInet 小例子

    #include <windows.h> #include <wininet.h> #include <string> #include <iostream& ...

  6. QTableView修改数据后弹出是否保存的提示框。

    自定义CustomDelegate继承自QStyledItemDelegate,重写setModelData(self, editor, model, index)方法 def setModelDat ...

  7. Making the iPhone vibrate (iPhone 振动)

    from: http://stackoverflow.com/a/4725039 There are two seemingly similar functions that take a param ...

  8. javaEE mvc样例具体解释

    一个不错的样例值得细细品味: 以下依照包顺序将代码贴出来供大家參考: IEmpDAO package org.lzch.dao; import java.util.List; import org.l ...

  9. 在你开发完brew应用之后 ,你又如果将brew应用由编译成可以部署到brew真机上的程序包呢

    参考自:http://blog.csdn.net/feimor/article/details/6239281 一.准备工作(安装工具) 先安装Visual C++ 6.0,再安装BREW SDK v ...

  10. 在程序中使用命令行的方式来调用py文件

    做这个主要是程序可以做到直接调用一个脚本,而不是从脚本中把类或者函数import出来这样调用,比如我们写的python命令行文件,让java来调用,让c++来调用,都是可以的.这样不需要整个语言都用p ...