Max Mex

无法直接处理

可以二分答案!

[0,mid]是否在同一个链上?

可以不修改地做了

修改?

能不能信息合并?可以!

记录包含[l,r]的最短链的两端

可以[0,k][k+1,mid]合并:枚举四个端点中的两个,使得另外两个一定在这两个的路径上

(判断z点在x,y路径上:(lca(x,z)==z||lca(y,z)=z)&&(lca(lca(x,y),z)=lca(x,y))画图即可理解

能合并,所以线段树可以维护。

线段树维护

线段树上二分。

LCA用ST表存

#include<bits/stdc++.h>
#define reg register int
#define il inline
#define fi first
#define se second
#define mk(a,b) make_pair(a,b)
#define numb (ch^'0')
using namespace std;
typedef long long ll;
template<class T>il void rd(T &x){
char ch;x=;bool fl=false;
while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true);
for(x=numb;isdigit(ch=getchar());x=x*+numb);
(fl==true)&&(x=-x);
}
template<class T>il void output(T x){if(x/)output(x/);putchar(x%+'');}
template<class T>il void ot(T x){if(x<) putchar('-'),x=-x;output(x);putchar(' ');}
template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} namespace Miracle{
const int N=2e5+;
int n,q;
struct node{
int nxt,to;
}e[*N];
int hd[N],cnt;
void add(int x,int y){
e[++cnt].nxt=hd[x];
e[cnt].to=y;
hd[x]=cnt;
}
int dep[N];
int a[*N],tot;
int dfn[N];
int f[*N][];
int lg[*N];
int id[N],fid[N];
void dfs(int x,int d){
dep[x]=d;
a[++tot]=x;
dfn[x]=tot;
for(reg i=hd[x];i;i=e[i].nxt){
int y=e[i].to;
dfs(y,d+);
a[++tot]=x;
}
}
int big(int x,int y){
return dep[x]<dep[y]?x:y;
}
int lca(int x,int y){
if(dfn[x]>dfn[y]) swap(x,y);
int len=lg[dfn[y]-dfn[x]+];
return big(f[dfn[x]][len],f[dfn[y]-(<<len)+][len]);
}
int on(int p1,int p2,int p3){
int y=lca(p1,p2);
// cout<<" p1 "<<p1<<" p2 "<<p2<<" p3 "<<p3<<" y "<<y<<endl;
return ((lca(p1,p3)==p3||lca(p2,p3)==p3)&&(lca(y,p3)==y));
}
struct tr{
int p[];
int can;
void init(){
can=p[]=p[]=;
}
void op(){
cout<<" can "<<can<<" p[0] "<<p[]<<" p[1] "<<p[]<<endl;
}
}t[*N];
#define ls (x<<1)
#define rs (x<<1|1)
#define mid ((l+r)>>1)
void pushup(int x){
t[x].init();
if(t[x<<].can&&t[x<<|].can){
t[x].can|=on(t[ls].p[],t[ls].p[],t[rs].p[])&&on(t[ls].p[],t[ls].p[],t[rs].p[]);
if(t[x].can==) {t[x].p[]=t[ls].p[],t[x].p[]=t[ls].p[];return;}
t[x].can|=on(t[rs].p[],t[rs].p[],t[ls].p[])&&on(t[rs].p[],t[rs].p[],t[ls].p[]);
if(t[x].can==) {t[x].p[]=t[rs].p[],t[x].p[]=t[rs].p[];return;}
if(!t[x].can){
for(reg i=;i<=;++i){
for(reg j=;j<=;++j){
t[x].can|=on(t[ls].p[i],t[rs].p[j],t[ls].p[i^])&&on(t[ls].p[i],t[rs].p[j],t[rs].p[j^]);
if(t[x].can==) {t[x].p[]=t[ls].p[i],t[x].p[]=t[rs].p[j];return;}
}
}
}
}
}
tr merge(tr A,tr B){
tr C;C.can=C.p[]=C.p[]=;
// cout<<" A ";A.op();
// cout<<" B ";B.op();
if(A.can&&B.can){
C.can|=on(A.p[],A.p[],B.p[])&&on(A.p[],A.p[],B.p[]);
if(C.can==) {C.p[]=A.p[],C.p[]=A.p[];return C;}
C.can|=on(B.p[],B.p[],A.p[])&&on(B.p[],B.p[],A.p[]);
if(C.can==) {C.p[]=B.p[],C.p[]=B.p[];return C;}
if(!C.can){
for(reg i=;i<=;++i){
for(reg j=;j<=;++j){
C.can|=on(A.p[i],B.p[j],A.p[i^])&&on(A.p[i],B.p[j],B.p[j^]);
if(C.can==) {C.p[]=A.p[i],C.p[]=B.p[j];return C;}
}
}
}
}
return C;
}
void build(int x,int l,int r){
if(l==r){
t[x].p[]=t[x].p[]=fid[l];
t[x].can=;return;
}
t[x].can=;
build(x<<,l,mid);
build(x<<|,mid+,r);
pushup(x);
}
void upda(int x,int l,int r,int p,int c){//pos to c
if(l==r){
t[x].p[]=c;t[x].p[]=c;
t[x].can=;return;
}
if(p<=mid) upda(ls,l,mid,p,c);
else upda(rs,mid+,r,p,c);
pushup(x);
}
tr tmp;
int query(int x,int l,int r){
// cout<<" query "<<x<<" "<<l<<" "<<r<<" : ";t[x].op(); tr C;
if(l==r){
if(tmp.can==-){
C=t[x];
}else{
C=merge(tmp,t[x]);
}
return C.can==?l:l-;
}
// cout<<" ls ";t[ls].op();
if(tmp.can==-){
C=t[ls];
}else{
C=merge(tmp,t[ls]);
}
// C.op();
if(C.can){
tmp=C;
return query(rs,mid+,r);
}else{
return query(ls,l,mid);
}
}
int main(){
rd(n);
for(reg i=;i<=n;++i){
rd(id[i]);fid[id[i]]=i;
}
int y;
for(reg i=;i<=n;++i){
rd(y);add(y,i);
}
dfs(,);
for(reg i=;i<=*n-;++i){
lg[i]=(i>>(lg[i-]+))?lg[i-]+:lg[i-];
f[i][]=a[i];
}
// cout<<" tot "<<tot<<endl;
// prt(a,1,tot);
// prt(lg,1,2*n-1);
for(reg j=;j<=;++j){
for(reg i=;i+(<<j)-<=tot;++i){
f[i][j]=big(f[i][j-],f[i+(<<(j-))][j-]);
}
}
build(,,n-); rd(q);
int op,x;
while(q--){
rd(op);
if(op==){
rd(x);rd(y);
upda(,,n-,id[x],y);
upda(,,n-,id[y],x);
swap(id[x],id[y]);
}else{
tmp.init();
tmp.can=-;
printf("%d\n",query(,,n-)+);
}
}
return ;
} }
signed main(){
Miracle::main();
return ;
} /*
Author: *Miracle*
Date: 2019/4/11 16:56:19
*/

正难则反考虑二分检验。

[IOI2018] seats 排座位有点像,维护前缀信息的连通性

修改?能否两两区间合并?树上链的信息合并的套路。

Max Mex的更多相关文章

  1. Codeforces 1083C Max Mex

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

  2. CF1083C Max Mex 线段树

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

  3. CF 1083 C. Max Mex

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

  4. CodeForces 1084 F Max Mex

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

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

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

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

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

  7. [CF1083C]Max Mex

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

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

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

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

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

随机推荐

  1. Mybatis+Spring整合后Mapper测试类编写

    public class UserMapperTest { private ApplicationContext applicationContext; @Before public void ini ...

  2. Day 4-9 subprocess模块

    我们经常需要通过Python去执行一条系统命令或脚本,系统的shell命令是独立于你的python进程之外的,每执行一条命令,就是发起一个新进程,通过python调用系统命令或脚本的模块在python ...

  3. Java 线程的生命周期

    当线程被创建并启动以后,它既不是一启动就进入了执行状态,也不是一直处于执行状态,在线程的生命周期中,它要经过新建(New).就绪(Runnable).运行(Running).阻塞(Blocked)和死 ...

  4. java & jdk

    java & jdk JDK 下载太慢 & java 12 https://download.oracle.com/otn-pub/java/jdk/12.0.1+12/69cfe15 ...

  5. python数据结构与算法第十七天【概率算法】

    1. 古典概率 例如:麻将开始摸到的14张牌中无将的概率,两张相同的牌即为将,则有: 所有的情况:从136张牌中选出14张牌,为C136-14 无将的情况:将不同的牌分组,共有34组,依次取14张牌, ...

  6. tomcat 与 nginx,apache的区别

    tomcat 与 nginx,apache的有什么区别 回答一: 题主说的Apache,指的应该是Apache软件基金会下的一个项目——Apache HTTP Server Project:Nginx ...

  7. Java反射交换两个整型变量的值

    在一次面试中,做了这么一道题"交换两个整型变量的值",当时看到这个题目之后,会心一笑,这也太简单了--直接使用中间变量交换不就可以了吗?但是,面试官却说不需要返回值,在调用的地方, ...

  8. php2

    session   //将用户的会话数据存储在服务端,通过 session_start()开启session,通过$_SESSION读写session session_start(); //开启ses ...

  9. openblas下载安装编译

    编译好的库: https://github.com/JuliaLinearAlgebra/OpenBLASBuilder/releases 源码编译 下载:https://github.com/xia ...

  10. .net core compatibility windows & windows compatible Linux

    Who is this package for? This package is meant for developers that need to port existing .NET Framew ...