就是简单的树链剖分,但标记下传的时候一定要 ^1 而不能直接 = 1,我竟然WA在这么逗比的错误上不如一头撞死……

上代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define N 1100000
#define inf 0x7f7f7f7f
using namespace std; struct sss
{
int minnum, maxnum;
int push;
}t[N*];
int n, nowplace, bianp[N];
int p[N], next[N*], v[N*], c[N*], bnum;
int fa[N], son[N], siz[N], deep[N], top[N], w[N]; void build_tree(int now, int l, int r)
{
t[now].minnum = inf; t[now].maxnum = -inf; t[now].push = ;
if (l == r) return;
int mid = (l+r)/;
build_tree(now*, l, mid); build_tree(now*+, mid+, r);
} void downdate(int now)
{
if (!t[now].push) return; t[now].push = ;
t[now*].push ^= ; t[now*+].push ^= ;
swap(t[now*].maxnum, t[now*].minnum);
swap(t[now*+].maxnum, t[now*+].minnum);
t[now*].maxnum *= -; t[now*].minnum *= -;
t[now*+].maxnum *= -; t[now*+].minnum *= -;
} void update(int now)
{
t[now].maxnum = max(t[now*].maxnum, t[now*+].maxnum);
t[now].minnum = min(t[now*].minnum, t[now*+].minnum);
} void addbian(int x, int y)
{
bnum++; next[bnum] = p[x]; p[x] = bnum; v[bnum] = y;
bnum++; next[bnum] = p[y]; p[y] = bnum; v[bnum] = x;
} void dfs_1(int now, int nowfa, int nowdeep)
{
int k = p[now]; fa[now] = nowfa; deep[now] = nowdeep;
int maxson = ; son[now] = ; siz[now] = ;
while (k)
{
if (v[k] != nowfa)
{
bianp[(k+)/] = v[k];
dfs_1(v[k], now, nowdeep+);
siz[now] += siz[v[k]];
if (siz[v[k]] > maxson)
{
maxson = siz[v[k]];
son[now] = v[k];
}
}
k = next[k];
}
} void dfs_2(int now, int nowfa, int nowtop)
{
int k = p[now]; top[now] = nowtop; w[now] = ++nowplace;
if (son[now]) dfs_2(son[now], now, nowtop);
while (k)
{
if (v[k] != nowfa && v[k] != son[now])
dfs_2(v[k], now, v[k]);
k = next[k];
}
} int task(int now, int l, int r, int al, int ar)
{
if (al <= l && r <= ar) return t[now].maxnum;
int mid = (l+r)/, ans = -inf;
downdate(now);
if (al <= mid) ans = task(now*, l, mid, al, ar);
if (ar > mid) ans = max(ans, task(now*+, mid+, r, al, ar));
update(now); return ans;
} void tneg(int now, int l, int r, int tl, int tr)
{
if (tl <= l && r <= tr)
{
downdate(now);
swap(t[now].maxnum, t[now].minnum);
t[now].maxnum *= -; t[now].minnum *= -;
t[now].push ^= ; return;
}
int mid = (l+r)/;
downdate(now);
if (tl <= mid) tneg(now*, l, mid, tl, tr);
if (tr > mid) tneg(now*+, mid+, r, tl, tr);
update(now); return;
} void chan(int now, int l, int r, int cplace, int cnum)
{
if (l == r)
{
t[now].maxnum = t[now].minnum = cnum;
return;
}
int mid = (l+r)/;
downdate(now);
if (cplace <= mid) chan(now*, l, mid, cplace, cnum);
else chan(now*+, mid+, r, cplace, cnum);
update(now); return;
} void neg(int u, int v)
{
int f1 = top[u], f2 = top[v];
if (deep[f1] < deep[f2]) { swap(f1, f2); swap(u, v); }
if (f1 == f2)
{
if (u == v) return;
if (w[u] > w[v]) swap(u, v);
tneg(, , n, w[son[u]], w[v]);
return;
}
tneg(, , n, w[f1], w[u]); neg(fa[f1], v);
} int find(int u, int v)
{
int f1 = top[u],f2 = top[v];
if (deep[f1] < deep[f2]) { swap(f1, f2); swap(u, v); }
if (f1 == f2)
{
if (u == v) return -inf;
if (w[u] > w[v]) swap(u, v);
return task(, , n, w[son[u]], w[v]);
}
int ans = task(, , n, w[f1], w[u]);
return max(ans, find(fa[f1], v));
} int main()
{
int T; scanf("%d", &T);
while (T--)
{
scanf("%d", &n); memset(p, , sizeof(p));
build_tree(, , n); nowplace = ; bnum = ;
for (int i = ; i < n; ++i)
{
int x, y, z; scanf("%d%d%d", &x, &y, &z);
addbian(x, y); c[i] = z;
}
dfs_1(, , );
dfs_2(, , );
for (int i = ; i < n; ++i)
chan(, , n, w[bianp[i]], c[i]);
char s[];
while (scanf("%s", s) != EOF)
{
if (s[] == 'D') break;
int x, y; scanf("%d%d", &x, &y);
if (s[] == 'Q') printf("%d\n", find(x, y));
else if (s[] == 'C') chan(, , n, w[bianp[x]], y);
else if (s[]=='N') neg(x, y);
}
}
return ;
}

poj 3237 Tree的更多相关文章

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

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

  2. poj 3237 Tree(树链拆分)

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

  3. poj 3237 Tree 树链剖分

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

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

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

  5. poj 3237 Tree(树链剖分,线段树)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 7268   Accepted: 1969 Description ...

  6. ●POJ 3237 Tree

    题链: http://poj.org/problem?id=3237 题解: LCT 说一说如何完成询问操作就好了(把一条链的边权变成相反数的操作可以类比着来): 首先明确一下,我们把边权下放到点上. ...

  7. POJ 3237 Tree (树链剖分)

    Tree Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 2825   Accepted: 769 Description ...

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

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

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

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

  10. POJ 3237 Tree 【树链剖分】+【线段树】

    <题目链接> 题目大意: 给定一棵树,该树带有边权,现在对该树进行三种操作: 一:改变指定编号边的边权: 二:对树上指定路径的边权全部取反: 三:查询树上指定路径的最大边权值. 解题分析: ...

随机推荐

  1. CAShapeLayer和CAGradientLayer

    两个动画效果来了解一下CALayer的两个重要的subClass,CAGradientLayer和CAShapeLayer. 微视录制视频的时候那个进度效果和Spark相机类似,但是个人还是比较喜欢S ...

  2. 使用proguard混淆android代码

    当前是有些工具比方apktool,dextojar等是能够对我们android安装包进行反编译,获得源代码的.为了降低被别人破解,导致源代码泄露,程序被别人盗代替码,等等.我们须要对代码进行混淆,an ...

  3. php实现网页标签补全方法(转)

    导读:PHP在生成静态文件的时候,有时候会因为一些混编问题让HTML标签不完整或混乱而导致页面混乱.作者分享下面这段小代码可以非常方便解决问题. 如果你的网页内容的html标签显示不全,有些表格标签不 ...

  4. Python_爬虫1

    Urllib库的基本使用 那么接下来,小伙伴们就一起和我真正迈向我们的爬虫之路吧. 1.分分钟扒一个网页下来 怎样扒网页呢?其实就是根据URL来获取它的网页信息,虽然我们在浏览器中看到的是一幅幅优美的 ...

  5. Linux内核加载全流程

    无论是Linux还是Windows,在加电后的第一步都是先运行BIOS(Basic Input/Output System)程序——不知道是不是所以的电脑系统都是如此.BIOS保存在主板上的一个non ...

  6. graylog2+syslog-ng+mongodb构建集中管理日志服务器 --转载

    原文地址:http://blog.chinaunix.net/uid-11065483-id-3654882.html 由于公司内需要监控QQ的上下线记录,原本使用了分光+Panabit+Splunk ...

  7. QNetworkAccessManager的异步与线程

    Qt版本5.1.1 以HTTP操作为例 Qt中的HTTP操作都是异步的. 内部通过线程实现 创建线程的时机在QNetworkReplyHttpImplPrivate::postRequest() vo ...

  8. BootStrap2学习日记7---表格

    基本表 代码: <div class="container"> <h1 class="page-header">基本表</h1&g ...

  9. Emacs 安装 jedi

    Jedi 是个很棒的 python 的自动补全插件,可以显示 docstring, function arguments and code location. 安装步骤: 一.安装 python 的虚 ...

  10. [Android]自定义dialog

    吃过饭,有没有太写代码的愿望,就开始想今天工作中遇到的问题了. 有一个很有意思的东西就是Dialog,这个是基础的组件: 我们会在一些地方需要自定义弹出窗,可是用popUpWindow太小题大作,用弹 ...