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:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:https://onlinejudge.u-aizu.ac.jp/services/room.html#ACPC2018Day3/problems/B>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

/*
 1-index
 RangeAdd : [s,t] に xを加算
 RangeSum : [s,t] の総和を出力
 */
struct BIT {
    int N;
    vector<ll> bit1;
    vector<ll> bit2;
    BIT(int N):N(N) {
        /* BITは[1..N]で扱う */
        bit1.resize(N + 1, 0);
        bit2.resize(N + 1, 0);
    }
    
    void add1(int x, ll val){
        while (x <= N) {
            bit1[x] += val;
            x += x & -x;
        }
    }
    void add2(int x, ll val){
        while (x <= N) {
            bit2[x] += val;
            x += x & -x;
        }
    }
    
    // [l,r]
    void RangeAdd(int l,int r,ll val){
        // Update BIT1
        add1(l,val);
        add1(r+1,-val);
        
        // Update BIT2
        add2(l,val*(l-1));
        add2(r+1,-val*r);
    }
    void RangeAdd(int v,ll val){
        RangeAdd(v,v,val);
    }
    
    ll sum1(int x){
        ll ret = 0;
        while (x) {
            ret += bit1[x];
            x &= (x - 1);
        }
        return (ret);
    }
    
    ll sum2(int x){
        ll ret = 0;
        while (x) {
            ret += bit2[x];
            x &= (x - 1);
        }
        return (ret);
    }
    
    ll sum(int x){
        return sum1(x)*x - sum2(x);
    }
    // [l,r]
    ll RangeSum(int l,int r){
        return sum(r) - sum(l-1);
    }
    ll RangeSum(int v){
        return sum(v) - sum(v-1);
    }
};


struct node{
    int prev,v,next;
    node(int prev = 0,int v = 0,int next = 0):prev(prev),v(v),next(next){}
};
vector<int> solve(){
    vector<int> res;
    int N,Q; cin >> N >> Q;
    vector<int> a(N+2); a[0] = 0; a[N+1] = N+1;
    for(int i = 1; i <= N;i++) cin >> a[i];
    
    vector<node> X(N+2);
    X[0] = node(INF,0,a[1]);
    X[N+1] = node(a[N],N+1,INF);
    for(int i = 1; i <= N;i++){
        X[a[i]] = node(a[i-1],a[i],a[i+1]);
    }
    
    if(N == 1) {
        res.push_back(1);
        return res;
    }
    
//    for(int i = 0; i <= N+1;i++){
//        cout << i << " : " << X[i].prev << "," << X[i].v << "," << X[i].next << endl;
//    }
    while(Q--){
        ll q; cin >> q;
        ll prev = X[q].prev;
        ll v = q;
        ll next = X[q].next;
        
        if(prev != 0 && next != N+1){
            node nodel1 = X[X[0].next];
            node noder1 = X[prev];
            node nodev = X[v];
            node nodel2 = X[next];
            node noder2 = X[X[N+1].prev];
            
            
            X[0].next = nodel2.v;
            X[nodel1.v].prev = nodev.v;
            X[noder1.v].next = X[N+1].v;
            X[nodev.v].prev = noder2.v;
            X[nodev.v].next = nodel1.v;
            X[nodel2.v].prev = X[0].v;
            X[noder2.v].next = nodev.v;
            X[N+1].prev = noder1.v;
        }
        if(prev == 0){
            node nodev = X[v];
            node nodel = X[next];
            node noder = X[X[N+1].prev];
            
            X[0].next = nodel.v;
            X[nodel.v].prev = X[0].v;
            X[noder.v].next = nodev.v;
            X[nodev.v].prev = noder.v;
            X[nodev.v].next = X[N+1].v;
            X[N+1].prev = nodev.v;
        }
        if(next == N+1){
            node nodel = X[X[0].next];
            node noder = X[prev];
            node nodev = X[v];
            
            X[0].next = nodev.v;
            X[nodev.v].prev = X[0].v;
            X[nodev.v].next = nodel.v;
            X[nodel.v].prev = nodev.v;
            X[noder.v].next = X[N+1].v;
            X[N+1].prev = noder.v;
            
        }
        
//        for(int i = 0; i <= N+1;i++){
//            cout << i << " : " << X[i].prev << "," << X[i].v << "," << X[i].next << endl;
//        }
    }
    int ind = X[0].next;
    while(ind != N+1){
        res.push_back(ind);
        ind = X[ind].next;
    }
    
    return res;
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    cout << solve() << endl;
    return 0;
}