// compile: make data
// run: ./data < data.in
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#ifdef LOCAL
#include <debug/codeforces.h>
#define debug(x...) _debug_print(#x, x);
#define Debug(x...) _debug_print_format(#x, x);
#else
#define debug(x...)
#define Debug(x...)
#endif
template<typename...Args> void print_(Args...args){((cout<<args<<" "),...)<<endl;}
#define VI vector<int>
#define VII vector<vector<int>>
#define VIII vector<vector<vector<int>>>
#define rep(i,a,b) for(int i=(a);i<(int)(b);++i)
#define sz(v) ((int)(v).size())
#define print(...) print_(__VA_ARGS__);
#define FIND(a, x) ((find(a.begin(),a.end(),(x))!=a.end())?1:0)
#define cmin(x,...) x=min({(x), __VA_ARGS__})
#define cmax(x,...) x=max({(x), __VA_ARGS__})
#define INTMAX (int)(9223372036854775807)
#define INF (int)(1152921504606846976)
#define NaN (int)(0x8b88e1d0595d51d1)
#define double long double
#define int long long
#define MAXN 200010
struct Splay {
struct node {
int rk, size, val;
node *ch[2], *parent;
};
node *newnode(int rank, int val) {
node *x = &datalist[cnt++];
x->rk = rank, x->val = val;
x->ch[0] = x->ch[1] = x->parent = nullptr;
return x;
}
node *root = nullptr;
int cnt = 0;
vector<node> datalist;
Splay(int n) {
datalist.resize(n);
}
int get(node *x) {return x->parent->ch[1] == x; }
int getsize(node *x) {return x ? x->size : 0; }
void pushup(node *x) {
if (!x) return;
x->size = getsize(x->ch[0]) + getsize(x->ch[1]) + 1;
}
void pushdown(node *x) {
}
void rotate(node *x) {
node *y = x->parent;
node *z = y->parent;
int t = get(x);
y->ch[t] = x->ch[t ^ 1];
if (x->ch[t ^ 1]) x->ch[t ^ 1]->parent = y;
x->ch[t ^ 1] = y;
y->parent = x;
if (z) z->ch[y == z->ch[1]] = x;
x->parent = z;
pushup(y);
pushup(x);
}
void splay(node *x, node *target) {
for (node *f; f = x->parent, f != target; rotate(x)) {
if (f->parent != target) rotate(get(x) == get(f) ? f : x);
}
if (!target) root = x;
}
void insert(int rank, int val) {
if (!root) {
root = newnode(rank, val);
return;
}
node *x = root;
for (; ; x = x->ch[rank > x->rk]) {
if (!x->ch[rank > x->rk]) {
x->ch[rank > x->rk] = newnode(rank, val);
x->ch[rank > x->rk]->parent = x;
//
pushup(x->ch[rank > x->rk]);
pushup(x);
//
splay(x->ch[rank > x->rk], nullptr);
return;
}
}
}
node *kth(int k) { // 0-index
node *x = root; pushdown(x);
for (pushdown(x); ; pushdown(x)) {
if (k == getsize(x->ch[0])) return x;
if (k < getsize(x->ch[0])) x = x->ch[0];
else k -= getsize(x->ch[0]) + 1, x = x->ch[1];
}
return nullptr;
}
void remove(int l, int r) {
splay(kth(l - 1), nullptr);
splay(kth(r + 1), root);
root->ch[1]->ch[0] = nullptr;
splay(root->ch[1], nullptr);
}
void graphviz_dump(string filename = "graph.dot") {
FILE *f = fopen(filename.c_str(), "w");
fprintf(f, "digraph {\n");
rep(i, 0, cnt) {
node *x = &datalist[i];
fprintf(f, " %lld;\n");
}
rep(i, 0, cnt) {
node *x = &datalist[i];
if (x->ch[0]) fprintf(f, " %lld -> %ld;\n", i, x->ch[0] - &datalist[0]);
if (x->ch[1]) fprintf(f, " %lld -> %ld;\n", i, x->ch[1] - &datalist[0]);
}
fprintf(f, "}\n");
}
};
int32_t main() {
ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
int n; cin >> n;
Splay t(n + 2);
rep(i, 0, n) {
int x; cin >> x;
t.insert(i+1, x);
}
t.insert(0, 0), t.insert(n+1, 0);
rep(i, 0, n) {
int x; cin >> x;
cout << t.kth(x)->val << " \n"[i==n-1];
t.remove(x, x);
}
return 0;
}
No Comments