程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2736 Housewife Wind(樹鏈剖分)

POJ 2736 Housewife Wind(樹鏈剖分)

編輯:C++入門知識

POJ 2736 Housewife Wind(樹鏈剖分)


POJ 2763 Housewife Wind

題目鏈接

就樹鏈剖分。。不過這題坑vector啊!, vector居然超時

代碼:

#include 
#include 
#include 
#include 
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;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved