Educational Codeforces Round 53 (Rated for Div. 2) B. Vasya and Books

B. Vasya and Books

 

Vasya has got nn books, numbered from 11 to nn, arranged in a stack. The topmost book has number a1a1, the next one — a2a2, and so on. The book at the bottom of the stack has number anan. All numbers are distinct.

Vasya wants to move all the books to his backpack in nn steps. During ii-th step he wants to move the book number bibi into his backpack. If the book with number bibi is in the stack, he takes this book and all the books above the book bibi, and puts them into the backpack; otherwise he does nothing and begins the next step. For example, if books are arranged in the order [1,2,3][1,2,3] (book 11 is the topmost), and Vasya moves the books in the order [2,1,3][2,1,3], then during the first step he will move two books (11 and 22), during the second step he will do nothing (since book 11 is already in the backpack), and during the third step — one book (the book number 33). Note that b1,b2,…,bnb1,b2,…,bn are distinct.

Help Vasya! Tell him the number of books he will put into his backpack during each step.

Input

The first line contains one integer n (1≤n≤2⋅105)n (1≤n≤2⋅105) — the number of books in the stack.

The second line contains nn integers a1,a2,…,an (1≤ai≤n)a1,a2,…,an (1≤ai≤n) denoting the stack of books.

The third line contains nn integers b1,b2,…,bn (1≤bi≤n)b1,b2,…,bn (1≤bi≤n) denoting the steps Vasya is going to perform.

All numbers a1…ana1…an are distinct, the same goes for b1…bnb1…bn.

Output

Print nn integers. The ii-th of them should be equal to the number of books Vasya moves to his backpack during the ii-th step.

Examples

input

Copy

3
1 2 3
2 1 3

output

Copy

2 0 1 

input

Copy

5
3 1 4 2 5
4 5 1 3 2

output

Copy

3 2 0 0 0 

input

Copy

6
6 5 4 3 2 1
6 5 3 4 2 1

output

Copy

1 1 2 0 1 1 

题意:给你一摞书,从上到下序号依次为a[i],现在从b[1]开始,在a[i]中找到b[1]这本书,以此书所在位置左边全部书移除(包括当前这本),输出这次移除总数。之后是[b2],[b3]等等,如果没有这本书,则输出0。

思路:暴力,每次把要移除的书的位置找到,扫描a[i],包括它左边全部变成0。每次扫描碰到0就break,所以对任意一本书,我们只扫描一次,时间复杂度O(n)。

关键在于怎么找位置,开个vis数组,记录vis[a[i]]即可。之后vis[b[i]]直接反向定位在a[i]中的位置即可。

不难- -

​
#include 
#define ll long long
using namespace std;
int a[200020],b[200020],vis[200020],n;
int main()
{
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));
        memset(a,0,sizeof(a));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            vis[a[i]]=i;   //任意一本书在a中的位置
        }
        for(int i=1;i<=n;i++)scanf("%d",&b[i]);

        for(int i=1;i<=n;i++)
        {
            if(a[vis[b[i]]])   //这本书在a的位置下存在
            {
                for(int j=vis[b[i]];j>=0;j--)   //从当前位置扫描到0
                {
                    if(a[j])a[j]=0;
                    else
                    {
                        printf("%d ",vis[b[i]]-j);   //当前位置-最右边0位置就是移除总数
                        break;
                    }
                }
            }
            else printf("0 ");
        }
    }
    return 0;
}

​

 

你可能感兴趣的:(Codeforces)