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(树链剖分)的更多相关文章
- poj 3237 Tree 树链剖分
题目链接:http://poj.org/problem?id=3237 You are given a tree with N nodes. The tree’s nodes are numbered ...
- POJ 3237.Tree -树链剖分(边权)(边值更新、路径边权最值、区间标记)贴个板子备忘
Tree Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12247 Accepted: 3151 Descriptio ...
- poj 3237 Tree 树链剖分+线段树
Description You are given a tree with N nodes. The tree’s nodes are numbered 1 through N and its edg ...
- POJ 3237 Tree (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...
- POJ3237 Tree 树链剖分 边权
POJ3237 Tree 树链剖分 边权 传送门:http://poj.org/problem?id=3237 题意: n个点的,n-1条边 修改单边边权 将a->b的边权取反 查询a-> ...
- Hdu 5274 Dylans loves tree (树链剖分模板)
Hdu 5274 Dylans loves tree (树链剖分模板) 题目传送门 #include <queue> #include <cmath> #include < ...
- poj 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- Query on a tree——树链剖分整理
树链剖分整理 树链剖分就是把树拆成一系列链,然后用数据结构对链进行维护. 通常的剖分方法是轻重链剖分,所谓轻重链就是对于节点u的所有子结点v,size[v]最大的v与u的边是重边,其它边是轻边,其中s ...
- 【BZOJ-4353】Play with tree 树链剖分
4353: Play with tree Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 31 Solved: 19[Submit][Status][ ...
- SPOJ Query on a tree 树链剖分 水题
You are given a tree (an acyclic undirected connected graph) with N nodes, and edges numbered 1, 2, ...
随机推荐
- php统计网站 / html页面 浏览访问次数程序
本文章来给大这介绍了php自己写的一些常用的网站统计代码写法,用无数据库的与使用数据库及html静态页面浏览资次数统计代码,大家可进入参考. 实例1 直接使用txt文件进行统计的代码 <?php ...
- 全文检索引擎及工具 Lucene Solr
全文检索引擎及工具 lucence lucence是一个全文检索引擎. lucence代码级别的使用步骤大致如下: 创建文档(org.apache.lucene.document.Document), ...
- 【PostgreSQL-9.6.3】log参数的设置
编辑数据目录中的postgresql.conf参数文件,我的数据目录是/usr/local/pgsql/data vi postgresql.conf 找到如下内容: ... #----------- ...
- CVPR2015深度学习回顾
原文链接:http://www.csdn.net/article/2015-08-06/2825395 本文做了少量修改,仅作转载存贮,如有疑问或版权问题,请访问原作者或告知本人. CVPR可谓计算机 ...
- asp.net mvc学习入门
MVC是什么? M: Model就是我们获取的网页需要的数据 V: View就是我们的aspx页面,注意这是一个不包含后台代码文件的aspx页面.(其实带有.asp.cs文件也不会有编译错误,但是这样 ...
- 【sqli-labs】 less5 GET - Double Injection - Single Quotes - String (双注入GET单引号字符型注入)
双注入查询可以查看这两篇介绍 https://www.2cto.com/article/201302/190763.html https://www.2cto.com/article/201303/1 ...
- 针对Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1的解决方案
背景:本项目使用JDK1.8 编译maven工程的时候出现如下错误: Failed to execute goal org.apache.maven.plugins:maven-compiler-pl ...
- swiper和Navigator组件
<swiper class="index-banner" indicator-dots="{{true}}" autoplay="{{true} ...
- 用了那么多项目管理工具,还是CORNERSTONE这款最好用
在与软件开发有关的项目,往往会出现很难管理情况.许多事情都需提前计划.控制与管理,所以许多项目经理很容易迷失在计划的过程中.幸运的是,市场上提供了各种各样的项目管理工具.但不幸的是,工具实在是太多了. ...
- case when用法小结
case 对比字段 when 值 then 输出结果 when 值 then 输出结果 ....else 输出结果 end 对比字段可以不在case后面确定 可以把条件直接写在when后面,如果对比字 ...