POJ 3723 Tree(树链剖分)

POJ 3237 Tree

题目链接

就多一个取负操作,所以线段树结点就把最大和最小值存下来,每次取负的时候,最大和最小值取负后,交换即可

代码:

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

const int N = 10005;
const int INF = 0x3f3f3f3f;
int dep[N], fa[N], son[N], sz[N], top[N], id[N], idx, val[N];
int first[N], next[N * 2], vv[N * 2], en;

struct Edge {
	int u, v, val;
	Edge() {}
	Edge(int u, int v, int val) {
		this->u = u;
		this->v = v;
		this->val = val;
	}
} e[N];

void init() {
	en = 0;
	idx = 0;
	memset(first, -1, sizeof(first));
}

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;
	sz[u] = 1;
	fa[u] = f;
	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) {
	id[u] = ++idx;
	top[u] = tp;
	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);
	}
}

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

struct Node {
	int l, r, Max, Min;
	bool neg;
} node[N * 4];

void pushup(int x) {
	node[x].Max = max(node[lson(x)].Max, node[rson(x)].Max);
	node[x].Min = min(node[lson(x)].Min, node[rson(x)].Min);
}

void pushdown(int x) {
	if (node[x].neg) {
		node[lson(x)].Max = -node[lson(x)].Max;
		node[rson(x)].Max = -node[rson(x)].Max;
		node[lson(x)].Min = -node[lson(x)].Min;
		node[rson(x)].Min = -node[rson(x)].Min;
		swap(node[lson(x)].Max, node[lson(x)].Min);
		swap(node[rson(x)].Max, node[rson(x)].Min);
		node[lson(x)].neg = !node[lson(x)].neg;
		node[rson(x)].neg = !node[rson(x)].neg;
		node[x].neg = false;
	}
}

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

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

void negate(int l, int r, int x = 0) {
	if (node[x].l >= l && node[x].r <= r) {
		node[x].neg = !node[x].neg;
		node[x].Max = -node[x].Max;
		node[x].Min = -node[x].Min;
		swap(node[x].Max, node[x].Min);
		return;
	}
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	if (l <= mid) negate(l, r, lson(x));
	if (r > mid) negate(l, r, rson(x));
	pushup(x);
}

int query(int l, int r, int x = 0) {
	if (node[x].l >= l && node[x].r <= r) {
		return node[x].Max;
	}
	int mid = (node[x].l + node[x].r) / 2;
	pushdown(x);
	int ans = -INF;
	if (l <= mid) ans = max(ans, query(l, r, lson(x)));
	if (r > mid) ans = max(ans, query(l, r, rson(x)));
	pushup(x);
	return ans;
}

void gao1(int u, int v) {
	int tp1 = top[u], tp2 = top[v];
	while (tp1 != tp2) {
		if (dep[tp1] < dep[tp2]) {
			swap(tp1, tp2);
			swap(u, v);
		}
		negate(id[tp1], id[u]);
		u = fa[tp1];
		tp1 = top[u];
	}
	if (u == v) return;
	if (dep[u] > dep[v]) swap(u, v);
	negate(id[son[u]], id[v]);
}

int gao2(int u, int v) {
	int ans = -INF;
	int tp1 = top[u], tp2 = top[v];
	while (tp1 != tp2) {
		if (dep[tp1] < dep[tp2]) {
			swap(tp1, tp2);
			swap(u, v);
		}
		ans = max(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 = max(ans, query(id[son[u]], id[v]));
	return ans;
}

int T, n;

int main() {
	scanf("%d", &T);
	while (T--) {
		init();
		scanf("%d", &n);
		int u, v, w;
		for (int i = 1; i < n; i++) {
			scanf("%d%d%d", &u, &v, &w);
			add_Edge(u, v);
			add_Edge(v, u);
			e[i] = Edge(u, v, w);
		}
		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);
		char Q[10];
		int a, b;
		while (scanf("%s", Q)) {
			if (Q[0] == 'D') break;
			scanf("%d%d", &a, &b);
			if (Q[0] == 'Q') printf("%d\n", gao2(a, b));
			if (Q[0] == 'C') change(id[e[a].u], b);
			if (Q[0] == 'N') gao1(a, b);
		}
	}
	return 0;
}
时间: 2024-10-20 12:15:13

POJ 3723 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 树链剖分

单点替换,区间取相反数,区间求最大值,其实线段树里面只要保存最小值和最大值就可以了,取反之后最小值变成最大值最大值变成最小值. #include <cstdio> #include <cstring> #include <algorithm> #include <map> #include <set> #include <bitset> #include <queue> #include <stack> #in

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

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

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