[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 思路 很容易发现答案满足单调性,可以二分答案. 接下来询问就转换成判断前缀点集是否能组成一条链. 我最初的想法:找到点集的直径,判断直径是否覆盖了所有点,需要用到树套树,复 ...
随机推荐
- DSP5509的RTC实验-第3篇
1. RTC实时时钟,不在过多介绍,本例程直接调用芯片支持库CSL的库函数,用起来比较简单 main() { CSL_init(); printf ("\nTESTING...\n" ...
- Ubuntu配置IP
Ubuntu网络配置的常用系统,于是我学习研究了Ubuntu网络配置,在这里对大家详细介绍下Ubuntu网络配置应用,希望对大家有用Ubuntu网络配置包含了非常好的翻译和容易使用的架构./etc/n ...
- Win10 远程服务器版
朋友的电脑刚装了1803版的Win10,然后他用KMS_VL_ALL6.9激活了一下,竟然变成了一个奇怪的版本:“远程服务器版”!第一次见这玩意,还真稀罕.帮他研究了一下,发现KMS_VL_ALL在激 ...
- JavaWeb(三)——Tomcat服务器(二)
一.打包JavaWeb应用 在Java中,使用"jar"命令来对将JavaWeb应用打包成一个War包,jar命令的用法如下:
- 「Python」matplotlib备忘录
总结了一下网上现有的资源,得到了一些东西.随手做个备忘. 更多设置见:https://matplotlib.org/users/customizing.html. 导入 import matplotl ...
- 接口测试工具postman(六)添加变量(参数化)
1.添加全局变量并引用 2.通过设置请求前置配置变量 3.在Tests里面,把响应数据设置为变量 4.添加外部文件,引用外部文件中的变量和数据,此种场景就可以执行多次请求 1)配置文件,txt或者cs ...
- jmeter获取cookies
使用场景:登录后,后续的请求操作需获取到JSESSIONID才可进行 1.将jmeter的bin目录下的jmeter.properties文件中的CookieManager.save.cookies= ...
- 【WXS全局对象】JSON
方法: 原型:JSON.stringify( Object ) 说明:将 object 对象转换为 JSON 字符串,并返回该字符串. 返回:[String] 原型:JSON.parse( [Stri ...
- github项目切换远程https到ssh通道
github 切换远程https到ssh通道 github 每个仓库有两类地址:https和ssh通道. https通道获取代码方便,提交过程中每次都需要输入用户名和密码. ssh通道需要提前配置号s ...
- c语言中反转字符串的函数strrev(), reverse()
1.使用string.h中的strrev函数 #include<stdio.h> #include<string.h> int main() { char s[]=" ...