[CF1083C]Max Mex
题目大意:有一棵$n(n\leqslant2\times10^5)$个点的树,每个点有点权,所有的点权构成了$0\sim n-1$的排列。$q(q\leqslant2\times10^5)$次操作,操作有两种:
- $1\;x\;y:$交换$x$和$y$的点权
- $2:$询问$\max\limits_{x,y\in V}\{mex(x\to y)\}$
题解:这个明显具有可二分性(然后我考场上写了一个虚树判链的,复杂度爆炸+没用)。翻看题解知道这道题可以用线段树来维护区间点是否在一条链上,在区间合并的时候枚举一下在两个区间的四个端点中是哪两个端点为大区间的端点,用$LCA$以及$dfn$序判断一下即可。
在最开始判一下树的形态是一条链的情况
卡点:无
C++ Code:
#include <algorithm>
#include <cctype>
#include <cstdio>
namespace __IO {
int ch;
inline int read() {
static int x;
while (isspace(ch = getchar())) ; x = ch & 15;
while (isdigit(ch = getchar())) x = x * 10 + (ch & 15);
return x;
}
}
using __IO::read; #define maxn 200010
int head[maxn], cnt;
struct Edge {
int to, nxt;
} e[maxn];
inline void addedge(int a, int b) {
e[++cnt] = (Edge) { b, head[a] }; head[a] = cnt;
} int fa[maxn], sz[maxn], dep[maxn];
int top[maxn], dfn[maxn], idx;
int find(int x) { return x == top[x] ? x : (top[x] = find(top[x])); }
void dfs(int u) {
int son = 0;
dfn[u] = ++idx, sz[u] = 1;
for (int i = head[u], v; i; i = e[i].nxt) {
v = e[i].to;
dep[v] = dep[u] + 1;
dfs(v), sz[u] += sz[v];
if (sz[v] > sz[son]) son = v;
}
if (son) top[son] = u;
}
inline int LCA(int x, int y) {
while (top[x] != top[y]) {
if (dep[top[x]] > dep[top[y]]) x = fa[top[x]];
else y = fa[top[y]];
}
return dep[x] < dep[y] ? x : y;
} int n, m, w[maxn], ret[maxn]; inline bool in_sub(int x, int y) {
return (dfn[x] >= dfn[y]) && (dfn[x] < dfn[y] + sz[y]);
}
inline bool in_line(int x, int y, int z) {
static int lca; lca = LCA(x, y);
return in_sub(z, lca) && (in_sub(x, z) || in_sub(y, z));
}
inline bool in_line(int x, int y, int l, int r) {
return in_line(x, y, l) && in_line(x, y, r);
} struct node {
bool line;
int l, r;
inline node() { }
inline node(int __l) : line(true), l(__l), r(__l) { }
inline node(int __l, int __r) : line(true), l(__l), r(__r) { }
inline node(bool __line, int __l, int __r) : line(__line), l(__l), r(__r) { }
static inline node NS() { return node(0, -1, -1); }
inline node operator + (const node &rhs) const {
if (!l || !r) return rhs;
if (!line || !rhs.line) return NS();
const int x = rhs.l, y = rhs.r;
if (in_line(l, r, x, y)) return node(l, r);
if (in_line(l, x, r, y)) return node(l, x);
if (in_line(l, y, x, r)) return node(l, y);
if (in_line(r, x, l, y)) return node(r, x);
if (in_line(r, y, l, x)) return node(r, y);
if (in_line(x, y, l, r)) return node(x, y);
return NS();
}
} I(0, 0, 0); namespace SgT {
node V[maxn << 2];
void build(const int rt, const int l, const int r) {
if (l == r) {
V[rt] = node(ret[l]);
return ;
}
const int mid = l + r >> 1;
build(rt << 1, l, mid), build(rt << 1 | 1, mid + 1, r);
V[rt] = V[rt << 1] + V[rt << 1 | 1];
} int pos;
void __modify(const int rt, const int l, const int r) {
if (l == r) {
V[rt] = node(ret[l]);
return ;
}
const int mid = l + r >> 1;
if (pos <= mid) __modify(rt << 1, l, mid);
else __modify(rt << 1 | 1, mid + 1, r);
V[rt] = V[rt << 1] + V[rt << 1 | 1];
}
void modify(const int __pos) {
pos = __pos;
__modify(1, 0, n - 1);
} node now;
int res;
void __query(int rt, int l, int r) {
if (l == r) {
res = l;
return ;
}
node tmp = now + V[rt << 1];
const int mid = l + r >> 1;
if (tmp.line) {
now = tmp;
__query(rt << 1 | 1, mid + 1, r);
} else __query(rt << 1, l, mid);
}
int query() {
now = I, res = 1;
__query(1, 0, n - 1);
return res;
}
} int ind[maxn];
bool is_line = true;
int main() {
n = read();
for (int i = 1; i <= n; ++i) {
w[i] = read();
ret[w[i]] = top[i] = i;
}
for (int i = 2; i <= n; ++i) {
fa[i] = read();
addedge(fa[i], i);
++ind[fa[i]], ++ind[i];
if (ind[fa[i]] > 2 || ind[i] > 2) is_line = false;
}
dfs(1);
for (int i = 1; i <= n; ++i) find(i);
SgT::build(1, 0, n - 1); m = read();
while (m --> 0) {
static int op, x, y;
op = read();
if (op == 1) {
x = read(), y = read();
std::swap(ret[w[x]], ret[w[y]]), std::swap(w[x], w[y]);
if (!is_line) SgT::modify(w[x]), SgT::modify(w[y]);
} else printf("%d\n", is_line ? n : SgT::query());
}
return 0;
}
[CF1083C]Max Mex的更多相关文章
- CF1083C Max Mex 线段树
题面 CF1083C Max Mex 题解 首先我们考虑,如果一个数x是某条路径上的mex,那么这个数要满足什么条件? 1 ~ x - 1的数都必须出现过. x必须没出现过. 现在我们要最大化x,那么 ...
- Codeforces 1083C Max Mex
Description 一棵\(N\)个节点的树, 每个节点上都有 互不相同的 \([0, ~N-1]\) 的数. 定义一条路径上的数的集合为 \(S\), 求一条路径使得 \(Mex(S)\) 最大 ...
- Max Mex
Max Mex 无法直接处理 可以二分答案! [0,mid]是否在同一个链上? 可以不修改地做了 修改? 能不能信息合并?可以! 记录包含[l,r]的最短链的两端 可以[0,k][k+1,mid]合并 ...
- CF 1083 C. Max Mex
C. Max Mex https://codeforces.com/contest/1083/problem/C 题意: 一棵$n$个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~ ...
- CodeForces 1084 F Max Mex
Max Mex 题意:问在树上的所有路中mex值最大是多少. 题解: 用线段树维护值. 区间[L,R]意味着 区间[L,R]的数可不可以合并. 重点就是合并的问题了. 首先合法的区间只有3种: 1. ...
- CF 526F Max Mex(倍增求LCA+线段树路径合并)
Max Mex 题目地址:https://codeforces.com/contest/1084/problem/F 然后合并时注意分情况讨论: 参考代码: #include<bits/stdc ...
- 【Codeforces 1083C】Max Mex(线段树 & LCA)
Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 有点权 \(p_i\).其中 \(p_1,p_2,\cdots, p_n\) 为一个 \(0\sim (n-1)\) 的一个 ...
- 【线段树】【CF1083C】 Max Mex
Description 给定一棵有 \(n\) 个点的树,每个节点有点权.所有的点权构成了一个 \(0~\sim~n - 1\) 的排列.有 \(q\) 次操作,每次操作 \(1\) 为交换两个点的点 ...
- Codeforces 1083C Max Mex [线段树]
洛谷 Codeforces 思路 很容易发现答案满足单调性,可以二分答案. 接下来询问就转换成判断前缀点集是否能组成一条链. 我最初的想法:找到点集的直径,判断直径是否覆盖了所有点,需要用到树套树,复 ...
随机推荐
- mysql using filesort Using temporary
using filesort 一般人的回答是: “当行数据太大,导致内存无法容下这些数据产生的临时表时,他们就会被放入磁盘中排序.” 很不幸,这个答案是错的 ,临时表在太大的时候确实会到磁盘离去,但 ...
- ogg的安装配置 配置双向同步(含DDL)
第一部分 先配置单向同步(含DDL) 一 源端安装GoldenGate 创建用户 创建目录 mkdir -p /opt/ogg chmod -R 777 /opt/ogg chown -R oracl ...
- 三分钟小课堂-----------------docker(三)增删改查命令
主要为docker容器的增删改查命令 1 创建容器: docker run -it --name 别名 image_name /bin/bash --name 别名 -d 后台 -t ...
- 栈和队列ADT -数据结构(C语言实现)
数据结构与算法分析 栈模型 限制插入和删除只能在表的末端的表 表的末端叫做栈顶(top) 支持Push进栈和Pop入栈操作 //LIFO后进先出表 栈的实现 链表实现 类型声明 struct Node ...
- Java学习 · 初识 多线程
多线程 1. 基础概念 a) 程序 Program i. 静态代码,指令集,应用程序执行的蓝本 b) 进程 Process i. 动态概念,正在运 ...
- Java学习 · 初识 IO流
IO流 1. 原理与概念 a) 流 i. 流动,流向 ii. 从一端移动到另一端 源头到目的地 iii. 抽象.动态概念,是一连 ...
- 骰子涂色 (Cube painting,UVa 253)
题目描述:算法竞赛入门习题4-4 题目思路:1.旋转其中一个骰子进行匹配 2.进行遍历,如果匹配,就进行相对面的匹配 3.三个对立面都匹配即是一样等价的 //没有按照原题的输入输出 #include ...
- php多进程单例模式下的 MySQL及Redis连接错误修复
前几天写了个php常驻脚本,主要逻辑如下 //跑完数据后休息60秒 $sleepTime = 60; $maxWorker = 10; while (true) { $htmlModel = new ...
- HDU 1512 Monkey King(左偏树)
Description Once in a forest, there lived N aggressive monkeys. At the beginning, they each does thi ...
- httpd 2.2.15 添加流媒体模块
项目中使用的一直都是 httpd 2.2.15 用于播放视频资源,近期有个新产品上线发现快进视频会出现卡顿情况,因此添加了流媒体模块.(怀疑是新产品中的播放器进行了更改) 原文:http://li ...