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 < ...
随机推荐
- There is no Action mapped for namespace / and action name UserAction
果断收藏了,说的非常具体.刚開始学习的人常常遇到的问题. There is no Action mapped for namespace / and action name UserAction 在网 ...
- UVA 11235 Frequent values(RMQ)
Frequent values TimeLimit:3000Ms , ... , an in non-decreasing order. In addition to that, you are gi ...
- Oracle历史记录
请问如何查询ORACLE的历史操作记录!!!!!------解决方案-------------------- 有一个专门存储操作的数据库表..select t.SQL_TEXT, t.FIRST_LO ...
- dede时间格试,dede时间标签
dede的日期时间格式[field:pubdate function='strftime("时间",@me'/]这个是固定格式,(利用strftime()函数格式化时间),因些只需 ...
- linux动态库编译和使用
linux动态库编译和使用详细剖析 引言 重点讲述linux上使用gcc编译动态库的一些操作.并且对其深入的案例分析.最后介绍一下动态库插件技术, 让代码向后兼容.关于linux上使用gcc基础编译, ...
- Xamarin 手动安装步骤+破解
原文:Xamarin 手动安装步骤+破解 由于Xamarin运行的时候有很多依赖项,所以现在在官网上下载的XamarinInstall只是一个下载安装器,并不是软件包.它会自动下载安装所需的依赖软件, ...
- HDU 1201
18岁生日 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- 猫学习IOS(三)UI纯代码UI——图片浏览器
猫分享.必须精品 看看效果 主要实现相似看新闻的一个界面,不用拖拽,纯代码手工写. 首先分析app能够非常easy知道他这里有两个UILabel一个UIImageView还有两个UIButton 定义 ...
- Google Maps Android API v2 (2)- 地图对象
地图对象 Android的谷歌地图API允许你在你的Android应用程序中显示谷歌地图.在谷歌地图移动(GMM)的应用程序,你看到的地图,这些地图具有相同的外观和API暴露出许多相同的功能.GM ...
- [Eclipse]代码已被写入关于如何切换到unix在新行
切换换行符的显示格式, 一般的设置是这种: 可是这样仅仅能对新文件起作用,原来已经写好的还是那熊样.(怎么办呢,得吃懊悔药啊,谁让你開始没准备好呢?!T_T) 以下就是懊悔药: 这样就OK了. 附:有 ...