G - 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

3
1 2 3
2 1 3

Output

2 0 1 

Input

5
3 1 4 2 5
4 5 1 3 2

Output

3 2 0 0 0 

Input

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

Output

1 1 2 0 1 1 

Note

The first example is described in the statement.

In the second example, during the first step Vasya will move the books [3,1,4][3,1,4] . After that only books 22 and 55 remain in the stack (22 is above 55 ). During the second step Vasya will take the books 22 and 55 . After that the stack becomes empty, so during next steps Vasya won't move any books.

#include<bits/stdc++.h>
using namespace std;
int vis[200020];
int a[200020];
int b[200020];

int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		vis[a[i]]=i;
	}
	for(int i=1;i<=n;i++){
		cin>>b[i];
	}
	for(int i=1;i<=n;i++){
		if(a[vis[b[i]]]){
			for(int j=vis[b[i]];j>=0;j--){
				if(a[j]) a[j]=0;
				else {
					printf("%d ",vis[b[i]]-j);
					break;
				}
			}
		}
		else printf("0 ");
	}
	return 0;
}

此题用VIS数组反过来从值对应位置,是个很好的想法。

根据位置的差来计算移动的数目,而另一个择超时了。是一个个加上去的。

#include<bits/stdc++.h>
using namespace std;
int a[200020];
int b[200020];
int vis[200020];
int main(){
	int n;
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&b[i]);
		int num=1;
		if(vis[b[i]]) {
				printf("0 ");
				continue;
			}
		for(int j=0;j<n;j++){
			
			
			if(a[j]!=b[i]&&!vis[a[j]]){
				
				vis[a[j]]=1;
				num++;
			}
			else if(a[j]!=b[i]&&vis[a[j]]) continue;
			else {
				break;
			}
		}
		vis[b[i]]=1;
		printf("%d ",num);
	
	}
	return 0;
	
	
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值