首页 > 代码库 > POJ 3723 Tree(树链剖分)
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; }
POJ 3723 Tree(树链剖分)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。