BZOJ 3083: 遥远的国度(树链剖分+DFS序)
可以很显而易见的看出,修改就是树链剖分,而询问就是在dfs出的线段树里查询最小值,但由于这道题会修改根节点,所以在查询的时候需判断x是否为root的祖先,如果不是就直接做,是的话应该查询从1-st[y]-1 ed[y]+1 -- n 的最小值,其中y是root的祖先,x的儿子
PS:好久没做题,线段树都查了好久QAQ,还是没查出错来,最后只能cheat一个程序了QAQ
CODE:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100010
#define maxm 200020
typedef long long ll ;
struct edges{
int to,next;
}edge[maxm];
int next[maxn],l,n,m;
int addedge(int x,int y){
edge[++l]=(edges){y,next[x]};next[x]=l;
edge[++l]=(edges){x,next[y]};next[y]=l;
return 0;
}
ll _min(ll x,ll y){
if (x==0) return y;
if (y==0) return x;
return min(x,y);
}
int dep[maxn],fa[maxn],sum[maxn],ch[maxn],pre[maxn],pos[maxn],add[maxn],lef[maxn],righ[maxn];
int cl;
bool b[maxn];
int dfs(int u){
dep[u]=dep[fa[u]]+1;
sum[u]=1;b[u]=1;
for (int i=next[u];i;i=edge[i].next)
if (!b[edge[i].to]){
fa[edge[i].to]=u;
dfs(edge[i].to);
sum[u]+=sum[edge[i].to];
ch[u]=sum[edge[i].to]>sum[ch[u]]?edge[i].to:ch[u];
}
return 0;
}
int heavy(int u,bool bo){
cl++;
pos[add[cl]=u]=cl;
b[u]=1;lef[u]=cl;
if (bo) pre[u]=u;
else pre[u]=pre[fa[u]];
if (ch[u]) heavy(ch[u],0);
for (int i=next[u];i;i=edge[i].next)
if (!b[edge[i].to])
heavy(edge[i].to,1);
righ[u]=cl;
return 0;
}
struct TREE{
int l,r;ll x;bool lazy;
}t[maxn*8];
ll s[maxn];
int buildtree(int x,int l,int r){
t[x].l=l;t[x].r=r;
if (l==r) {t[x].x=s[add[l]];return 0;}
buildtree(x<<1,l,(l+r)>>1);
buildtree((x<<1)+1,((l+r)>>1)+1,r);
t[x].x=_min(t[x<<1].x,t[(x<<1)+1].x);
return 0;
}
int pushdown(int x){
if (t[x].lazy) {
t[x<<1].x=t[(x<<1)+1].x=t[x].x;
t[x<<1].lazy=t[(x<<1)+1].lazy=1;
t[x].lazy=0;
}
return 0;
}
ll cha(int x,int x1,int y1){
int l=t[x].l,r=t[x].r;
if (l>y1||r<x1) return 0;
if (l==r) return (t[x].x);
if (l>=x1&&r<=y1) return t[x].x;
pushdown(x);
return _min(cha(x<<1,x1,y1),cha((x<<1)+1,x1,y1));
}
int gai(int x,ll y,int x1,int y1){
int l=t[x].l,r=t[x].r;
if (l>y1||r<x1) return 0;
if (l==r) {t[x].x=y;return 0;}
if (l>=x1&&r<=y1) {t[x].lazy=1;t[x].x=y;return 0;}
pushdown(x);
gai(x<<1,y,x1,y1);gai((x<<1)+1,y,x1,y1);
t[x].x=_min(t[x<<1].x,t[(x<<1)+1].x);
return 0;
}
int change(int l,int r,ll x){
for (;;){
if (pre[l]==pre[r]) {
if (dep[l]>dep[r]) swap(l,r);
gai(1,x,pos[l],pos[r]);
return 0;
}else{
if (dep[pre[l]]<dep[pre[r]]) swap(l,r);
gai(1,x,pos[pre[l]],pos[l]);
l=fa[pre[l]];
}
}
return 0;
}
int root,tem;
bool check(int x){
int y=root;
while (fa[y]!=0){
if (fa[y]==x) {tem=y;return 1;}
if (dep[fa[y]]<dep[x]) return 0;
if (pre[fa[y]]==pre[x]) {tem=ch[x];return 1;}
y=pre[fa[y]];
}
return 0;
}
ll query(int x){
if (!check(x))return (cha(1,lef[x],righ[x]));
else return (_min(cha(1,1,lef[tem]-1),cha(1,righ[tem]+1,n)));
}
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<n;i++) {
int x,y;
scanf("%d%d",&x,&y);
addedge(x,y);
}
for (int i=1;i<=n;i++) scanf("%lld",s+i);
memset(b,0,sizeof(b));
dfs(1);
memset(b,0,sizeof(b));
heavy(1,1);
buildtree(1,1,n);
scanf("%d",&root);
while (m--){
int opt,l,r;ll x;
scanf("%d",&opt);
if (opt==1) scanf("%d",&root);
if (opt==2) {
scanf("%d%d%lld",&l,&r,&x);
change(l,r,x);
}
if (opt==3){
scanf("%d",&l);
printf("%lld\n",query(l));
}
}
return 0;
}
BZOJ 3083: 遥远的国度(树链剖分+DFS序)的更多相关文章
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 3083 遥远的国度 树链剖分
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 797 Solved: 181[Submit][Status] Descrip ...
- BZOJ 3083 遥远的国度(树链剖分+LCA)
Description 描述zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcwwzdjn的去路,他需要z ...
- BZOJ 3083 遥远的国度 树链剖分+线段树
有换根的树链剖分的裸题. 在换根的时候注意讨论. 注意数据范围要开unsigned int或longlong #include<iostream> #include<cstdio&g ...
- bzoj 3083 遥远的国度 —— 树链剖分
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3083 换根后路径还是不变,子树分类讨论一下,树剖后线段树维护即可. 代码如下: #inclu ...
- bzoj 3083 遥远的国度——树链剖分+线段树维护子树信息
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3083 int 的范围是 2^31 - 1 ,所以权值是不是爆 int 了…… O( nlog ...
- BZOJ 3083 遥远的国度 树链剖分+脑子
唉..又调了半天QWQ..为何读入挂了.....莫非读入是反着的????据ywy学长所言如是...OvO震惊 这啥骚题啊...还要换根...不过清明讲过...(然鹅我现在才做... 先随便选个点(比如 ...
- [BZOJ - 2819] Nim 【树链剖分 / DFS序】
题目链接: BZOJ - 2819 题目分析 我们知道,单纯的 Nim 的必胜状态是,各堆石子的数量异或和不为 0 .那么这道题其实就是要求求出树上的两点之间的路径的异或和.要求支持单点修改. 方法一 ...
- 树链剖分&dfs序
树上问题 很多处理区间的问题(像是RMQ,区间修改).可以用线段树,树状数组,ST表这些数据结构来维护.但是如果将这些问题挪到了树上,就不能直接用这些数据结构来处理了.这时就用到了dfs序和树链剖分. ...
随机推荐
- 编译Uboot时提示error while loading shared libraries: libz.so.1: cannot open shared object file: No such file or directory
在Ubuntu14.04 64位系统中已经安装了libc6:i386的库,编译Uboot时提示error while loading shared libraries: libz.so.1: cann ...
- 1)Linux学习笔记:crontab命令
crond简介 crond是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程 配置文件 ``` SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin: ...
- ui主线程控件的更新就让这个activity的异步任务做完整
项目中使用的SingleMessageView,控件实例化后,点击用户头像,此时跳转到UserInfo里查看这个用户的头像.用户名.签名.标签. 之前,师兄在SingleMessage里写了个头像的点 ...
- Myeclipse程序调试快捷键及步骤详解
Myeclipse程序调试快捷键及步骤详解: 调试快捷键 Eclipse中有如下一些和运行调试相关的快捷键. 1. [Ctrl+Shift+B]:在当前行设置断点或取消设置的断点. ...
- HDU-2502-月之数
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2502 分析: 比如n=4时,有: 1000 1001 1010 1011 1100 1101 1110 ...
- VS生成时复制文件到指定目录
1.右键项目属性,选择生成事件,再点击"编辑后生成事件",可以直接在编辑框内填写命令行,如图: 2.也可以在弹出的编辑框内,写命令,$(ProjectDir)这种是系统的宏路径,具 ...
- Winform ListView的用法
清除数据: lvOrder.Items.Clear(); 赋值数据: if (lvList.Count != 0) { foreach (var item in lvList) { string[] ...
- 《你不知道的js》 ------1.作用域是什么
相关定义 引擎:从头到尾负责整个JavaScript程序的编译及执行过程. 编译器:负责语法分析及代码生成等. 作用域:负责收集并维护由所有声明的标识符(变量)组成的一系列查询,并实施一套非常严格的规 ...
- (一)Hololens Unity 开发环境搭建(Mac BOOTCAMP WIN10)
(一)Hololens Unity 开发环境搭建(Mac BOOTCAMP WIN10) 系统要求 64位 Windows 10 除了家庭版的 都支持 ~ 64位CPU CPU至少是四核心以上~ 至少 ...
- Excel实用知识2(排序,筛选,分析工具)
[排序,筛选,分析工具(描述统计,相关系数,回归分析,抽样分析,预测工作表)] 纯手打,可能有错别字,使用的版本是office2013 转载请注明出处 http://www.cnblogs.com/h ...