也是入门题,和上一题不一样的是权值在边上。

调了半天后来发现线段树写错了,build的时候没有pushup。。。蠢哭了好吗。。。。

做题还是不专心,太慢辣。。

#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
const int N = ; /**
树链剖分 权值在边上
所以把权值赋给每个边连着的向下的结点 因为每个点有且只有一个父节点(除了根
因为更新的是边,所以对于每一个边记录它对应的点
更新的时候会多更新一个边 因为父节点上边的也会更新到 实际上并不需要
处理方法就是 更新最后一条链的时候 更新从父节点的子节点开始
*/ //
struct Edge {
int to, next, cost, id;
} edge[N*];
int head[N], cntE;
void addedge(int u, int v, int w, int id) {
edge[cntE].to = v; edge[cntE].next = head[u]; edge[cntE].cost = w; edge[cntE].id = id; head[u] = cntE++;
edge[cntE].to = u; edge[cntE].next = head[v]; edge[cntE].cost = w; edge[cntE].id = id; head[v] = cntE++;
}
//
int dep[N], sz[N], fa[N], son[N], son_cost[N];
int road[N];
void dfs1(int u, int par, int d) {
dep[u] = d; sz[u] = ; fa[u] = par;
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v != par) {
road[edge[i].id] = v;
dfs1(v, u, d+);
sz[u] += sz[v];
if (son[u] == - || sz[v] > sz[son[u]]) {
son[u] = v;
son_cost[u] = edge[i].cost;
}
}
}
}
int top[N], dfn[N], rk[N], idx;
int a[N];
void dfs2(int u, int rt, int cost) {
top[u] = rt; dfn[u] = ++idx; a[idx] = cost;
if (son[u] == -) return;
dfs2(son[u], rt, son_cost[u]);
for (int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].to;
if (v != fa[u] && v != son[u]) dfs2(v, v, edge[i].cost);
}
}
//
int tr[N<<];
void build(int o, int l, int r) {
if (l == r) {
tr[o] = a[l];
} else {
int mid = (l+r) >> ;
build(o<<, l, mid);
build(o<<|, mid+, r);
tr[o] = tr[o<<] + tr[o<<|];
}
} void update(int o, int l, int r, int v, int w) {
if (l == r) {
tr[o] = w;
} else {
int mid = (l + r) >> ;
if (mid >= v) update(o<<, l, mid, v, w);
else update(o<<|, mid+, r, v, w);
tr[o] = tr[o<<] + tr[o<<|];
}
} int query(int o, int l, int r, int L, int R) {
if (l >= L && r <= R) return tr[o];
if (l > R && r < L) return ;
int mid = (l+r) >> ;
int ans = ;
if (L <= mid) ans += query(o<<, l, mid, L, R);
if (R > mid) ans += query(o<<|, mid+, r, L, R);
return ans;
} int change(int x, int y, int n) {
int ans = ;
while (top[x] != top[y]) {
if (dep[top[x]] < dep[top[y]]) swap(x, y);
ans += query(, , n, dfn[top[x]], dfn[x]);
x = fa[top[x]];
}
if (x == y) return ans;
if (dep[x] > dep[y]) swap(x, y);
ans += query(, , n, dfn[son[x]], dfn[y]); // 注意这里是son[x]
return ans;
} void init() {
idx = cntE = ;
memset(head, -, sizeof head);
memset(son, -, sizeof son);
} // 单点更新 区间查询
int main() {
int n, q, s;
while (~scanf("%d%d%d", &n, &q, &s)) {
init();
int u, v, w;
int op;
for (int i = ; i < n; ++i) scanf("%d%d%d", &u, &v, &w), addedge(u, v, w, i); dfs1(, , ); dfs2(, , ); build(, , n); while (q--) {
scanf("%d", &op);
if (op == ) {
scanf("%d", &u);
v = s; s = u;
printf("%d\n", change(u, v, n));
} else {
scanf("%d%d", &u, &w);
update(, , n, dfn[road[u]], w);
}
}
}
return ;
}
/**
3 3 1
1 2 1
1 3 2
0 2
1 1 2
0 3 1
4
*/

POJ2763-Housewife Wind(树链剖分)的更多相关文章

  1. POJ2763 Housewife Wind 树链剖分 边权

    POJ2763 Housewife Wind 树链剖分 边权 传送门:http://poj.org/problem?id=2763 题意: n个点的,n-1条边,有边权 修改单边边权 询问 输出 当前 ...

  2. POJ - 2763 Housewife Wind (树链剖分/ LCA+RMQ+树状数组)

    题意:有一棵树,每条边给定初始权值.一个人从s点出发.支持两种操作:修改一条边的权值:求从当前位置到点u的最短路径. 分析:就是在边可以修改的情况下求树上最短路.如果不带修改的话,用RMQ预处理LCA ...

  3. POJ 2763 Housewife Wind (树链剖分 有修改单边权)

    题目链接:http://poj.org/problem?id=2763 n个节点的树上知道了每条边权,然后有两种操作:0操作是输出 当前节点到 x节点的最短距离,并移动到 x 节点位置:1操作是第i条 ...

  4. poj 2763 Housewife Wind : 树链剖分维护边 O(nlogn)建树 O((logn)²)修改与查询

    /** problem: http://poj.org/problem?id=2763 **/ #include<stdio.h> #include<stdlib.h> #in ...

  5. poj 2763 Housewife Wind(树链拆分)

    id=2763" target="_blank" style="">题目链接:poj 2763 Housewife Wind 题目大意:给定一棵 ...

  6. POJ 2763 Housewife Wind 树链拋分

    一.前言 这破题WA了一天,最后重构还是WA,最后通过POJ讨论版得到的数据显示,我看上去是把某个变量写错了..于是,还是低级错误背锅啊....代码能力有待进一步提升2333333 二.题意 某家庭主 ...

  7. POJ2763 Housewife Wind(树剖+线段树)

    After their royal wedding, Jiajia and Wind hid away in XX Village, to enjoy their ordinary happy lif ...

  8. Housewife Wind(边权树链剖分)

    Housewife Wind http://poj.org/problem?id=2763 Time Limit: 4000MS   Memory Limit: 65536K Total Submis ...

  9. POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 )

    POJ.2763 Housewife Wind ( 边权树链剖分 线段树维护区间和 ) 题意分析 给出n个点,m个询问,和当前位置pos. 先给出n-1条边,u->v以及边权w. 然后有m个询问 ...

  10. POJ 2763:Housewife Wind(树链剖分)

    http://poj.org/problem?id=2763 题意:给出 n 个点, n-1 条带权边, 询问是询问 s 到 v 的权值, 修改是修改存储时候的第 i 条边的权值. 思路:树链剖分之修 ...

随机推荐

  1. 转 Android的消息处理机制

    来自:http://blog.csdn.net/andyhuabing/article/details/7368217 Windows编程的朋友可能知道Windows程序是消息驱动的,并且有全局的消息 ...

  2. org.hibernate.PersistentObjectException: detached entity passed to persist异常

    再用jpa+spring+struts2开发的是时候遇到一个问题(采用了注解的方式,xml配置的道理是一样的),当我在注册用户的时候,注册第一个用户没有问题,但注册第二个用户开始就会抛出一个异常: j ...

  3. jquery.post用法

    使用ajax在前端验证是否执行某个操作 jquery.post参数为 url,[data],[callback],[type] url:发送请求地址. data:待发送 Key/value 参数. c ...

  4. HDU4389:X mod f(x)(数位DP)

    Problem Description Here is a function f(x): int f ( int x ) { if ( x == 0 ) return 0; return f ( x ...

  5. 【转载】React初学者入门须知

    http://www.oschina.net/news/75530/9-things-every-reactjs-beginner-should-know react.js入门学习 看了一遍,没什么特 ...

  6. uva live 6170

    Esspe-Peasee Esspe-Peasee is an ancient game played by children throughout the land of Acmania. The ...

  7. I.MX6 linux Qt 同时支持Touch、mouse

    /***************************************************************************** * I.MX6 linux Qt 同时支持 ...

  8. 【 D3.js 高级系列 — 6.0 】 值域和颜色

    在[入门 - 第 10 章]作了一张中国地图,其中各省份的颜色值都是随意赋值的.如果要将一些值反映在地图上,可以利用颜色的变化来表示值的变化. 1. 思路 例如,有值域的范围为: [10, 500] ...

  9. BZOJ3540: [Usaco2014 Open]Fair Photography

    3540: [Usaco2014 Open]Fair Photography Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 29 ...

  10. 【C#学习笔记】List容器使用

    using System; using System.Collections.Generic; namespace ConsoleApplication { class Program { stati ...