E - Yet Another Card Deck
You have a card deck of nn cards, numbered from top to bottom, i. e. the top card has index 11 and bottom card — index nn. Each card has its color: the ii-th card has color a_iai.
You should process qq queries. The jj-th query is described by integer t_jtj. For each query you should:
- find the highest card in the deck with color t_jtj, i. e. the card with minimum index;
- print the position of the card you found;
- take the card and place it on top of the deck.
Input
The first line contains two integers nn and qq (2 \le n \le 3 \cdot 10^52≤n≤3⋅105; 1 \le q \le 3 \cdot 10^51≤q≤3⋅105) — the number of cards in the deck and the number of queries.
The second line contains nn integers a_1, a_2, \dots, a_na1,a2,…,an (1 \le a_i \le 501≤ai≤50) — the colors of cards.
The third line contains qq integers t_1, t_2, \dots, t_qt1,t2,…,tq (1 \le t_j \le 501≤tj≤50) — the query colors. It's guaranteed that queries ask only colors that are present in the deck.
Output
Print qq integers — the answers for each query.
Example
Input
7 5 2 1 1 4 3 3 1 3 2 1 1 4Output
5 2 3 1 5Note
Description of the sample:
- the deck is [2, 1, 1, 4, \underline{3}, 3, 1][2,1,1,4,3,3,1] and the first card with color t_1 = 3t1=3 has position 55;
- the deck is [3, \underline{2}, 1, 1, 4, 3, 1][3,2,1,1,4,3,1] and the first card with color t_2 = 2t2=2 has position 22;
- the deck is [2, 3, \underline{1}, 1, 4, 3, 1][2,3,1,1,4,3,1] and the first card with color t_3 = 1t3=1 has position 33;
- the deck is [\underline{1}, 2, 3, 1, 4, 3, 1][1,2,3,1,4,3,1] and the first card with color t_4 = 1t4=1 has position 11;
- the deck is [1, 2, 3, 1, \underline{4}, 3, 1][1,2,3,1,4,3,1] and the first card with color t_5 = 4t5=4 has position 55.
Sponsor
//这题虽然给了两秒,但数据很大,而且一看就必须得嵌套循环
//普通位置对应颜色的筛查应该是行不通,但这题良心的只给了
//50种颜色...这不很明显嘛,用颜色标记位置!走起~
#include <iostream>
#include<algorithm>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int color[51] = { 0 };
int n, q;
cin >> n >> q;
int eveco, * answer;//储存答案
answer = (int*)malloc(q * sizeof(int));
for (int i = 1; i <= n; i++) {//储存颜色种类对应第一次出现的位置
cin >> eveco;
if (color[eveco] == 0) {
color[eveco] = i;
}
}
int t, j = 0;
for (int i = 0; i < q; i++) {
cin >> t;
answer[j++] = color[t];//储存答案
for (int k = 1; k <= 50; k++) {
if (color[k] < color[t] && color[k] != 0) {//比筛查样例小的位置+1
color[k]++;
}
else continue;
}
color[t] = 1;//把筛查样例放在最前面
}
for (int h = 0; h < j; h++) {
cout << answer[h] << ' ';
}
return 0;
}