CoderForces Round526F Max Mex(倍增求LCA+线段树路径合并)

Max Mex
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Once Grisha found a tree (connected graph without cycles) with a root in node 1.

But this tree was not just a tree. A permutation p of integers from 0 to n1 is written in nodes, a number pi is written in node i.

As Grisha likes to invent some strange and interesting problems for himself, but not always can solve them, you need to help him deal with two types of queries on this tree.

Let's define a function MEX(S), where S is a set of non-negative integers, as a smallest non-negative integer that is not included in this set.

Let l be a simple path in this tree. So let's define indices of nodes which lie on l as u1u2uk.

Define V(l) as a set {pu1pu2 , puk}.

Then queries are:

  1. For two nodes i and j, swap pi and pj.
  2. Find the maximum value of MEX(V(l)) in all possible l.
Input

The first line contains a single integer n (2n2105) — the number of nodes of a tree.

The second line contains n integers — p1p2pn (0pi<n) — the permutation p, it's guaranteed that all numbers are different .

The third line contains n1 integers — d2d3dn (1di<i), where di is a direct ancestor of node i in a tree.

The fourth line contains a single integer q (1q2105) — the number of queries.

The following q lines contain the description of queries:

At the beginning of each of next q lines, there is a single integer t (1 or 2) — the type of a query:

  1. If t=1, the line also contains two integers i and j (1i,jn) — the indices of nodes, where values of the permutation should be swapped.
  2. If t=2, you need to find the maximum value of MEX(V(l)) in all possible l.
Output

For each type 2 query print a single integer — the answer for this query.

Examples
input
Copy
6
2 5 0 3 1 4
1 1 3 3 3
3
2
1 6 3
2
output
Copy
3
2
input
Copy
6
5 2 1 4 3 0
1 1 1 3 3
9
2
1 5 3
2
1 6 1
2
1 4 2
2
1 1 6
2
output
Copy
3
2
4
4
2

Number written in brackets is a permutation value of a node.

In the first example, for the first query, optimal path is a path from node  1 to node 5. For it, set of values is {0,1,2} and MEX is 3.
For the third query, optimal path is a path from node 5 to node 6. For it, set of values is {0,1,4} and MEX is 2.
In the second example, for the first query, optimal path is a path from node 2 to node 6. For it, set of values is {0,1,2,5} and MEX is 3.
For the third query, optimal path is a path from node 5 to node 6. For it, set of values is {0,1,3} and MEX is 2.
For the fifth query, optimal path is a path from node 5 to node 2. For it, set of values is {0,1,2,3} and MEX is 4.
For the seventh query, optimal path is a path from node 5 to node 4. For it, set of values is {0,1,2,3} and MEX is 4.
src="https://codeforces.com/predownloaded/8f/b7/8fb73f1d5de495d91e704f41ae1a13c55273c659.png" alt="" width="261" height="302" /> For the ninth query, optimal path is a path from node 6 to node 5. For it, set of values is {0,1,3} and MEX is 2.
题意:一棵n个点的树,每个点上有一个数(每个点的上的数互不相同,而且构成一个0~n-1的排列),要求找到一条路径,使得路径的mex最大。

分析:问题转化为,查询一个a,0~a-1是否可以都存在于一条路径上。类似线段树维护连通性,这里线段树的每个点表示所对应的区间[l,r]是否可 以存在于一条路径上。合并的时候用lca和dfs序的位置判断。然后就是线段树上二分了。 重点在连通时,用pair<x,y>表示x和y之间有一条路径;

我们从左边开始判断下一个点a是否在前面0~a-1个点合并为一条路径可以的话,继续下一个点,如果不行,答案就是ans=a;

首先先dfs,求出各个点的深度以及dfs顺序(in[]和out[]两个数组用来判断一个点是否在另一个点的上方)

然后合并时注意分情况讨论:

参考代码:
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define pb push_back
  4 #define mkp make_pair
  5 #define fi first
  6 #define se second
  7 typedef long long ll;
  8 typedef pair<int,int> PII;
  9 const int INF=0x3f3f3f3f;
 10 inline int read()
 11 {
 12     int x=0,f=1;char ch=getchar();
 13     while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
 14     while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
 15     return x*f;
 16 }
 17 const int maxn=2e5+10;
 18 int n,q,typ,x,y,cnt,ans;
 19 int a[maxn],in[maxn],out[maxn];
 20 int dep[maxn],fc[21],fa[21][maxn];
 21 vector<int> G[maxn];
 22 PII res;
 23 
 24 inline void dfs(int u)
 25 {
 26     in[u]=++cnt;
 27     for(int i=1;fc[i]<=dep[u];++i) fa[i][u]=fa[i-1][fa[i-1][u]];
 28     for(int i=0,len=G[u].size();i<len;++i)
 29     {
 30         dep[G[u][i]]=dep[u]+1;fa[0][G[u][i]]=u;
 31         dfs(G[u][i]);    
 32     } 
 33     out[u]=++cnt;
 34 }
 35 
 36 inline int LCA(int x,int y)
 37 {
 38     if(dep[x]<dep[y]) swap(x,y);
 39     for(int i=0,t=dep[x]-dep[y];t;++i)
 40         if(t&fc[i]) x=fa[i][x],t^=fc[i];
 41     for(int i=20;~i;--i) if(fa[i][x]^fa[i][y])
 42         x=fa[i][x],y=fa[i][y];
 43     return x==y? x:fa[0][x];
 44 }
 45 
 46 namespace Segment
 47 {
 48     #define ls (rt<<1)
 49     #define rs (rt<<1|1)
 50     PII T[maxn<<2];
 51     
 52     bool anc(int x,int y){return in[x]<=in[y]&&out[x]>=out[y];}
 53     
 54     PII check(PII x,int y)
 55     {
 56         if(!x.fi || !y) return mkp(0,0);
 57         if(anc(x.fi,x.se)) swap(x.fi,x.se);
 58         if(anc(x.se,x.fi))
 59         {
 60             if(anc(x.fi,y)) return mkp(y,x.se);
 61             if(anc(x.se,y))
 62             {
 63                 if(anc(y,x.fi)) return x;
 64                 if(LCA(x.fi,y)==x.se) return mkp(x.fi,y);
 65                 return mkp(0,0);
 66             }
 67             return mkp(y,x.fi);
 68         }
 69         
 70         if(anc(x.fi,y)) return mkp(y,x.se);
 71         if(anc(x.se,y)) return mkp(x.fi,y);
 72         if(!anc(LCA(x.fi,x.se),y)) return mkp(0,0);
 73         if(!anc(y,x.fi) && !anc(y,x.se)) return mkp(0,0);
 74         return x;
 75     }
 76     
 77     PII merge(PII x,PII y)
 78     {
 79         if(x.fi==-1) return y;
 80         x=check(x,y.fi);x=check(x,y.se);
 81         return x;
 82     }
 83     
 84     void update(int rt,int l,int r,int pos,int x)
 85     {
 86         if(l==r){T[rt]=mkp(x,x);return ;}
 87         int mid=l+r>>1;
 88         if(pos<=mid) update(ls,l,mid,pos,x);
 89         else update(rs,mid+1,r,pos,x);
 90         T[rt]=merge(T[ls],T[rs]);
 91     }
 92     
 93     bool query(int rt,int l,int r)
 94     {
 95         PII tmp=merge(res,T[rt]);
 96         if(tmp.fi){res=tmp;ans=r;return true;}
 97         if(l==r) return false;//
 98         int mid=l+r>>1;
 99         if(query(ls,l,mid)) query(rs,mid+1,r);
100         return false; 
101     }
102 }
103 using namespace Segment;
104 
105 int main()
106 {
107     n=read(); fc[0]=1;
108     for(int i=1;i<=n;++i) a[i]=read()+1;
109     for(int i=2;i<=n;++i) G[read()].pb(i);
110     for(int i=1;i<=20;++i) fc[i]=fc[i-1]<<1;
111     dfs(1);
112     for(int i=1;i<=n;++i) update(1,1,n,a[i],i);
113     q=read();
114     while(q--)
115     {
116         typ=read();
117         if(typ==1)
118         {
119             x=read();y=read();swap(a[x],a[y]);
120             update(1,1,n,a[x],x);update(1,1,n,a[y],y);
121         }
122         else
123         {
124             res=mkp(-1,0);ans=1;query(1,1,n);
125             printf("%d\n",ans);
126         }
127     }
128     
129     return 0;
130 }
View Code

;

猜你喜欢

转载自www.cnblogs.com/songorz/p/10196827.html
max
今日推荐