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. 二进制部署Kubernetes-v1.14.1集群

    一.部署Kubernetes集群 1.1 Kubernetes介绍 Kubernetes(K8S)是Google开源的容器集群管理系统,K8S在Docker容器技术的基础之上,大大地提高了容器化部署应 ...

  2. ROS-URDF-建立模型

    前言:建立一个简单的urdf模型 详解请参看教程http://wiki.ros.org/urdf/Tutorials/Building%20a%20Visual%20Robot%20Model%20w ...

  3. STL之map篇

    度熊所居住的 D 国,是一个完全尊重人权的国度.以至于这个国家的所有人命名自己的名字都非常奇怪.一个人的名字由若干个字符组成,同样的,这些字符的全排列的结果中的每一个字符串,也都是这个人的名字.例如, ...

  4. YOLO训练Pedestrain

    Pedestrain dl  使用darknet训练: 1. Inria 创建 yolo-inria.cfg 从cfg/yolo-voc.2.0.cfg拷贝一份,修改batch=64, subdivi ...

  5. C# 查找、结束进程 - 通过进程名精确、模糊查找、结束进程

    /// <summary> /// 根据“精确进程名”结束进程 /// </summary> /// <param name="strProcName" ...

  6. ROS:ubuntu-Ros使用OrbSLAM

    一般无误的官方连接:https://github.com/raulmur/ORB_SLAM ubuntu16.04没有多少改变,还是使用kinetic老代替indigo Related Publica ...

  7. Linux 之WinSCP连接FTP

    1.安装vsftpd 2.ftp命令 /sbin/service vsftpd start /sbin/service vsftpd restart /sbin/service vsftpd stop ...

  8. Dart 调用C语言混合编程

    Dart 调用C语言本篇博客研究Dart语言如何调用C语言代码混合编程,最后我们实现一个简单示例,在C语言中编写简单加解密函数,使用dart调用并传入字符串,返回加密结果,调用解密函数,恢复字符串内容 ...

  9. eas之控制kdtable滚动条

    //滚动条支持三种状态 自动 隐藏 显示 public static final int SCROLL_STATE_AUTO=0://自动根据数据判断是否显示或隐藏 public static fin ...

  10. 关于node对文件的读取

    设计: 通过终端git / cmd 获取用户输入路径,然后遍历路径下所有的文件,打印输出. 因为需要命令行交互,所以引入prompt库 (https://github.com/flatiron/pro ...