POJ 3237 Tree (树链拆分)
做题情绪:了。
解题思路:
主要注意如何区间更新就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 (树链拆分)的更多相关文章
- poj 3237 Tree(树链拆分)
题目链接:poj 3237 Tree 题目大意:给定一棵树,三种操作: CHANGE i v:将i节点权值变为v NEGATE a b:将ab路径上全部节点的权值变为相反数 QUERY a b:查询a ...
- 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 (树链剖分 路径剖分 线段树的lazy标记)
题目链接:http://poj.org/problem?id=3237 一棵有边权的树,有3种操作. 树链剖分+线段树lazy标记.lazy为0表示没更新区间或者区间更新了2的倍数次,1表示为更新,每 ...
- 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 ...
- hdu5044 Tree 树链拆分,点细分,刚,非递归版本
hdu5044 Tree 树链拆分.点细分.刚,非递归版本 //#pragma warning (disable: 4786) //#pragma comment (linker, "/ST ...
- hdu 4912 Paths on the tree(树链拆分+贪婪)
题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LC ...
- poj 3237 Tree [LCA] (树链剖分)
poj 3237 tree inline : 1. inline 定义的类的内联函数,函数的代码被放入符号表中,在使用时直接进行替换,(像宏一样展开),没有了调用的开销,效率也很高. 2. 很明显,类 ...
- 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 < ...
随机推荐
- Lichee(两) 在sun4i_crane该平台下编译
让我们先来回顾一下编译命令 $ cd workdir/lichee $ ./build.sh -p sun4i_crane -k 3.0 lichee文件夹下的build.sh #!/bin/bas ...
- Java EE (11) - 影响性能的因素
垂直层(Tier)影响性能的因素 资源层数据库性能通常考虑以下方面的优化(MySQL为例):--使用哪种存储引擎:MyISAM vs. InnoDB, MERGE, MEMORY, Federated ...
- POJ 2536 Gopher II(二分图的最大匹配)
题目链接:http://poj.org/problem?id=2536 题意:已知有n仅仅老鼠的坐标,m个洞的坐标,老鼠的移动速度为V,S秒以后有一仅仅老鹰要吃老鼠,问有多少个老鼠被吃. 非常明晰,二 ...
- Matlab hermite
保形分段三次hermite插值 % 这是MATLAB里面的pchip.m文件.这里把它的凝视改写成汉语,主要是想弄清楚它是怎么计算在节点处的导数的. function v = pchip(x,y,xx ...
- oracle nologging用法(转)
一.oracle日志模式分为(logging,force logging,nologging) 默认情况是logging,就是会记录到redo日志中,force logging是强制记录日志,nolo ...
- SpringMVC(转)
http://www.cnblogs.com/liukemng/p/3725582.html
- hdu2544(自己实现优先队列)
hdu2544 dij水题,用来测试自己实现优先队列对不对 #include <stdio.h> #include <string.h> #include <stdli ...
- C++ 操作 MySQL
使用VS2008如IDE, 准备操作: 1. 项目属性 C++ 其他包括文件夹 为 mysql 安装文件夹的include 如:"C:\Program Files (x86)\MySQL\ ...
- Git代理服务器设置和访问Github
因为现在工作的网络环境有着非常严格的限制,.可以说,在最近的访问通过代理Github它采取了一些曲折的.也积累了一些相关经验.我们认为有必要注意什么. 符合"不要再发明轮子"宗旨, ...
- ubuntu下安装wine
直接在终端里面输入“sudo apt-get install wine”不要引号,即可安装wine.至于使用它,终端命令就是“wine ....”举个例子,你现在要运行魔兽, 然后你的魔兽的文件夹的位 ...