题目链接: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 maxn = 10005;
const int INF = 0x3f3f3f3f; int id, far[maxn], son[maxn], cnt[maxn], idx[maxn], top[maxn], dep[maxn]; int N, en, first[maxn], jump[maxn * 2], val[maxn];
struct Edge {
int u, v, w;
void set (int u, int v, int w) {
this->u = u;
this->v = v;
this->w = w;
}
}ed[maxn * 2]; void dfs_fir (int u, int pre, int d) {
dep[u] = d;
far[u] = pre;
cnt[u] = 1;
son[u] = 0; for (int i = first[u]; i + 1; i = jump[i]) {
int v = ed[i].v;
if (v == pre)
continue;
dfs_fir(v, u, d + 1);
cnt[u] += cnt[v];
if (cnt[son[u]] < cnt[v])
son[u] = v;
}
} void dfs_sec(int u, int rot) {
idx[u] = id++;
top[u] = rot; if (son[u])
dfs_sec(son[u], rot); for (int i = first[u]; i + 1; i = jump[i]) {
int v = ed[i].v;
if (v == far[u] || v == son[u])
continue;
dfs_sec(v, v);
}
} #define lson(x) ((x)<<1)
#define rson(x) (((x)<<1)|1) int lc[maxn << 2], rc[maxn << 2], filp[maxn << 2], Ma[maxn << 2], Mi[maxn << 2]; inline void maintain (int u) {
filp[u] ^= 1;
swap(Ma[u], Mi[u]);
Ma[u] = -Ma[u];
Mi[u] = -Mi[u];
} inline void pushup(int u) {
Ma[u] = max(Ma[lson(u)], Ma[rson(u)]);
Mi[u] = min(Mi[lson(u)], Mi[rson(u)]);
} inline void pushdown (int u) {
if (filp[u]) {
maintain(lson(u));
maintain(rson(u));
filp[u] = 0;
}
} void build (int u, int l, int r) {
lc[u] = l;
rc[u] = r;
filp[u] = 0; if (l == r) {
Ma[u] = Mi[u] = val[l];
return;
} int mid = (l + r) / 2;
build(lson(u), l, mid);
build(rson(u), mid + 1, r);
pushup(u);
} void modify (int u, int x, int w) {
if (lc[u] == x && rc[u] == x) {
Ma[u] = Mi[u] = w;
return;
} pushdown(u);
int mid = (lc[u] + rc[u]) / 2;
if (x <= mid)
modify(lson(u), x, w);
else
modify(rson(u), x, w);
pushup(u);
} void splay(int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r) {
maintain(u);
return;
} pushdown(u);
int mid = (lc[u] + rc[u]) / 2;
if (l <= mid)
splay(lson(u), l, r);
if (r > mid)
splay(rson(u), l, r);
pushup(u);
} int query (int u, int l, int r) {
if (l <= lc[u] && rc[u] <= r)
return Ma[u]; pushdown(u);
int mid = (lc[u] + rc[u]) / 2, ret = -INF;
if (l <= mid)
ret = max(ret, query(lson(u), l, r));
if (r > mid)
ret = max(ret, query(rson(u), l, r));
pushup(u);
return ret;
} inline void add_Edge(int u, int v, int w) {
ed[en].set(u, v, w);
jump[en] = first[u];
first[u] = en++;
} void init () {
en = 0;
id = 1;
memset(first, -1, sizeof(first)); scanf("%d", &N); int u, v, w;
for (int i = 0; i < N - 1; i++) {
scanf("%d%d%d", &u, &v, &w);
add_Edge(u, v, w);
add_Edge(v, u, w);
} dfs_fir(1, 0, 0);
dfs_sec(1, 1); for (int i = 0; i < N - 1; i++) {
int t = i * 2;
if (dep[ed[t].u] < dep[ed[t].v])
swap(ed[t].u, ed[t].v);
val[idx[ed[t].u]] = ed[t].w;
}
build(1, 1, N);
} int query (int u, int v) {
int p = top[u], q = top[v], ret = -INF;
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
ret = max(ret, query(1, idx[p], idx[u]));
u = far[p];
p = top[u];
}
//printf("%d %d\n", u, v); if (u == v)
return ret; if (dep[u] > dep[v])
swap(u, v);
ret = max(ret, query(1, idx[son[u]], idx[v]));
return ret;
} void modify (int u, int v) {
int p = top[u], q = top[v];
while (p != q) {
if (dep[p] < dep[q]) {
swap(p, q);
swap(u, v);
}
splay(1, idx[p], idx[u]);
u = far[p];
p = top[u];
} if (u == v)
return; if (dep[u] > dep[v])
swap(u, v);
splay(1, idx[son[u]], idx[v]);
} void solve () {
int u, v;
char op[20];
while (scanf("%s", op), strcmp(op, "DONE") != 0) {
scanf("%d%d", &u, &v);
if (op[0] == 'C')
modify(1, idx[ed[u*2-2].u], v);
else if (op[0] == 'Q')
printf("%d\n", query(u, v));
else
modify(u, v);
}
} int main () {
int cas;
scanf("%d", &cas);
while (cas--) {
init();
solve();
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

poj 3237 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 (树链剖分 路径剖分 线段树的lazy标记)

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

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

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

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

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

  5. hdu5044 Tree 树链拆分,点细分,刚,非递归版本

    hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...

  6. hdu 4912 Paths on the tree(树链拆分+贪婪)

    题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...

  7. poj 3237 Tree [LCA] (树链剖分)

    poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...

  8. POJ3237 Tree 树链剖分 边权

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

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

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

随机推荐

  1. Android数据库高手秘籍(一)——SQLite命令

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/38461239 要想熟练地操作不论什么一个数据库.最最主要的要求就是要懂SQL语言, ...

  2. 一步一步学android之事件篇——单选按钮监听事件

    在平常使用软件的时候,我们经常会碰见一些选择题,例如选择性别的时候,在男和女之间选,前面说过这个情况要用RadioGroup组件,那么点击了之后我们该怎么获取到选择的那个值呢,这就是今天要说的OnCh ...

  3. MVAPI第一个版本架构图

    MVAPI采用矢量与栅格结合的方式进行移动地图的显示. 进过几个月,目前终于可以完成基本的地图显示及操作功能.还有待实现的是各种性能及效果优化.3D地物等. 发一个1.0的架构图留存一下.(虽然目前还 ...

  4. Storm-0.9.2-incubating源代码编译打包

    近期遇到一些同学询问Storm-0.9.2-incubating源代码编译打包的问题,现将编译步骤说明例如以下: 1.凝视掉project各pom文件里关于maven插件(maven-gpg-plug ...

  5. python手记(52)

    python将信息加密进图片 从图片中解密信息 >>> runfile(r'K:\testpro\test1.py', wdir=r'K:\testpro') http://blog ...

  6. quick-cocos2d-x游戏开发【4】——加入文本

    文本的加入在quick中被封装在ui类中,它能够创建EditBox.菜单以及文本,文本总得来说能够创建TTF和BMFont两种. api对于它的说明非常具体.ui.newBMFontLabel(par ...

  7. (九)通过几段代码,理清angularJS中的$injector、$rootScope和$scope的概念和关联关系

    $injector.$rootScope和$scope是angularJS框架中比較重要的东西,理清它们之间的关系,对我们兴许学习和理解angularJS框架都很实用. 1.$injector事实上是 ...

  8. Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发

    原文 Visual Studio跨平台开发实战(5) - Xamarin Android多页面应用程式开发 前言 大部份的Andr​​oid 都具有实体或虚拟的Back键. 因此在处理多页面应用程式时 ...

  9. Google是不是真的不能用了?非常奇怪的问题

    这几天,事实上是这一阵子. 我连用goagent都无法上google了. 可怜我一直用Gmail邮箱.但如今我连用代理都上不了Gmail了. .. 是我自己电脑本身的问题吗?非常奇怪的问题,我原先用g ...

  10. ubuntu12.04 安装和配置jdk1.7

    第一步:下载jdk-7-linux-i586.tar.gz wget -c http://download.oracle.com/otn-pub/java/jdk/7/jdk-7-linux-i586 ...