Codeforces-1326E Bombs

Codeforces-1326E Bombs

You are given a permutation, \(p_1, p_2, \ldots, p_n\).

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 \(p_i\) 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, \(q_1, q_2, \ldots, q_n\).

For each \(1 \leq i \leq n\), find the cost of a configuration of bombs such that there exists a bomb in positions \(q_1, q_2, \ldots, q_{i-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 \(q_1, q_2, \ldots, q_{n-1}\).

Input

The first line contains a single integer, \(n\) (\(2 \leq n \leq 300\,000\)).

The second line contains \(n\) distinct integers \(p_1, p_2, \ldots, p_n\) (\(1 \leq p_i \leq n)\).

The third line contains \(n\) distinct integers \(q_1, q_2, \ldots, q_n\) (\(1 \leq q_i \leq n)\).

Output

Print \(n\) space-separated integers, such that the \(i\)-th of them equals the cost of a configuration of bombs in positions \(q_1, q_2, \ldots, q_{i-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 \(q_1 = 5\), \(q_2 = 2\), and \(q_3 = 1\).

At the beginning, \(A = \{\}\).

  • Operation \(1\): Add \(p_1 = 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 \(p_2 = 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 \(p_3 = 6\) to \(A\), so \(A\) is equal to \(\{6\}\). There is no bomb in position \(3\), so we do nothing.
  • Operation \(4\): Add \(p_4 = 1\) to \(A\), so \(A\) is equal to \(\{1, 6\}\). There is no bomb in position \(4\), so we do nothing.
  • Operation \(5\): Add \(p_5 = 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 \(p_6 = 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\).

题意

给定一个排列p,再给定一个q,表示了n颗炸弹,每次操作添加\(q_1, q_2, \ldots, q_{i-1}\)的炸弹,并向\(A[]\)添加一个数\(p_i\),第\(i\)颗炸弹会在加入p中第\(q_i\)个元素的时候引爆,使\(A[]\)中最大的元素消失,问每一次操作之后\(A[]\)中最大的元素是多少

题解

显然第一次操作之后答案就是n,因为没有炸弹,答案为最大值.

注意到每添加一颗炸弹,炸弹的对应关系会基本打乱,所以正常维护不可能做到

可以观察到,每一次操作之后的答案是递减的,也就是说如果我们能快速算出能否使答案为\(x\)成立,就可以在\(O(n*f(计算答案为x是否成立))\)的复杂度内算出答案

我们要使答案x成立,就要炸掉所有\(\ge x\)的值

我们维护一个数组,表示\(i \dots n 中 \ge x的数量-炸弹的数量\),如果对于每一个i这个值都满足\(\le 0\)的话,就说明答案为\(x\)不成立 ,因为所有\(\ge x\)的值都被炸掉了,我们令\(x-1\),同时加入值为\(x-1\)的数对数组的影响即可

这个数组用线段树维护即可,复杂度\(O(nlogn)\)

代码

#include <bits/stdc++.h>
#define lson (o << 1)
#define rson (o << 1 | 1)
using namespace std;
typedef long long ll;
const int N = 3e5 + 50;
int p[N], q[N];
int pos[N];
int maxv[N << 2];
int addv[N << 2];
void pushup(int o) {
    maxv[o] = max(maxv[lson], maxv[rson]);
}
void pushdown(int o) {
    if (addv[o]) {
        addv[lson] += addv[o]; addv[rson] += addv[o];
        maxv[lson] += addv[o]; maxv[rson] += addv[o];
        addv[o] = 0; 
    }
}
void update(int o, int l, int r, int ql, int qr, int v) {
    if (ql <= l && r <= qr) {
        maxv[o] += v;
        addv[o] += v;
        return;
    }
    pushdown(o);
    int mid = (l + r) >> 1;
    if (ql <= mid) update(lson, l, mid, ql, qr, v);
    if (qr > mid) update(rson, mid + 1, r, ql, qr, v);
    pushup(o);
}
int main() {
    int n; scanf("%d", &n);
    for (int i = 1; i <= n; i++) scanf("%d", &p[i]), pos[p[i]] = i;
    for (int i = 1; i <= n; i++) scanf("%d", &q[i]);
    int ans = n; printf("%d", n);
    update(1, 1, n, 1, pos[ans], 1);
    for (int i = 1; i < n; i++) {
        update(1, 1, n, 1, q[i], -1);
        while (maxv[1] <= 0) {
            ans--;
            update(1, 1, n, 1, pos[ans], 1);
        }
        printf(" %d", ans);
    }
    return 0;
}
posted @ 2020-03-29 18:30  Artoriax  阅读(204)  评论(0编辑  收藏  举报