Problem: B: Pivots

problem

Given a permutation of length N, a_1, a_2, ..., a_N, which is a permutation of integers from 1 to N. Also, Q queries are given in order for this permutation. In the i-th query, you have to do the following:

* The value q_i (1 \ leq q_i \ leq N) is given. In the permutation \\ {a_1, a_2, ..., a_N \\}, where L is the permutation on the left side of q_i and R is the permutation on the right side of q_i, the original permutation L \\ q_i \\ R is R \\ Change to q_i \\ L. That is, when q_ {i} = a_j, the permutations \\ {a_1, ..., a_ {j-1}, a_j, a_ {j + 1}, ..., a_N \\} are \\ { Change to a_ {j + 1}, ..., a_N, a_j, a_1, ..., a_ {j-1} \\}.



The permutations L and R may be empty. For example, if L is empty, change q_i \\ R to R \\ q_i. The same is true when R is empty.

Output the permutation after processing these Q queries in order for the given permutation on one line.

Input format


N Q
a_1 a_2 ... a_N
q_1 q_2 ... q_Q


All inputs are integers.

The first row gives the number of elements in the permutation N and the number of queries Q, separated by blanks. In the second line, permutations a_1, a_2, ..., a_N, in which integers from 1 to N are rearranged, are given separated by blanks. The third line gives Q queries, separated by blanks. q_i represents the i-th query.

Constraint

* 1 \ leq N \ leq 10 ^ 5
* 1 \ leq Q \ leq 10 ^ 5
* 1 \ leq a_i \ leq N
* a_i is different
* 1 \ leq q_i \ leq N



Output format

Output the permutation after processing all the queries in order on one line.

Input example 1


5 2
1 5 3 2 4
5 2


Output example 1


4 5 1 2 3

* The first query changes the permutation to \\ {3, 2, 4, 5, 1 \\}.
* The second query changes the permutation to \\ {4, 5, 1, 2, 3 \\}.



Input example 2


5 1
1 2 3 4 5
Five


Output example 2


5 1 2 3 4





Example

Input

5 2
1 5 3 2 4
5 2


Output

4 5 1 2 3

Solution (in CPP):

Solution:
/*  -*- coding: utf-8 -*-
 *
 * 2890.cc: Pivots
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;

/* typedef */

typedef vector<int> vi;
typedef queue<int> qi;
typedef pair<int,int> pii;

struct Node {
  int v;
  Node *lpt, *rpt, *ppt;
  Node() {}

  void print() {
    printf("(%d:P%d,L%d,R%d)",
	   v, ppt ? ppt->v : -1, lpt ? lpt->v : -1, rpt ? rpt->v : -1);
  }
};

/* global variables */

int as[MAX_N];
Node buf[MAX_N];

/* subroutines */

Node *maketree(int i0, int i1) {
  if (i0 >= i1) return NULL;
  int im = (i0 + i1) / 2;
  Node *npt = buf + as[im];
  npt->v = as[im];
  npt->ppt = NULL;
  if ((npt->lpt = maketree(i0, im)) != NULL) npt->lpt->ppt = npt;
  if ((npt->rpt = maketree(im + 1, i1)) != NULL) npt->rpt->ppt = npt;
  return npt;
}

void traverse(vi &v, Node *npt) {
  if (npt->lpt != NULL) traverse(v, npt->lpt);
  v.push_back(npt->v);
  if (npt->rpt != NULL) traverse(v, npt->rpt);
}

void printtree(Node *npt) {
  vi v;
  traverse(v, npt);
  //printf("root=%d: ", npt->v);
 for (int i = 0; i < v.size(); i++) {
    if (i) putchar(' ');
    printf("%d", v[i] + 1);
    //buf[v[i]].print();
  }
  putchar('\n');
}

Node *rot_l(Node *npt0) {
  if (npt0 == NULL || npt0->rpt == NULL) return npt0;
  Node *npt1 = npt0->rpt;
  npt0->rpt = npt1->lpt;
  if (npt1->lpt) npt1->lpt->ppt = npt0;
  npt1->lpt = npt0;
  npt1->ppt = npt0->ppt; npt0->ppt = npt1;
  if (npt1->ppt) {
    if (npt1->ppt->lpt == npt0) npt1->ppt->lpt = npt1;
    else npt1->ppt->rpt = npt1;
  }
  return npt1;
}

Node *rot_r(Node *npt0) {
  if (npt0 == NULL || npt0->lpt == NULL) return npt0;
  Node *npt1 = npt0->lpt;
  npt0->lpt = npt1->rpt;
  if (npt1->rpt) npt1->rpt->ppt = npt0;
  npt1->rpt = npt0;
  npt1->ppt = npt0->ppt; npt0->ppt = npt1;
  if (npt1->ppt) {
    if (npt1->ppt->lpt == npt0) npt1->ppt->lpt = npt1;
    else npt1->ppt->rpt = npt1;
  }
  return npt1;
}

Node *maketop(Node *npt) {
  while (npt->ppt != NULL) {
    Node *npt0 = npt->ppt;
    if (npt0->lpt == npt) rot_r(npt0);
    else rot_l(npt0);
  }
  return npt;
}

/* main */

int main() {
  int n, qn;
  scanf("%d%d", &n, &qn);
  for (int i = 0; i < n; i++)
    scanf("%d", as + i), as[i]--;

  Node *root = maketree(0, n);
  //printtree(root);

  while (qn--) {
    int ai;
    scanf("%d", &ai);
    ai--;

    root = maketop(&buf[ai]);
    swap(root->lpt, root->rpt);
    //printtree(root);
  }

  printtree(root);
  return 0;
}