Luogu 3979 遥远的国度
树剖已经是人尽皆知的sb题了吗……
很早以前就想填掉这坑了……
考虑到树链唯一,进行操作并不会对换根产生影响,那么我们的换根操作只要记下root在哪里就好了
询问的时候分类讨论:
1:root == x 直接返回全树最大值就好了
2:lca(root, x) != x,那就和x没什么关系了,只要返回原子树最大值就好了
3:lca(root, x) == x,说明x在root的上方,那么找到x的儿子中root的祖先y,除了y的子树就都是x的子树了
跳树链和找lca可以用倍增实现
总复杂度 O(nlog2n)
Code(写的很长……):
#include <cstdio>
using namespace std; const int N = 1e5 + ;
const int Lg = ;
const int inf = << ; int n, qn, a[N], tot = , head[N], root;
int dfsc = , id[N], siz[N], top[N];
int w[N], dep[N], fa[N][Lg], son[N]; struct Edge {
int to, nxt;
} e[N << ]; inline void add(int from, int to) {
e[++tot].to = to;
e[tot].nxt = head[from];
head[from] = tot;
} inline void read(int &X) {
X = ;
char ch = ;
int op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} inline int min(int x, int y) {
return x > y ? y : x;
} inline void chkMin(int &x, int y) {
if(y < x) x = y;
} inline void swap(int &x, int &y) {
int t = x;
x = y;
y = t;
} void dfs1(int x, int fat, int depth) {
siz[x] = , fa[x][] = fat, dep[x] = depth;
for(int i = ; i <= ; i++)
fa[x][i] = fa[fa[x][i - ]][i - ]; int maxson = -;
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fat) continue;
dfs1(y, x, depth + );
siz[x] += siz[y];
if(siz[y] > maxson)
maxson = siz[y], son[x] = y;
}
} void dfs2(int x, int topf) {
w[id[x] = ++dfsc] = a[x], top[x] = topf;
if(!son[x]) return;
dfs2(son[x], topf);
for(int i = head[x]; i; i = e[i].nxt) {
int y = e[i].to;
if(y == fa[x][] || y == son[x]) continue;
dfs2(y, y);
}
} inline int getLca(int x, int y) {
if(dep[x] < dep[y]) swap(x, y);
for(int i = ; i >= ; i--)
if(dep[fa[x][i]] >= dep[y])
x = fa[x][i];
if(x == y) return x;
for(int i = ; i >= ; i--)
if(fa[x][i] != fa[y][i])
x = fa[x][i], y = fa[y][i];
return fa[x][];
} namespace SegT {
int s[N << ], tag[N << ]; #define lc p << 1
#define rc p << 1 | 1
#define mid ((l + r) >> 1) inline void up(int p) {
if(p) s[p] = min(s[lc], s[rc]);
} inline void down(int p) {
if(tag[p] == inf) return;
s[lc] = tag[lc] = s[rc] = tag[rc] = tag[p];
tag[p] = inf;
} void build(int p, int l, int r) {
tag[p] = inf;
if(l == r) {
s[p] = w[l];
return;
} build(lc, l, mid);
build(rc, mid + , r);
up(p);
} void modify(int p, int l, int r, int x, int y, int v) {
if(x <= l && y >= r) {
s[p] = tag[p] = v;
return;
} down(p);
if(x <= mid) modify(lc, l, mid, x, y, v);
if(y > mid) modify(rc, mid + , r, x, y, v);
up(p);
} int query(int p, int l, int r, int x, int y) {
if(x <= l && y >= r) return s[p];
down(p); int res = inf;
if(x <= mid) chkMin(res, query(lc, l, mid, x, y));
if(y > mid) chkMin(res, query(rc, mid + , r, x, y));
return res;
} } using namespace SegT; inline void mtree(int x, int y, int v) {
for(; top[x] != top[y]; ) {
if(dep[top[x]] < dep[top[y]]) swap(x, y);
modify(, , n, id[top[x]], id[x], v);
x = fa[top[x]][];
}
if(dep[x] > dep[y]) swap(x, y);
modify(, , n, id[x], id[y], v);
} inline int ask(int x) {
if(root == x) return s[];
int y = getLca(x, root);
if(y == x) {
int z = root;
for(int i = ; i >= ; i--)
if(dep[fa[z][i]] > dep[y])
z = fa[z][i];
return min(query(, , n, , id[z] - ), query(, , n, id[z] + siz[z], n));
} else return query(, , n, id[x], id[x] + siz[x] - );
} int main() {
read(n), read(qn);
for(int x, y, i = ; i < n; i++) {
read(x), read(y);
add(x, y), add(y, x);
}
dfs1(, , ); for(int i = ; i <= n; i++) read(a[i]);
dfs2(, );
build(, , n); read(root);
for(int op; qn--; ) {
read(op);
if(op == ) read(root);
if(op == ) {
int x, y, v;
read(x), read(y), read(v);
mtree(x, y, v);
}
if(op == ) {
int x;
read(x);
printf("%d\n", ask(x));
}
} return ;
}
Luogu 3979 遥远的国度的更多相关文章
- 【luogu P3979 遥远的国度】 题解
题目链接:https://www.luogu.org/problemnew/show/P3979 除了换根操作都是裸的树剖 所以换根时考虑: 1.我查询的根等于换的根:无影响 2.我查询的根是换的根的 ...
- [Luogu] 遥远的国度
https://www.luogu.org/problemnew/show/P3979 3种情况 x=root,很显然此时应当查询整棵树 lca(root,x)!=x ,此时直接查询x的子树即可,与换 ...
- 【Luogu】P3979遥远的国度(树链剖分)
题目链接 不会换根从暑假开始就困扰我了……拖到现在…… 会了还是很激动的. 换根操作事实上不需要(也不能)改树剖本来的dfs序……只是在query上动动手脚…… 设全树的集合为G,以root为根,u在 ...
- 洛谷 3979 BZOJ 3083 遥远的国度
[题解] 这道题除去根操作就是普通的树链剖分了.但是有换根操作怎么处理呢? 我们可以发现如果现在的根不在查询的点的子树里,那么对本次查询没有影响.如果现在的跟在查询的点x的子树里,那么答案将变为整棵树 ...
- [luogu3979][bzoj3083]遥远的国度
[luogu传送门] [bzoj传送门] 题目描述 zcwwzdjn在追杀十分sb的zhx,而zhx逃入了一个遥远的国度.当zcwwzdjn准备进入遥远的国度继续追杀时,守护神RapiD阻拦了zcww ...
- Luogu 1514 引水入城 (搜索,动态规划)
Luogu 1514 引水入城 (搜索,动态规划) Description 在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N行M列的矩形,如上图 ...
- [luogu]P1514 引水入城[搜索][记忆化][DP]
[luogu]P1514 引水入城 引水入城 题目描述在一个遥远的国度,一侧是风景秀美的湖泊,另一侧则是漫无边际的沙漠.该国的行政区划十分特殊,刚好构成一个N 行M 列的矩形 ,如下图所示,其中每个格 ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- Luogu 魔法学院杯-第二弹(萌新的第一法blog)
虽然有点久远 还是放一下吧. 传送门:https://www.luogu.org/contest/show?tid=754 第一题 沉迷游戏,伤感情 #include <queue> ...
随机推荐
- yeomen/bower/grunt
yeomen: npm install yo angular-in-action project npm install -g generator-angular npm install -g gen ...
- SQL使用指南(1)—— 数据定义语言(DDL)
1.使用create 语句创建表 CREATE TABLE table_name (column_name datatype[null|not null], column_name datatype[ ...
- 1.Linux和Unix区别
整理来源于网络:http://blog.csdn.net/xiaojianpitt/article/details/6377419 有很多初学Linux的人比较关心Linux和windows的区别,这 ...
- Redis底层探秘(二):链表和跳跃表
链表简介 链表提供了高效的节点重排能力,以及顺序性的节点访问方式,并且可以通过增删节点来灵活地跳转链表的长度. 作为一种常用数据结构,链表内置在很多高级的编程语言里面,因为Redis使用C语言并没有内 ...
- ACM学习历程—SNNUOJ1214 矩阵1(二分)
题目链接:http://219.244.176.199/JudgeOnline/problem.php?id=1214 这是这次微软实习面试的一道题,题目大意就是:有一个n*m的矩阵,已知它每一行都是 ...
- Codeforces 786C. Till I Collapse 主席树
题目大意: 给定一个长度为\(n\)的序列,要求将其划分为最少的若干段使得每段中不同的数字的种数不超过\(k\). 对于 \(k = 1 .. n\)输出所有的答案. \(n \leq 10^5\) ...
- 巧用padding让图片宽高比固定并自适应布局
1.从上图知道原始图片大小是 800 * 250,即宽高比为 3.2: 2.html 及 css 代码如下, 可以知道就只是在一个div里面放了一张图片,那么如何让这张图片的宽高比固定呢,看了css之 ...
- UDP10040 和 setsockopt设置大全
今天无意之中碰到 UDP 10040 错误 原来是缓冲区不够,以下转载的解决方法以供不时之需. 1.closesocket(一般不会立即关闭而经历TIME_WAIT的过程)后想继续重用该sock ...
- keepalived之 ipvsadm-1.26-4(lvs)+ keepalived-1.2.24 安装
一.安装 LVS 前提:已经提前配置好本地 Yum 源 配置过程可参考> http://blog.csdn.net/zhang123456456/article/details/56690945 ...
- Ubuntu下部署GitLab-——基于14.04系统
搭建GitLab的目的: 方便公司开发管理代码 GitLab实现的功能: 1.关闭了gitlab的注册功能 2.修改了默认端口 3.汉化 0.前期准备 # 环境 Ubuntu 14.04 root@i ...