CSES - Shared codeLink to this code: https://cses.fi/paste/b59079c11590563a4035bb/
#include <bits/stdc++.h>
using pii=std::pair<int,int>;
using namespace std;

const int maxn = 2e5 + 5, maxbit = 17;

int n, x[maxn], p, bit[maxn];

void add(int idx, int val)
{
    for(++idx; idx < maxn; idx += idx & -idx)
        bit[idx] += val;
}

int get_fi_pos(int val)
{
    int curpos = 0, cursum = 0;
    for(int j = maxbit; j >= 0; j--)
        if(curpos + (1ll << j) < maxn && cursum + bit[curpos + (1ll << j)] < val)
        {
            cursum += bit[curpos + (1ll << j)];
            curpos += (1ll << j);
        }
    return curpos;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> x[i];
        add(i, 1);
    }
    for(int i = 0; i < n; i++)
    {
        cin >> p;
        int pos = get_fi_pos(p);
        cout << x[pos] << " ";
        add(pos, -1);
    }
    cout << "\n";
    return 0;
}