码迷,mamicode.com
首页 > Windows程序 > 详细

POJ 2736 Housewife Wind(树链剖分)

时间:2014-09-30 22:23:30      阅读:363      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   io   os   ar   for   sp   c   

POJ 2763 Housewife Wind

题目链接

就树链剖分。。不过这题坑ector啊!, vector居然超时

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;

#define lson(x) ((x<<1)+1)
#define rson(x) ((x<<1)+2)

typedef long long ll;
const int N = 100005;

int dep[N], sz[N], son[N], fa[N], top[N], id[N], idx, val[N];

int n, q, s;
struct Edge {
	int u, v, val;
	void read() {
		scanf("%d%d%d", &u, &v, &val);
	}
} e[N];

int first[N], next[N * 2], vv[N * 2], en;

void add_Edge(int u, int v) {
	vv[en] = v;
	next[en] = first[u];
	first[u] = en++;
}

void dfs1(int u, int f, int d) {
	dep[u] = d;
	fa[u] = f;
	sz[u] = 1;
	son[u] = 0;
	for (int i = first[u]; i + 1; i = next[i]) {
		int v = vv[i];
		if (v == f) continue;
		dfs1(v, u, d + 1);
		sz[u] += sz[v];
		if (sz[son[u]] < sz[v])
			son[u] = v;
	}
}

void dfs2(int u, int tp) {
	top[u] = tp;
	id[u] = ++idx;
	if (son[u]) dfs2(son[u], tp);
	for (int i = first[u]; i + 1; i = next[i]) {
		int v = vv[i];
		if (v == fa[u] || v == son[u]) continue;
		dfs2(v, v);
	}
}

struct Node {
	int l, r;
	ll v;
} node[N * 4];

void pushup(int x) {
	node[x].v = node[lson(x)].v + node[rson(x)].v;
}

void build(int l, int r, int x = 0) {
	if (l > r) return;
	node[x].l = l; node[x].r = r;
	if (l == r) {
		node[x].v = val[l];
		return;
	}
	int mid = (l + r) / 2;
	build(l, mid, lson(x));
	build(mid + 1, r, rson(x));
	pushup(x);
}

void add(int v, int val, int x = 0) {
	if (node[x].l == node[x].r) {
		node[x].v = val;
		return;
	}
	int mid = (node[x].l + node[x].r) / 2;
	if (v <= mid) add(v, val, lson(x));
	if (v > mid) add(v, val, rson(x));
	pushup(x);
}

ll query(int l, int r, int x = 0) {
	if (node[x].l >= l && node[x].r <= r) {
		return node[x].v;
	}
	int mid = (node[x].l + node[x].r) / 2;
	ll ans = 0;
	if (l <= mid) ans += query(l, r, lson(x));
	if (r > mid) ans += query(l, r, rson(x));
	return ans;
}

ll gao(int u, int v) {
	int tp1 = top[u], tp2 = top[v];
	ll ans = 0;
	while (tp1 != tp2) {
		if (dep[tp1] < dep[tp2]) {
			swap(tp1, tp2);
			swap(u, v);
		}
		ans += query(id[tp1], id[u]);
		u = fa[tp1];
		tp1 = top[u];
	}
	if (u == v) return ans;
	if (dep[u] > dep[v]) swap(u, v);
	ans += query(id[son[u]], id[v]);
	return ans;
}

int main() {
	while (~scanf("%d%d%d", &n, &q, &s)) {
		idx = -1;
		en = 0;
		memset(first, -1, sizeof(first));
		for (int i = 1; i < n; i++) {
			e[i].read();
			add_Edge(e[i].u, e[i].v);
			add_Edge(e[i].v, e[i].u);
		}
		dfs1(1, 0, 1);
		dfs2(1, 1);
		for (int i = 1; i < n; i++) {
			if (dep[e[i].u] < dep[e[i].v]) swap(e[i].u, e[i].v);
			val[id[e[i].u]] = e[i].val;
		}
		build(1, idx);
		int ty, a, b;
		while (q--) {
			scanf("%d%d", &ty, &a);
			if (ty == 0) {
				printf("%lld\n", gao(s, a));
				s = a;
			} else {
				scanf("%d", &b);
				add(id[e[a].u], b);
			}
		}
	}
	return 0;
}


POJ 2736 Housewife Wind(树链剖分)

标签:style   http   color   io   os   ar   for   sp   c   

原文地址:http://blog.csdn.net/accelerator_/article/details/39699325

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!