struct Treap {
int lef, rgh, pos;
} fhq[100005 * 205];
int root = 0, tot = 0;
queue<int> que;
int tmp[100005], cnt = 0;
int Build(int l, int r) {
if (l > r)return 0;
int ans = que.empty() ? ++tot : que.front();
if (!que.empty())que.pop();
int mid = (l + r) / 2;
fhq[ans] = {0, 0, tmp[mid]};
fhq[ans].lef = Build(l, mid - 1);
fhq[ans].rgh = Build(mid + 1, r);
return ans;
}
void Split(int p, int val, int &x, int &y) {
if (p == 0) {
x = y = 0;
return;
}
if (fhq[p].pos <= val) {
x = p;
Split(fhq[p].rgh, val, fhq[p].rgh, y);
} else {
y = p;
Split(fhq[p].lef, val, x, fhq[p].lef);
}
}
int Merge(int x, int y) {
if (x == 0 || y == 0)return x + y;
if (rand() % 2) {
fhq[x].rgh = Merge(fhq[x].rgh, y);
return x;
} else {
fhq[y].lef = Merge(x, fhq[y].lef);
return y;
}
}