【POJ - 3237】: Tree 树链剖分

传送门

分析

树链剖分写炸了是真的难调

代码

#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

const int N = 1e5 + 10, M = N * 2;
int h[N], e[M], ne[M], w[M], idx;
int id[N], son[N], sz[N], fa[N], dep[N], top[N], son_w[N];
int nw[N];
int n, cnt;
struct edge {
	int x, y;
} ed[M];

struct Node {
	int l, r;
	int mx, mi;
	int flag;
} tr[N * 4];

void init() {
	memset(tr, 0, sizeof tr);
	for (int i = 1; i <= n; i++) h[i] = -1;
	idx = cnt = 0;
}

void add(int x, int y, int z) {
	ne[idx] = h[x], e[idx] = y, w[idx] = z, h[x] = idx++;
}

void dfs(int u, int f, int d) {
	sz[u] = 1, son[u] = 0, fa[u] = f, dep[u] = d;
	for (int i  = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == f) continue;
		dfs(j, u, d + 1);
		sz[u] += sz[j];
		if (sz[j] > sz[son[u]]) {
			son[u] = j;
			son_w[u] = w[i];
		}

	}
}

void dfs_(int u, int t, int res) {
	id[u] = ++cnt, nw[cnt] = res, top[u] = t;
	if (!son[u]) return;
	dfs_(son[u], t, son_w[u]);
	for (int i = h[u]; ~i; i = ne[i]) {
		int j = e[i];
		if (j == fa[u]) continue;
		if (j == son[u]) continue;
		else dfs_(j, j, w[i]);
	}
}

void push(int u) {
	tr[u].mx = max(tr[u << 1].mx, tr[u << 1 | 1].mx);
	tr[u].mi = min(tr[u << 1].mi, tr[u << 1 | 1].mi);
}

void down(int u) {
	if (tr[u].flag) {
		tr[u].flag = 0;
		int x = tr[u << 1].mx, y = tr[u << 1].mi;
		tr[u << 1].mx = -y, tr[u << 1].mi = -x;
		tr[u << 1].flag ^= 1;
		x = tr[u << 1 | 1].mx, y = tr[u << 1 | 1].mi;
		tr[u << 1 | 1].mx = -y, tr[u << 1 | 1].mi = -x;
		tr[u << 1 | 1].flag ^= 1;
	}
}

void build(int u, int l, int r) {
	tr[u].l = l, tr[u].r = r, tr[u].flag = 0;
	if (l == r) {
		tr[u].mx = tr[u].mi = nw[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(u << 1, l, mid), build(u << 1 | 1, mid + 1, r);
	push(u);
}

void modify(int u, int l, int r) {
	if (tr[u].l >= l && tr[u].r <= r) {
		int x = tr[u].mx, y = tr[u].mi;
		tr[u].mx = -y, tr[u].mi = -x;
		tr[u].flag ^= 1;
		return;
	}
	down(u);
	int mid = (tr[u].l + tr[u].r) >> 1;
	if (l <= mid) modify(u << 1, l, r);
	if (r > mid) modify(u << 1 | 1, l, r);
	push(u);
}

int query(int u, int l, int r) {
	if (tr[u].l >= l && tr[u].r <= r) return tr[u].mx;
	down(u);
	int mid = (tr[u].l + tr[u].r) >> 1;
	int res = -0x3f3f3f3f;
	if (l <= mid) res = query(u << 1, l, r);
	if (r > mid) res = max(res, query(u << 1 | 1, l, r));
	return res;
}

void modify_path(int l, int r) {
	while (top[l] != top[r]) {
		if (dep[top[l]] < dep[top[r]]) swap(l, r);
		modify(1, id[top[l]], id[l]);
		l = fa[top[l]];
	}
	if (dep[l] > dep[r]) swap(l, r);
	modify(1, id[son[l]], id[r]);
}

int query_path(int l, int r) {
	int res = -0x3f3f3f3f;
	while (top[l] != top[r]) {
		if (dep[top[l]] < dep[top[r]]) swap(l, r);
		res = max(query(1, id[top[l]], id[l]), res);
		l = fa[top[l]];
	}
	if (dep[l] > dep[r]) swap(l, r);
	res = max(res, query(1, id[son[l]], id[r]));
	return res;
}

void modify(int u, int l, int r, int k) {
	if (tr[u].l == tr[u].r) {
		tr[u].mx = tr[u].mi = k;
		return;
	}
	down(u);
	int mid = (tr[u].l + tr[u].r) >> 1;
	if (l <= mid) modify(u << 1, l, r, k);
	if (r > mid) modify(u << 1 | 1, l, r, k);
	push(u);
}

void modify__(int l, int r, int k) {
	while (top[l] != top[r]) {
		if (dep[l] < dep[r]) swap(l, r);
		modify(1, id[top[l]], id[l], k);
		l = fa[top[l]];
	}
	if (dep[l] > dep[r]) swap(l, r);
	modify(1, id[son[l]], id[r], k);
}

int main() {
	int T;
	scanf("%d", &T);
	while (T--) {
		scanf("%d", &n);
		init();
		for (int i = 1; i < n; i++) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			ed[i].x = a, ed[i].y = b;
			add(a, b, c), add(b, a, c);
		}
		dfs(1, -1, 1);
		dfs_(1, 1, 0);
		build(1, 1, n);
		char op[10];
		while (scanf("%s", op) && *op != 'D') {
			int x, y;
			scanf("%d%d", &x, &y);
			if (*op == 'C') {
				modify__(ed[x].x, ed[x].y, y);
			}
			if (*op == 'N') {
				modify_path(x, y);
			}
			if (*op == 'Q') {
				printf("%d\n", query_path(x, y));
			}
		}
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值