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(树链剖分)的更多相关文章

  1. poj 3237 Tree 树链剖分

    题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...

  2. POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12247   Accepted: 3151 Descriptio ...

  3. poj 3237 Tree 树链剖分+线段树

    Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...

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

    题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...

  5. POJ3237 Tree 树链剖分 边权

    POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...

  6. Hdu 5274 Dylans loves tree (树链剖分模板)

    Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...

  7. poj 3237 Tree(树链拆分)

    题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...

  8. Query on a tree——树链剖分整理

    树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...

  9. 【BZOJ-4353】Play with tree 树链剖分

    4353: Play with tree Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 31  Solved: 19[Submit][Status][ ...

  10. SPOJ Query on a tree 树链剖分 水题

    You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...

随机推荐

  1. 字符串转为JSON对象

    经常写字符串转为JSON对象,但是每次没有说一次就成功的,老是搞错属于哪个包的方法,遂记录一下 JSONObject.parseObject(str);这个方法需要导入包 com.alibaba.fa ...

  2. 数据通讯与网络 第五版第24章 传输层协议-TCP协议部分要点

    上一博客记录了UDP协议的关键要点,这部分记录TCP协议的关键要点. 24.3 传输控制协议(TRANSMISSION CONTROL PROTOCOL) TCP(Transmission Contr ...

  3. 基于NPOI的扩展

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.HSS ...

  4. [转]Oracle 存储过程语法

    转自:http://www.cnblogs.com/chuncn/archive/2009/04/29/1381282.html 存储过程 1  CREATE OR REPLACE PROCEDURE ...

  5. HTML+CSS(11)

    n  CSS背景属性 Background-color:背景色. Background-image:背景图片地址.如:background-image:url(images/bg.gif;) Back ...

  6. Ad hoc polymorphism

    与面向对象中的接口类或抽象类中定义的函数组类似: 函数的具体执行依赖与函数医用的类型. In programming languages, ad-hoc polymorphism[1] is a ki ...

  7. CorelDRAW中内置的视频教程在哪里?

    CorelDRAW中内置了很多教学内容和视频教程,可以帮助用户快速学习和掌握CorelDRAW的使用方法,创作出个性化的作品.很多小伙伴表示找不到软件自带学习视频,现在小编就来告诉你. 用户可以通过两 ...

  8. console.log、toString方法与js判断变量类型

    Java调用system.print.out()是会调用toString方法打印js里的console.log也是控制台打印,很多时候,我们以为也是调用toString方法,其实并不是.我们在chro ...

  9. grep命令总结

    grep (缩写来自Globally search a Regular Expression and Print)是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹 ...

  10. Uoj #274. 【清华集训2016】温暖会指引我们前行 LCT维护边权_动态最小生成树

    Code: 行#include<bits/stdc++.h> #define ll long long #define maxn 1000000 #define inf 100000000 ...