BZOJ 3083 遥远的国度 树链剖分+线段树
有换根的树链剖分的裸题.
在换根的时候注意讨论.
注意数据范围要开unsigned int或longlong
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<string>
#include<iomanip>
#include<algorithm>
#include<map>
using namespace std;
#define LL long long
#define FILE "dealing"
#define up(i,j,n) for(LL i=j;i<=n;++i)
#define db double
#define uint unsigned int
#define eps 1e-12
#define pii pair<LL,LL>
LL read(){
LL x=0,f=1,ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return f*x;
}
const LL maxn=800010,maxm=20000,limit=1e6,mod=(LL)(7+1e9+0.1);
const db inf=(1e18);
template<class T>bool cmax(T& a,T b){return a<b?a=b,true:false;}
template<class T>bool cmin(T& a,T b){return a>b?a=b,true:false;}
template<class T>T min(T& a,T& b){return a<b?a:b;}
template<class T>T max(T& a,T& b){return a>b?a:b;}
LL n,m,root;
LL v[maxn],id[maxn],pre[maxn],low[maxn],dfs_clock=0,top[maxn],dep[maxn],fa[maxn][30];
struct node{
LL y,next;
}e[maxn];
LL len,linkk[maxn];
void insert(LL x,LL y){
e[++len].y=y;
e[len].next=linkk[x];
linkk[x]=len;
}
namespace Seg_tree{
LL Min[maxn],L,R,key,flag[maxn],f[maxn];
void updata(LL x){
Min[x]=min(Min[x<<1],Min[x<<1|1]);
}
void add(LL x,LL d){
Min[x]=d;
f[x]=1;
flag[x]=d;
}
void pushdown(LL x){
if(f[x]){
f[x]=0;
add(x<<1,flag[x]);
add(x<<1|1,flag[x]);
flag[x]=0;
}
}
void change(LL l,LL r,LL x){
if(l>R||r<L)return;
if(l>=L&&r<=R){add(x,key);return;}
LL mid=(l+r)>>1;
pushdown(x);
change(l,mid,x<<1);
change(mid+1,r,x<<1|1);
updata(x);
}
void Change(LL l,LL r,LL K){
L=l,R=r,key=K;
change(1,n,1);
}
LL query(LL l,LL r,LL x){
if(l>R||r<L)return (LL)(1e12);
if(l>=L&&r<=R)return Min[x];
LL mid=(l+r)>>1;
pushdown(x);
return min(query(l,mid,x<<1),query(mid+1,r,x<<1|1));
}
LL Query(LL Lef,LL Rig){
L=Lef,R=Rig;
return query(1,n,1);
}
void build(LL l,LL r,LL x){
if(l==r){
Min[x]=v[id[l]];
return;
}
LL mid=(l+r)>>1;
build(l,mid,x<<1);
build(mid+1,r,x<<1|1);
updata(x);
}
}
namespace shupou{
LL son[maxn],siz[maxn];
void dfs1(LL x){
siz[x]=1;
for(LL i=linkk[x];i;i=e[i].next){
if(e[i].y==fa[x][0])continue;
fa[e[i].y][0]=x;
dep[e[i].y]=dep[x]+1;
dfs1(e[i].y);
siz[x]+=siz[e[i].y];
if(siz[e[i].y]>siz[son[x]])son[x]=e[i].y;
}
}
void dfs2(LL x){
pre[x]=++dfs_clock;
if(son[x]){
top[son[x]]=top[x];
dfs2(son[x]);
}
for(LL i=linkk[x];i;i=e[i].next){
if(e[i].y==fa[x][0]||e[i].y==son[x])continue;
top[e[i].y]=e[i].y;
dfs2(e[i].y);
}
low[x]=dfs_clock;
}
void solve(){
dfs1(1);
top[1]=1;
dfs2(1);
}
}
namespace QUERY{
void change(LL x,LL y,LL key){//将x-y改成key
while(true){
if(dep[x]>dep[y])swap(x,y);
LL f1=top[x],f2=top[y];
if(f1==f2){
Seg_tree::Change(pre[x],pre[y],key);
return;
}
if(dep[f1]>dep[f2])swap(x,y),swap(f1,f2);
Seg_tree::Change(pre[f2],pre[y],key);
y=fa[f2][0];
}
}
LL query(LL x){
if(x==root)return Seg_tree::Min[1];
if(pre[x]>=pre[root]&&pre[x]<=low[root])return Seg_tree::Query(pre[x],low[x]);
if(dep[x]>=dep[root])return Seg_tree::Query(pre[x],low[x]);
LL y=root;
for(LL i=23;i>=0;i--)if(dep[y]-(dep[x]+1)>=(1<<i))y=fa[y][i];
if(fa[y][0]==x)return min(Seg_tree::Query(1,pre[y]-1),Seg_tree::Query(low[y]+1,n));
return Seg_tree::Query(pre[x],low[x]);
}
};
namespace sol{
void solve(){
n=read(),m=read();
up(i,2,n){
LL x=read(),y=read();
insert(x,y);insert(y,x);
}
up(i,1,n)v[i]=read();
root=read();
shupou::solve();
up(j,1,23)up(i,1,n)fa[i][j]=fa[fa[i][j-1]][j-1];
up(i,1,n)id[pre[i]]=i;
Seg_tree::build(1,n,1);
up(i,1,m){
LL ch=read();
if(ch==1)root=read();
if(ch==2){
LL x=read(),y=read(),v=read();
QUERY::change(x,y,v);
}
if(ch==3){
LL x=read();
LL d=0;
printf("%lld\n",d=QUERY::query(x));
}
}
}
}
int main(){
freopen(FILE".in","r",stdin);
freopen(FILE".out","w",stdout);
sol::solve();
return 0;
}
BZOJ 3083 遥远的国度 树链剖分+线段树的更多相关文章
- BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )
BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...
- BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)
BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...
- 【bzoj3083】遥远的国度 树链剖分+线段树
题目描述 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn ...
- BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)
前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...
- bzoj 4196 [Noi2015]软件包管理器 (树链剖分+线段树)
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 2852 Solved: 1668[Submit][Sta ...
- bzoj 2157: 旅游【树链剖分+线段树】
裸的树链剖分+线段树 但是要注意一个地方--我WA了好几次才发现取完相反数之后max值和min值是要交换的-- #include<iostream> #include<cstdio& ...
- BZOJ 3589 动态树 (树链剖分+线段树)
前言 众所周知,90%90\%90%的题目与解法毫无关系. 题意 有一棵有根树,两种操作.一种是子树内每一个点的权值加上一个同一个数,另一种是查询多条路径的并的点权之和. 分析 很容易看出是树链剖分+ ...
- 【BZOJ-2325】道馆之战 树链剖分 + 线段树
2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 1153 Solved: 421[Submit][Statu ...
- 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树
[BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...
- BZOJ2243 (树链剖分+线段树)
Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...
随机推荐
- 【GLSL教程】(九)其他说明 【转】
http://blog.csdn.net/racehorse/article/details/6664775 法线矩阵 在很多顶点shader中都用到了gl_NormalMatrix.这里将介绍这个矩 ...
- 英语词组instead of的用法
nstead of 是个短语介词.Instead of 的意思是“代替……”.“而不……”, 在语言的实际运用中,instead o功能与连词十分相似,现归纳如下: 1.跟名词:I give him ...
- Mycat本地模式的自增长分表操作
Mycat对表t_rc_rule_monitor做分表操作 在mysql上执行(没有t_rc_rule_monitor) DROP TABLE IF EXISTS t_rc_rule_monitor; ...
- 2016.7.12 Table configuration with catalog null, schema public, and table globalpage did not resolve to any tables(疑)
在eclipse中运行mybatis的generator插件时,出现如下错误提示: Generation Warnings Occured:Table configuration with catal ...
- poj 2479 Maximum sum(递推)
题意:给定n个数,求两段连续不重叠子段的最大和. 思路非常easy.把原串划为两段.求两段的连续最大子串和之和,这里要先预处理一下,用lmax数组表示1到i的最大连续子串和,用rmax数组表示n ...
- 【Python】程序在运行失败时,一声不吭继续运行pass
在前面程序出现异常时,我们都会给一个提示,告诉用户,程序为什么会异常,但是现在我们想在程序出现异常时,不做处理,让程序默默的往下执行,不要做声. 那么我们就引入了pass语句 def count_wo ...
- 在Linux里环境变量设置的方法(export PATH)
一般来说,配置交叉编译工具链的时候须要指定编译工具的路径,此时就须要环境变量设置.比如我的mips-linux-gcc编译器在"/opt/au1200_rm/build_tools/bin& ...
- Hibernate “N+1”查询问题
Hibernate 默认情况下使用立即检索策略,即从数据库加载A对象时 会同时加载跟它关联的B,这样产生了不必要的对象集合查询,而且本来可以合并的sql要执行1+N次,因为一条select出所有的A ...
- HDFS源码分析之数据块及副本状态BlockUCState、ReplicaState
关于数据块.副本的介绍,请参考文章<HDFS源码分析之数据块Block.副本Replica>. 一.数据块状态BlockUCState 数据块状态用枚举类BlockUCState来表示,代 ...
- Linux进入单用户模式
有时候配置linux的过程中,因为一些误操作导致系统初始化时堵塞或挂起而无法进入系统,原因往往是因为配置文件设置错误,部分文件被误删之类.遇到这种情况一般新手的做法就是重装(虚拟机不装白不装),但在实 ...