POJ 3237 Tree 树链剖分

单点替换,区间取相反数,区间求最大值,其实线段树里面只要保存最小值和最大值就可以了,取反之后最小值变成最大值最大值变成最小值。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <set>
#include <bitset>
#include <queue>
#include <stack>
#include <string>
#include <iostream>
#include <cmath>
#include <climits>

using namespace std;
const int maxn = 10000 + 5;
int n, head[maxn], nxt[maxn << 1], u[maxn << 1], v[maxn << 1], w[maxn << 1];
int tid[maxn], maxson[maxn], fa[maxn], dep[maxn], siz[maxn], idcnt, ecnt;
int top[maxn], maxv[maxn << 2], minv[maxn << 2], sval[maxn], lazy[maxn << 2];
int eid[maxn << 1], rnum[maxn];

#define lson rt << 1, l, mid
#define rson rt << 1 | 1, mid + 1, r

void init() {
	memset(head, -1, sizeof(head));
	memset(maxson, -1, sizeof(maxson));
	ecnt = idcnt = 0;
}

void adde(int uu, int vv, int ww, int id) {
	u[ecnt] = uu; v[ecnt] = vv; w[ecnt] = ww; eid[ecnt] = id;
	nxt[ecnt] = head[uu]; head[uu] = ecnt++;
}

void dfs1(int now, int nowfa, int nowdep) {
	fa[now] = nowfa; dep[now] = nowdep;
	siz[now] = 1;
	for(int i = head[now]; ~i; i = nxt[i]) if(v[i] != nowfa) {
		dfs1(v[i], now, nowdep + 1);
		siz[now] += siz[v[i]];
		if(maxson[now] == -1 || siz[v[i]] > siz[maxson[now]]) {
			maxson[now] = v[i];
		}
	}
}

void dfs2(int now, int tp) {
	top[now] = tp;
	tid[now] = ++idcnt;
	if(maxson[now] == -1) return;
	dfs2(maxson[now], tp);
	for(int i = head[now]; ~i; i = nxt[i]) {
		if(v[i] != fa[now] && v[i] != maxson[now]) {
			dfs2(v[i], v[i]);
		}
	}
}

void pushup(int rt) {
	minv[rt] = min(minv[rt << 1], minv[rt << 1 | 1]);
	maxv[rt] = max(maxv[rt << 1], maxv[rt << 1 | 1]);
}

void pushdown(int rt) {
	if(lazy[rt] == 0) return;
	int lc = rt << 1, rc = rt << 1 | 1;
	lazy[lc] ^= lazy[rt]; lazy[rc] ^= lazy[rt];
	swap(minv[lc], maxv[lc]); swap(minv[rc], maxv[rc]);
	minv[lc] = -minv[lc]; minv[rc] = -minv[rc];
	maxv[lc] = -maxv[lc]; maxv[rc] = -maxv[rc];
	lazy[rt] = 0;
}

void build(int rt, int l, int r) {
	int mid = (l + r) >> 1;
	lazy[rt] = 0;
	if(l == r) {
		minv[rt] = maxv[rt] = sval[l];
	}
	else {
		build(lson); build(rson);
		pushup(rt);
	}
}

void change(int rt, int l, int r, int pos, int val) {
	if(l == r) {
		minv[rt] = maxv[rt] = val;
	}
	else {
		int mid = (l + r) >> 1;
		pushdown(rt);
		if(pos <= mid) change(lson, pos, val);
		else change(rson, pos, val);
		pushup(rt);
	}
}

void update(int rt, int l, int r, int ql, int qr) {
	if(ql <= l && qr >= r) {
		swap(minv[rt], maxv[rt]);
		minv[rt] = -minv[rt]; maxv[rt] = -maxv[rt];
		lazy[rt] ^= 1;
	}
	else {
		int mid = (l + r) >> 1;
		pushdown(rt);
		if(ql <= mid) update(lson, ql, qr);
		if(qr > mid) update(rson, ql, qr);
		pushup(rt);
	}
}

int query(int rt, int l, int r, int ql, int qr) {
	if(ql <= l && qr >= r) return maxv[rt];
	int mid = (l + r) >> 1, ret = -1e9;
	pushdown(rt);
	if(ql <= mid) ret = max(ret, query(lson, ql, qr));
	if(qr > mid) ret = max(ret, query(rson, ql, qr));
	pushup(rt);
	return ret;
}

int ask(int x, int y) {
	int ret = -1e9;
	while(top[x] != top[y]) {
		if(dep[top[x]] < dep[top[y]])
			swap(x, y);
		ret = max(ret, query(1, 1, idcnt, tid[top[x]], tid[x]));
		x = fa[top[x]];
	}
	if(dep[x] > dep[y]) swap(x, y);
	if(x != y) ret = max(ret, query(1, 1, idcnt, tid[x] + 1, tid[y]));
	return ret;
}

void Negate(int x, int y) {
	while(top[x] != top[y]) {
		if(dep[top[x]] < dep[top[y]]) swap(x, y);
		update(1, 1, idcnt, tid[top[x]], tid[x]);
		x = fa[top[x]];
	}
	if(dep[x] > dep[y]) swap(x, y);
	if(x != y) update(1, 1, idcnt, tid[x] + 1, tid[y]);
}

void debug() {
	for(int i = 1; i <= n; i++) {
		printf("for %d top is %d tid is %d val is %d\n",
				i, top[i], tid[i], sval[tid[i]]);
	}
	printf("the maxv: ");
	for(int i = 1; i <= n << 2; i++) printf("%d ", maxv[i]);
	puts("");
}

int main() {
	int T; scanf("%d", &T);
	while(T--) {
		init();
		scanf("%d", &n);
		for(int i = 1; i < n; i++) {
			int a, b, c;
			scanf("%d%d%d", &a, &b, &c);
			adde(a, b, c, i); adde(b, a, c, i);
		}
		dfs1(1, 1, 1); dfs2(1, 1);
		for(int i = 0; i < ecnt; i++) {
			if(dep[u[i]] > dep[v[i]]) {
				sval[tid[u[i]]] = w[i];
				rnum[eid[i]] = tid[u[i]];
			}
			else {
				sval[tid[v[i]]] = w[i];
				rnum[eid[i]] = tid[v[i]];
			}
		}
		build(1, 1, idcnt);
		//debug();
		char cmd[16];
		int x, y;
		while(scanf("%s", cmd)) {
			if(strcmp(cmd, "DONE") == 0) break;
			scanf("%d%d", &x, &y);
			if(cmd[0] == ‘Q‘) {
				printf("%d\n", ask(x, y));
			}
			else if(cmd[0] == ‘C‘) {
				change(1, 1, idcnt, rnum[x], y);
			}
			else {
				Negate(x, y);
			}
		}
	}
	return 0;
}

  

时间: 2024-12-15 02:46:30

POJ 3237 Tree 树链剖分的相关文章

POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)

题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每次更新异或1就可以. 熟悉线段树成段更新就很简单了,最初姿势不对一直wa,还是没有彻底理解lazy标记啊. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace st

poj 3237 Tree 树链剖分+线段树

Description You are given a tree with N nodes. The tree's nodes are numbered 1 through N and its edges are numbered 1 through N ? 1. Each edge is associated with a weight. Then you are to execute a series of instructions on the tree. The instructions

poj 3237 Tree(树链拆分)

题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询ab路径上节点权值的最大值. 解题思路:树链剖分.然后用线段树维护节点权值,成端更新查询. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; const int max

SPOJ375 Query on a tree 树链剖分

SPOJ375  Query on a tree   树链剖分 no tags You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of

SPOJ - QTREE 375 Query on a tree 树链剖分+线段树

操作1:修改第k条边权. 操作2:询问两点间最大边权. 树链剖分,然后线段树维护最大值 #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm> #include<set> #include<map> #include<queue> #include<vector> #inclu

Aizu 2450 Do use segment tree 树链剖分+线段树

Do use segment tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.bnuoj.com/v3/problem_show.php?pid=39566 Description Given a tree with n (1 ≤ n ≤ 200,000) nodes and a list of q (1 ≤ q ≤ 100,000) queries, process the queries in order and out

spoj 375 Query on a tree (树链剖分)

Query on a tree You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to ti or Q

SPOJ Query on a tree 树链剖分 水题

You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, 3...N-1. We will ask you to perfrom some instructions of the following form: CHANGE i ti : change the cost of the i-th edge to tior QUERY a b : ask fo

hdu 4912 Paths on the tree(树链剖分+贪心)

题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道,要求尽量选出多的通道,并且两两通道不想交. 解题思路:用树链剖分求LCA,然后根据通道两端节点的LCA深度排序,从深度最大优先选,判断两个节点均没被标 记即为可选通道.每次选完通道,将该通道LCA以下点全部标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include