有换根的树链剖分的裸题.

在换根的时候注意讨论.

注意数据范围要开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 遥远的国度 树链剖分+线段树的更多相关文章

  1. BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 )

    BZOJ.4034 [HAOI2015]树上操作 ( 点权树链剖分 线段树 ) 题意分析 有一棵点数为 N 的树,以点 1 为根,且树点有边权.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 ...

  2. BZOJ.1036 [ZJOI2008]树的统计Count ( 点权树链剖分 线段树维护和与最值)

    BZOJ.1036 [ZJOI2008]树的统计Count (树链剖分 线段树维护和与最值) 题意分析 (题目图片来自于 这里) 第一道树链剖分的题目,谈一下自己的理解. 树链剖分能解决的问题是,题目 ...

  3. 【bzoj3083】遥远的国度 树链剖分+线段树

    题目描述 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要zcwwzdjn ...

  4. BZOJ 3672[NOI2014]购票(树链剖分+线段树维护凸包+斜率优化) + BZOJ 2402 陶陶的难题II (树链剖分+线段树维护凸包+分数规划+斜率优化)

    前言 刚开始看着两道题感觉头皮发麻,后来看看题解,发现挺好理解,只是代码有点长. BZOJ 3672[NOI2014]购票 中文题面,题意略: BZOJ 3672[NOI2014]购票 设f(i)f( ...

  5. bzoj 4196 [Noi2015]软件包管理器 (树链剖分+线段树)

    4196: [Noi2015]软件包管理器 Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 2852  Solved: 1668[Submit][Sta ...

  6. bzoj 2157: 旅游【树链剖分+线段树】

    裸的树链剖分+线段树 但是要注意一个地方--我WA了好几次才发现取完相反数之后max值和min值是要交换的-- #include<iostream> #include<cstdio& ...

  7. BZOJ 3589 动态树 (树链剖分+线段树)

    前言 众所周知,90%90\%90%的题目与解法毫无关系. 题意 有一棵有根树,两种操作.一种是子树内每一个点的权值加上一个同一个数,另一种是查询多条路径的并的点权之和. 分析 很容易看出是树链剖分+ ...

  8. 【BZOJ-2325】道馆之战 树链剖分 + 线段树

    2325: [ZJOI2011]道馆之战 Time Limit: 40 Sec  Memory Limit: 256 MBSubmit: 1153  Solved: 421[Submit][Statu ...

  9. 【BZOJ2243】[SDOI2011]染色 树链剖分+线段树

    [BZOJ2243][SDOI2011]染色 Description 给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的 ...

  10. BZOJ2243 (树链剖分+线段树)

    Problem 染色(BZOJ2243) 题目大意 给定一颗树,每个节点上有一种颜色. 要求支持两种操作: 操作1:将a->b上所有点染成一种颜色. 操作2:询问a->b上的颜色段数量. ...

随机推荐

  1. 2016.8.22 Axure两级下拉框联动的实现

    刚学Axure,有些很简单的东西都要弄很久,但是弄出来的总归是很开心的. 参考来自:实现省市县下拉框的三级联动 http://www.woshipm.com/rp/348795.html/commen ...

  2. linux下内存

    MMU由一个或一组芯片组成.其功能是把逻辑地址映射为物理地址,进行地址转换(MMU是CPU的一部分) 机器指令仍然用逻辑地址指定一个操作数的地址或一条指令的地址 每个逻辑地址都由一个段选择符(16位) ...

  3. 取出所有的Map集合

    public static void main(String[] args) { Map<Integer, String> map = new HashMap<Integer, St ...

  4. SpringBoot启动流程分析(一):SpringApplication类初始化过程

    SpringBoot系列文章简介 SpringBoot源码阅读辅助篇: Spring IoC容器与应用上下文的设计与实现 SpringBoot启动流程源码分析: SpringBoot启动流程分析(一) ...

  5. c#线程顺序执行

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  6. 01-jsp与javabean

    <%@page import="java.util.Date"%><%@ page language="java" contentType=& ...

  7. js时间戳格式化成日期格式的多种方法

    js需要把时间戳转为为普通格式,一般的情况下可能用不到的, 下面先来看第一种吧 复制代码代码如下: function getLocalTime(nS) { return new Date(parseI ...

  8. 模拟struts2

    利用到的技术:dom4j和xpath 自己写一个Filter 在doFilter中拦截请求 // 2.1 得到请求资源路径            String uri = request.getReq ...

  9. 目标检测之hog(梯度方向直方图)---hog简介0

    梯度直方图特征(HOG) 是一种对图像局部重叠区域的密集型描述符, 它通过计算局部区域的梯度方向直方图来构成特征.Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功 ...

  10. Hive总结(四)hive安装记录

    本篇为安装篇较简单: 前提: 1: 安装了hadoop-1.0.4(1.0.3也能够)正常执行 2:安装了hbase-0.94.3, 正常执行 接下来,安装Hive,基于已经安装好的hadoop.过程 ...