主题链接~~>

做题情绪:了。

解题思路:

主要注意如何区间更新就ok了 。

树链剖分就是树上的线段树。

代码:

#include<iostream>
#include<sstream>
#include<map>
#include<cmath>
#include<fstream>
#include<queue>
#include<vector>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<stack>
#include<bitset>
#include<ctime>
#include<string>
#include<cctype>
#include<iomanip>
#include<algorithm>
using namespace std ;
#define INT __int64
#define L(x) (x * 2)
#define R(x) (x * 2 + 1)
const int INF = 0x3f3f3f3f ;
const double esp = 0.0000000001 ;
const double PI = acos(-1.0) ;
const int mod = 1e9 + 7 ;
const int MY = 1e7 + 5 ;
const int MX = 10000 + 5 ;
int n ,num ,idx ;
int head[MX] ,dep[MX] ,top[MX] ,ti[MX] ,siz[MX] ,son[MX] ,father[MX] ;
struct NODE
{
int u ,v ,w ;
}e[MX] ;
struct Edge
{
int v ,next ;
}E[MX*2] ;
void addedge(int u ,int v)
{
E[num].v = v ; E[num].next = head[u] ; head[u] = num++ ;
E[num].v = u ; E[num].next = head[v] ; head[v] = num++ ;
}
void dfs_find(int u ,int fa)
{
dep[u] = dep[fa] + 1 ;
siz[u] = 1 ;
son[u] = 0 ;
father[u] = fa ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == fa) continue ;
dfs_find(v ,u) ;
siz[u] += siz[v] ;
if(siz[son[u]] < siz[v]) son[u] = v ;
}
}
void dfs_time(int u ,int fa)
{
ti[u] = idx++ ;
top[u] = fa ;
if(son[u]) dfs_time(son[u] ,top[u]) ;
for(int i = head[u] ;i != -1 ;i = E[i].next)
{
int v = E[i].v ;
if(v == father[u] || v == son[u]) continue ;
dfs_time(v ,v) ;
}
}
struct node
{
int le ,rt ,Mi ,Mx ,add ;
}T[MX*4] ;
void build(int x ,int le ,int rt)
{
T[x].le = le ; T[x].rt = rt ;
T[x].Mi = INF ; T[x].Mx = -INF ;
T[x].add = 0 ;
if(le == rt) return ;
int Mid = (le + rt)>>1 ;
build(L(x) ,le ,Mid) ;
build(R(x) ,Mid+1 ,rt) ;
}
void push_down(int x)
{
if(T[x].le == T[x].rt) return ;
if(T[x].add)
{
T[L(x)].Mi = -T[L(x)].Mi ;
T[L(x)].Mx = -T[L(x)].Mx ;
swap(T[L(x)].Mi ,T[L(x)].Mx) ;
T[R(x)].Mi = -T[R(x)].Mi ;
T[R(x)].Mx = -T[R(x)].Mx ;
swap(T[R(x)].Mi ,T[R(x)].Mx) ;
T[L(x)].add ^= 1 ;
T[R(x)].add ^= 1 ;
T[x].add = 0 ;
}
}
void push_up(int x)
{
T[x].Mi = min(T[L(x)].Mi ,T[R(x)].Mi) ;
T[x].Mx = max(T[L(x)].Mx ,T[R(x)].Mx) ;
}
void section(int x ,int le ,int rt) // 更新某个区间
{
if(T[x].le == le && T[x].rt == rt)
{
T[x].add ^= 1 ;
T[x].Mi = -T[x].Mi ;
T[x].Mx = -T[x].Mx ;
swap(T[x].Mi ,T[x].Mx) ;
return ;
}
if(T[x].le == T[x].rt) return ;
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(le > Mid)
section(R(x) ,le ,rt) ;
else if(rt <= Mid)
section(L(x) ,le ,rt) ;
else
{
section(L(x) ,le ,Mid) ;
section(R(x) ,Mid+1 ,rt) ;
}
push_up(x) ;
}
void update(int x ,int pos ,int w)
{
if(T[x].le == T[x].rt)
{
T[x].Mi = w ;
T[x].Mx = w ;
T[x].add = 0 ;
return ;
}
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(pos <= Mid)
update(L(x) ,pos ,w) ;
else update(R(x) ,pos ,w) ;
push_up(x) ;
}
int Query(int x ,int le ,int rt) // 询问某个区间
{
if(T[x].le == le && T[x].rt == rt)
return T[x].Mx ;
push_down(x) ;
int Mid = (T[x].le + T[x].rt)>>1 ;
if(le > Mid)
return Query(R(x) ,le ,rt) ;
else if(rt <= Mid)
return Query(L(x) ,le ,rt) ;
else
return max(Query(L(x) ,le ,Mid) ,Query(R(x) ,Mid+1 ,rt)) ;
push_up(x) ;
}
int LCA(int u ,int v)
{
if(u == v) return 0 ;
int ans = -INF ;
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
swap(u ,v) ;
ans = max(ans ,Query(1 ,ti[top[u]] ,ti[u])) ;
u = father[top[u]] ;
}
if(dep[u] > dep[v])
swap(u ,v) ;
if(u != v)
ans = max(ans ,Query(1 ,ti[u]+1 ,ti[v])) ;
return ans ;
}
void change(int u ,int v)
{
while(top[u] != top[v])
{
if(dep[top[u]] < dep[top[v]])
swap(u ,v) ;
section(1 ,ti[top[u]] ,ti[u]) ;
u = father[top[u]] ;
}
if(dep[u] > dep[v])
swap(u ,v) ;
section(1 ,ti[u]+1 ,ti[v]) ;
}
int main()
{
int Tx ;
scanf("%d" ,&Tx) ;
while(Tx--)
{
scanf("%d" ,&n) ;
num = 0 ;
memset(head ,-1 ,sizeof(head)) ;
for(int i = 1 ;i < n ; ++i)
{
scanf("%d%d%d" ,&e[i].u ,&e[i].v ,&e[i].w) ;
addedge(e[i].u ,e[i].v) ;
}
dep[1] = 0 ; siz[0] = 0 ;
dfs_find(1 ,1) ;
idx = 1 ;
dfs_time(1 ,1) ;
build(1 ,2 ,n) ;
for(int i = 1 ;i < n ; ++i)
{
if(dep[e[i].u] < dep[e[i].v])
swap(e[i].u ,e[i].v) ;
update(1 ,ti[e[i].u] ,e[i].w) ;
}
int x ,y ;
char s[10] ;
while(true)
{
scanf("%s" ,s) ;
if(s[0] == 'D') break ;
scanf("%d%d" ,&x ,&y) ;
if(s[0] == 'C')
update(1 ,ti[e[x].u] ,y) ;
else if(s[0] == 'N')
change(x ,y) ;
else if(s[0] == 'Q')
cout<<LCA(x ,y)<<endl ;
}
}
return 0 ;
}

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

POJ 3237 Tree (树链拆分)的更多相关文章

  1. poj 3237 Tree(树链拆分)

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

  2. poj 3237 Tree 树链剖分

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

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

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

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

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

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

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

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

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

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

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

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

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

  9. POJ3237 Tree 树链剖分 边权

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

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

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

随机推荐

  1. mysql表修改

    CREATE TABLE tab2 AS (SELECT * FROM tab1)这种做法表的存储引擎也会采用服务器默认的存储引擎而不是源表的存储引擎,此种复制方法把表的内容也一起复制过来了. CRE ...

  2. 动态加载资源文件(ResourceDictionary)

    原文:动态加载资源文件(ResourceDictionary) 在xaml中控件通过绑定静态资源StaticResource来获取样式Style有多种方式: 1.在项目的启动文件App中<App ...

  3. 参数化测试--sheet表的应用

    自动化测试对录制和编辑好的测试步骤进行回放,这种是线性的自动化测试方式,其缺点是明显的,就是其测试覆盖面比较低.测试回放的只是录制时做出的界面操作,以及输入的测试数据,或者是脚本编辑时指定的界面操作和 ...

  4. Linux命令之文本处理(二)

    cut命令 cut命令用来操作文件的列,能够视为列编辑器:与之相应是大多数的行"编辑器".如sed.grep.sort等,它们操作文本时,以行为单位. cut的主要功能就是输出文本 ...

  5. Python3.2官方文件翻译--课堂笔记和异常是阶级

    6.7备注 有时喜欢Pasca在"录"和C中"数据体"的数据类型很实用.集合一些数据项. 一个空类定义能够清楚地显示: class Employee: pass ...

  6. memcached 分布式聚类算法

    memcached 分布式集群,该决定必须书面开发商自己.和redis 由分布式server决定.上 memcached 有两个选项用于分布式.第一个是:模运算 另一种是:一致性hash 分布式算法. ...

  7. profile与bashrc

    /etc/profile./etc/bashrc 是系统全局环境变量设定 ~/.profile,~/.bashrc用户家文件夹下的私有环境变量设定 当登入系统时候获得一个shell进程时.其读取环境设 ...

  8. 设计模式 - 模板方法模式(template method pattern) JFrame 具体解释

    模板方法模式(template method pattern) JFrame 具体解释 本文地址: http://blog.csdn.net/caroline_wendy 參考模板方法模式(templ ...

  9. WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体

    原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...

  10. Codeforces Round #257 (Div. 2) D题:Jzzhu and Cities 删特殊边的最短路

    D. Jzzhu and Cities time limit per test 2 seconds memory limit per test 256 megabytes input standard ...