题目大意:有一棵$n(n\leqslant2\times10^5)$个点的树,每个点有点权,所有的点权构成了$0\sim n-1$的排列。$q(q\leqslant2\times10^5)$次操作,操作有两种:

  1. $1\;x\;y:$交换$x$和$y$的点权
  2. $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的更多相关文章

  1. CF1083C Max Mex 线段树

    题面 CF1083C Max Mex 题解 首先我们考虑,如果一个数x是某条路径上的mex,那么这个数要满足什么条件? 1 ~ x - 1的数都必须出现过. x必须没出现过. 现在我们要最大化x,那么 ...

  2. Codeforces 1083C Max Mex

    Description 一棵\(N\)个节点的树, 每个节点上都有 互不相同的 \([0, ~N-1]\) 的数. 定义一条路径上的数的集合为 \(S\), 求一条路径使得 \(Mex(S)\) 最大 ...

  3. Max Mex

    Max Mex 无法直接处理 可以二分答案! [0,mid]是否在同一个链上? 可以不修改地做了 修改? 能不能信息合并?可以! 记录包含[l,r]的最短链的两端 可以[0,k][k+1,mid]合并 ...

  4. CF 1083 C. Max Mex

    C. Max Mex https://codeforces.com/contest/1083/problem/C 题意: 一棵$n$个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~ ...

  5. CodeForces 1084 F Max Mex

    Max Mex 题意:问在树上的所有路中mex值最大是多少. 题解: 用线段树维护值. 区间[L,R]意味着 区间[L,R]的数可不可以合并. 重点就是合并的问题了. 首先合法的区间只有3种: 1. ...

  6. CF 526F Max Mex(倍增求LCA+线段树路径合并)

    Max Mex 题目地址:https://codeforces.com/contest/1084/problem/F 然后合并时注意分情况讨论: 参考代码: #include<bits/stdc ...

  7. 【Codeforces 1083C】Max Mex(线段树 & LCA)

    Description 给定一颗 \(n\) 个顶点的树,顶点 \(i\) 有点权 \(p_i\).其中 \(p_1,p_2,\cdots, p_n\) 为一个 \(0\sim (n-1)\) 的一个 ...

  8. 【线段树】【CF1083C】 Max Mex

    Description 给定一棵有 \(n\) 个点的树,每个节点有点权.所有的点权构成了一个 \(0~\sim~n - 1\) 的排列.有 \(q\) 次操作,每次操作 \(1\) 为交换两个点的点 ...

  9. Codeforces 1083C Max Mex [线段树]

    洛谷 Codeforces 思路 很容易发现答案满足单调性,可以二分答案. 接下来询问就转换成判断前缀点集是否能组成一条链. 我最初的想法:找到点集的直径,判断直径是否覆盖了所有点,需要用到树套树,复 ...

随机推荐

  1. .net core 基于multipart/form-data的文件上传,这里以图片上传为例

    首先传递的数据格式大概如下: 然后就可以在后端获取数据了:直接上代码了哈: [HttpPost]        ///分别获取 data数据和调用图片上传方法 public async Task< ...

  2. 2019年猪年海报PSD模板-第一部分

    14套精美猪年海报,免费猪年海报,下载地址:百度网盘,https://pan.baidu.com/s/1i7bIzPRTX0OMbHFWnqURWQ                        

  3. spring + mybatis 注解 @Transactional失效

    1.问题 在使用@Transactional注解管理事务的时候会出现很多错误,比如: *** was not registered for synchronization because synchr ...

  4. JavaScript 之 对象/JSON/数组

    对象 简单说,所谓对象,就是一种无序的数据集合,由若干个“键值对”(key-value)构成. var obj = { p: 'Hello World' }; 上面代码中,大括号就定义了一个对象,它被 ...

  5. 剑指offer-栈的压入弹出序列21

    题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否可能为该栈的弹出顺序.假设压入栈的所有数字均不相等.例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压 ...

  6. Machine Learning笔记整理 ------ (三)基本性能度量

    1. 均方误差,错误率,精度 给定样例集 (Example set): D = {(x1, y1), (x2, y2), (x3, y3), ......, (xm, ym)} 其中xi是对应属性的值 ...

  7. Solium代码测试框架

    Solium, 在solid中,Linter用于标识和修复样式&安全问题 //调用测试 solium -d contracts --fix 源代码名称:Solium 源代码网址:http:// ...

  8. Sql server在cmd下的使用

    方法一 在DOS提示符下,c:\>isql -U sa -P (此处输入SA密码) 注----大小写敏感 回车后会出现"1>"提示符,表明已成功,此时可在DOS下做任何 ...

  9. Pandas dataframe数据写入文件和数据库

    转自:http://www.dcharm.com/?p=584 Pandas是Python下一个开源数据分析的库,它提供的数据结构DataFrame极大的简化了数据分析过程中一些繁琐操作,DataFr ...

  10. POJ 1679 The Unique MST(最小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...