POJ 2763 Housewife Wind (树链剖分 有修改单边权)
题目链接:http://poj.org/problem?id=2763
n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置;1操作是第i条边的边权变成x。
树链边权剖分的模版题,修改单边权和求和。
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN = 1e5 + ;
struct EDGE {
int next, to, cost;
}edge[MAXN << ];
int head[MAXN], tot;
int from[MAXN], to[MAXN], cost[MAXN];
int par[MAXN], dep[MAXN], son[MAXN], size[MAXN];
int top[MAXN], id[MAXN], cnt; void init() {
cnt = tot = ;
memset(head, -, sizeof(head));
} inline void add(int u, int v, int cost) {
edge[tot].next = head[u];
edge[tot].to = v;
head[u] = tot++;
} void dfs1(int u, int p, int d) {
dep[u] = d, par[u] = p, size[u] = , son[u] = u;
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == p)
continue;
dfs1(v, u, d + );
if(size[v] >= size[son[u]])
son[u] = v;
size[u] += size[v];
}
} void dfs2(int u, int p, int t) {
top[u] = t, id[u] = ++cnt;
if(son[u] != u)
dfs2(son[u], u, t);
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if(v == son[u] || v == p)
continue;
dfs2(v, u, v);
}
} struct segtree {
int l, r, val;
}T[MAXN << ]; void build(int p, int l, int r) {
int mid = (l + r) >> ;
T[p].l = l , T[p].r = r;
if(l == r) {
return ;
}
build(p << , l, mid);
build((p << )|, mid + , r);
} void updata(int p, int pos, int num) {
int mid = (T[p].l + T[p].r) >> ;
if(T[p].l == T[p].r && T[p].l == pos) {
T[p].val = num;
return ;
}
if(pos <= mid) {
updata(p << , pos, num);
}
else {
updata((p << )|, pos, num);
}
T[p].val = T[p << ].val + T[(p << )|].val;
} int query(int p, int l, int r) {
int mid = (T[p].l + T[p].r) >> ;
if(l == T[p].l && T[p].r == r) {
return T[p].val;
}
if(r <= mid) {
return query(p << , l, r);
}
else if(l > mid) {
return query((p << )|, l, r);
}
else {
return query(p << , l, mid) + query((p << )|, mid + , r);
}
} int find(int u, int v) {
int fu = top[u], fv = top[v], res = ;
while(fv != fu) {
if(dep[fu] > dep[fv]) {
res += query(, id[fu], id[u]);
u = par[fu];
fu = top[u];
}
else {
res += query(, id[fv], id[v]);
v = par[fv];
fv = top[v];
}
}
if(v == u)
return res;
else if(dep[u] > dep[v])
return res + query(, id[son[v]], id[u]);
else
return res + query(, id[son[u]], id[v]);
} int main()
{
int n, m, root;
while(~scanf("%d %d %d", &n, &m, &root)) {
init();
for(int i = ; i < n; ++i) {
scanf("%d %d %d", from + i, to + i, cost + i);
add(from[i], to[i], cost[i]);
add(to[i], from[i], cost[i]);
}
dfs1(, , );
dfs2(, , );
build(, , cnt);
for(int i = ; i < n; ++i) {
if(dep[from[i]] < dep[to[i]])
swap(from[i], to[i]);
updata(, id[from[i]], cost[i]);
}
int op, pos, num;
while(m--) {
scanf("%d", &op);
if(op) {
scanf("%d %d", &pos, &num);
updata(, id[from[pos]], num);
}
else {
scanf("%d", &pos);
printf("%d\n", find(root, pos));
root = pos;
}
}
}
}
POJ 2763 Housewife Wind (树链剖分 有修改单边权)的更多相关文章
- POJ - 2763 Housewife Wind (树链剖分/ LCA+RMQ+树状数组)
题意:有一棵树,每条边给定初始权值.一个人从s点出发.支持两种操作:修改一条边的权值:求从当前位置到点u的最短路径. 分析:就是在边可以修改的情况下求树上最短路.如果不带修改的话,用RMQ预处理LCA ...
- poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询
/** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...
- poj 2763 Housewife Wind(树链拆分)
id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...
- POJ 2763 Housewife Wind 树链拋分
一.前言 这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了..于是,还是低级错误背锅啊....代码能力有待进一步提升2333333 二.题意 某家庭主 ...
- POJ2763 Housewife Wind 树链剖分 边权
POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...
- poj 2763(RMQ+BIT\树链剖分)
传送门:Problem 2763 https://www.cnblogs.com/violet-acmer/p/9686774.html 题意: 一对夫妇居住在xx村庄,小屋之间有双向可达的道路,不会 ...
- POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )
POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...
- POJ 2763 Housewife Wind LCA转RMQ+时间戳+线段树成段更新
题目来源:POJ 2763 Housewife Wind 题意:给你一棵树 2种操作0 x 求当前点到x的最短路 然后当前的位置为x; 1 i x 将第i条边的权值置为x 思路:树上两点u, v距离为 ...
- poj 2763 Housewife Wind (树链剖分)
题目链接:http://poj.org/problem?id=2763 题意: 给定一棵含n个结点的树和树的边权,共有q次操作,分为两种 0 c :求从位置s到c的距离,然后s变成c 1 a b:把第 ...
随机推荐
- git cheat sheet,git四张手册图
- 函数buf_page_init
/********************************************************************//** Inits a page to the buffer ...
- JAVA将Excel中的报表导出为图片格式(三)换一种实现
上一篇介绍了使用Java的Robot机器人实现截图,然后将剪贴板上的数据流生成PNG图片 但是经过博主的不断测试,在完全依赖远程桌面的没有终端显示器的服务器上 使用截图方式是不可行的,因为一旦使用了远 ...
- UVa 11330 (置换 循环的分解) Andy's Shoes
和UVa11077的分析很类似. 我们固定左脚的鞋子不动,然后将右脚的鞋子看做一个置换分解. 对于一个长度为l的循环节,要交换到正确位置至少要交换l-1次. #include <cstdio&g ...
- 转:MVC 下导航超链接本页面高亮的一种解决方案
前言 导航高亮一直是一个让大家头疼的问题. 纯 Javascript 的话可以判断当前页面的地址和链接地址是否有关系. 这样的弊端就是自由度太低,MVC 下会出一定的问题 (MVC 下有默认的 Con ...
- 【C#学习笔记】播放wma/mp3文件
using System; using System.Runtime.InteropServices; namespace ConsoleApplication { class Program { [ ...
- 业界最具影响力MySQL精品文章荟萃(300篇)
MySQL是一种关联数据库管理系统,SQL语言是用于访问数据库的最常用标准化语言.本文档收集的资料有MySQL数据库备份与恢复,配置,解决方案等,供大家方便统一阅读. 博客专题 1 MySQL ...
- yum install错误 系统环境:Oracle Linux5.4 在通过yum安装软件时出现以下错误:
1.yum配置文件 1 [root@rh168 yum.repos.d]# cat yum.repo 2 [base] 3 name=Oracle linux 4 baseurl=file:/// ...
- jquery加入购物车飞入的效果
主要原理是:点击当前图片的时候,复制(克隆)当前图片在当前位置,然后利用jQuery的animate()方法实现图像的飞入效果 效果预览:http://runjs.cn/detail/qmf0mtm1 ...
- information_schema中的三个关于锁的表
在5.5中,information_schema 库中增加了三个关于锁的表(MEMORY引擎):innodb_trx ## 当前运行的所有事务innodb_locks ## ...