You are given a permutation, p1,p2,…,pn.
Imagine that some positions of the permutation contain bombs, such that there exists at least one position without a bomb.
For some fixed configuration of bombs, consider the following process. Initially, there is an empty set, A.
For each i from 1 to n:
Add pi to A.
If the i-th position contains a bomb, remove the largest element in A.
After the process is completed, A will be non-empty. The cost of the configuration of bombs equals the largest element in A.
You are given another permutation, q1,q2,…,qn.
For each 1≤i≤n, find the cost of a configuration of bombs such that there exists a bomb in positions q1,q2,…,qi−1.
For example, for i=1, you need to find the cost of a configuration without bombs, and for i=n, you need to find the cost of a configuration with bombs in positions q1,q2,…,qn−1.
Input
The first line contains a single integer, n (2≤n≤300000).
The second line contains n distinct integers p1,p2,…,pn (1≤pi≤n).
The third line contains n distinct integers q1,q2,…,qn (1≤qi≤n).
Output
Print n space-separated integers, such that the i-th of them equals the cost of a configuration of bombs in positions q1,q2,…,qi−1.
Examples
Input
3
3 2 1
1 2 3
Output
3 2 1
Input
6
2 3 6 1 5 4
5 2 1 4 6 3
Output
6 5 5 5 4 1
Note
In the first test:
If there are no bombs, A is equal to {1,2,3} at the end of the process, so the cost of the configuration is 3.
If there is one bomb in position 1, A is equal to {1,2} at the end of the process, so the cost of the configuration is 2;
If there are two bombs in positions 1 and 2, A is equal to {1} at the end of the process, so the cost of the configuration is 1.
In the second test:
Let’s consider the process for i=4. There are three bombs on positions q1=5, q2=2, and q3=1.
At the beginning, A={}.
Operation 1: Add p1=2 to A, so A is equal to {2}. There exists a bomb in position 1, so we should delete the largest element from A. A is equal to {}.
Operation 2: Add p2=3 to A, so A is equal to {3}. There exists a bomb in position 2, so we should delete the largest element from A. A is equal to {}.
Operation 3: Add p3=6 to A, so A is equal to {6}. There is no bomb in position 3, so we do nothing.
Operation 4: Add p4=1 to A, so A is equal to {1,6}. There is no bomb in position 4, so we do nothing.
Operation 5: Add p5=5 to A, so A is equal to {1,5,6}. There exists a bomb in position 5, so we delete the largest element from A. Now, A is equal to {1,5}.
Operation 6: Add p6=4 to A, so A is equal to {1,4,5}. There is no bomb in position 6, so we do nothing.
In the end, we have A={1,4,5}, so the cost of the configuration is equal to 5.
题意蛮难理解的,所以还是自己看样例理解了。
思路:我们可以看出来,答案是呈非递增形式的。对于每一次新加进来的炸弹,我们需要判断对当前答案有没有影响。什么时候有影响呢?假设每次新加进来的炸弹位置为q,那么它所影响的位置为[1,q],只有在[posans,n]内有炸弹,才有可能影响到当前的答案。炸弹会先作用于比ans大的数,所以只要右边大于等于ans的数个数大于右边的炸弹数就行。线段树维护区间最大值就可以了。
代码如下:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxx=3e5+100;
struct node{
int l,r,_max,lazy;
}p[maxx<<2];
int pos[maxx],q[maxx];
int n;
inline void pushup(int cur)
{
p[cur]._max=max(p[cur<<1]._max,p[cur<<1|1]._max);
}
inline void pushdown(int cur)
{
if(p[cur].lazy)
{
p[cur<<1].lazy+=p[cur].lazy;
p[cur<<1|1].lazy+=p[cur].lazy;
p[cur<<1]._max+=p[cur].lazy;
p[cur<<1|1]._max+=p[cur].lazy;
p[cur].lazy=0;
}
}
inline void build(int l,int r,int cur)
{
p[cur].l=l;
p[cur].r=r;
p[cur]._max=p[cur].lazy=0;
if(l==r) return ;
int mid=l+r>>1;
build(l,mid,cur<<1);
build(mid+1,r,cur<<1|1);
}
inline void update(int l,int r,int cur,int v)
{
int L=p[cur].l;
int R=p[cur].r;
if(l<=L&&R<=r)
{
p[cur].lazy+=v;
p[cur]._max+=v;
return ;
}
pushdown(cur);
int mid=L+R>>1;
if(r<=mid) update(l,r,cur<<1,v);
else if(l>mid) update(l,r,cur<<1|1,v);
else update(l,mid,cur<<1,v),update(mid+1,r,cur<<1|1,v);
pushup(cur);
}
int main()
{
scanf("%d",&n);
int x;
for(int i=1;i<=n;i++) scanf("%d",&x),pos[x]=i;
for(int i=1;i<=n;i++) scanf("%d",&q[i]);
build(1,n,1);
printf("%d ",n);
update(1,pos[n],1,1);
int ans=n;
for(int i=1;i<n;i++)
{
update(1,q[i],1,-1);
while(p[1]._max<=0) update(1,pos[--ans],1,1);
printf("%d ",ans);
}
return 0;
}
努力加油a啊,(o)/~