把博客园图标替换成自己的图标
把博客园图标替换成自己的图标end

CF1083C Max Mex

题面传送门
我们考虑一条链的mex值为\(k\)的条件。显然需要满足\(0\)~\(k-1\)均出现在这条链上。
但是这不充分。不过因为题目中要求的是最大值,所以一定最大值所在链满足这个条件就合法。所以我们可以认为这是充分条件。
首先我们如何判断一个值\(k\)是否合法?
很简单,看看有没有一条链能覆盖\(0\)~\(k-1\)的所有点即可,这个可以用线段树加上st表的\(O(1)\)LCA维护。
然后在线段树上二分,时间复杂度大常数\(O((n+q)logn)\)
code:

#include<bits/stdc++.h>
#define I inline
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(x) ((x)>0?(x):-(x))
#define re register
#define RI re int
#define ll long long
#define db double
#define lb long db
#define N 200000
#define M 500000
#define mod 998244353
#define Mod (mod-1)
#define eps (1e-5)
#define U unsigned int
#define it iterator
#define Gc() getchar() 
#define Me(x,y) memset(x,y,sizeof(x))
#define d(x,y) (m*x+(y))
#define R(n) (rand()*rand()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound 
using namespace std;
int n,m,P[N+5],Id[N+5],Dfn[N+5<<1],x,y,op,lg[N+5<<1],Bg[N+5],H,T[N+5<<1][20],d[N+5];
struct yyy{int to,z;};struct ljb{int head,h[N+5];yyy f[N+5];I void add(int x,int y){f[++head]=(yyy){y,h[x]};h[x]=head;}}s;
I void dfs(int x,int La){Dfn[Bg[x]=++H]=x;d[x]=d[La]+1;yyy tmp;for(RI i=s.h[x];i;i=tmp.z) tmp=s.f[i],dfs(tmp.to,x),Dfn[++H]=x;}
I int LCA(int x,int y){x=Bg[x];y=Bg[y];x>y&&(swap(x,y),0);int D=lg[y-x+1];return d[T[x][D]]<d[T[y-(1<<D)+1][D]]?T[x][D]:T[y-(1<<D)+1][D];}I int Dt(int x,int y){return d[x]+d[y]-2*d[LCA(x,y)];}
struct Ques{int x,y,Fl;};I Ques Merge(Ques x,Ques y){
	if(x.Fl==-1||y.Fl==-1) return (Ques){0,0,-1};if(!x.Fl) return y;if(!y.Fl) return x;
	int D1=Dt(x.x,x.y),D2=Dt(x.x,y.x),D3=Dt(x.x,y.y),D4=Dt(x.y,y.x),D5=Dt(x.y,y.y),D6=Dt(y.x,y.y);
	if(D1==D2+D4&&D1==D3+D5) return (Ques){x.x,x.y,1};if(D2==D3+D6&&D2==D1+D4) return (Ques){x.x,y.x,1};if(D3==D1+D5&&D3==D2+D6) return (Ques){x.x,y.y,1};
	if(D4==D1+D2&&D4==D5+D6) return (Ques){x.y,y.x,1};if(D5==D1+D3&&D5==D4+D6) return (Ques){x.y,y.y,1};if(D6==D2+D3&&D6==D4+D5) return (Ques){y.x,y.y,1};return (Ques){0,0,-1};
}
namespace Tree{
	#define ls now<<1
	#define rs now<<1|1
	Ques F[N+5<<2];I void Up(int now){F[now]=Merge(F[ls],F[rs]);}I void BD(int l=0,int r=n-1,int now=1){if(l==r) {F[now]=(Ques){Id[l],Id[l],1};return;}int m=l+r>>1;BD(l,m,ls);BD(m+1,r,rs);Up(now);}
	I void Ins(int x,int l=0,int r=n-1,int now=1){if(l==r) {F[now]=(Ques){Id[x],Id[x],1};return;}int m=l+r>>1;x<=m?Ins(x,l,m,ls):Ins(x,m+1,r,rs);Up(now);}
	I int Find(Ques x,int l=0,int r=n-1,int now=1){if(l==r) return x=Merge(x,F[now]),~x.Fl?l+1:l;int m=l+r>>1;return ~Merge(x,F[ls]).Fl?Find(Merge(x,F[ls]),m+1,r,rs):Find(x,l,m,ls);}
	#undef ls
	#undef rs
}
int main(){
	freopen("1.in","r",stdin);
	RI i,j;scanf("%d",&n);for(i=1;i<=n;i++) scanf("%d",&P[i]),Id[P[i]]=i;for(i=2;i<=n;i++)scanf("%d",&x),s.add(x,i);dfs(1,0);for(i=2;i<=H;i++) lg[i]=lg[i/2]+1;
	for(i=H;i;i--) for(T[i][0]=Dfn[i],j=1;i+(1<<j)-1<=H;j++) T[i][j]=(d[T[i][j-1]]<d[T[i+(1<<j-1)][j-1]]?T[i][j-1]:T[i+(1<<j-1)][j-1]);
	Tree::BD();scanf("%d",&m);while(m--){scanf("%d",&op);if(op==2) printf("%d\n",Tree::Find((Ques){0,0,0}));else scanf("%d%d",&x,&y),swap(P[x],P[y]),swap(Id[P[x]],Id[P[y]]),Tree::Ins(P[x]),Tree::Ins(P[y]);}
}
posted @ 2022-01-26 13:38  275307894a  阅读(28)  评论(0编辑  收藏  举报
浏览器标题切换
浏览器标题切换end