#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// generated at caterpillow.github.io/byot
namespace Treap {
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
using ptr = struct Node*;
struct Node {
int key;
int sz;
int pri;
ptr l, r;
Node() {
pri = mt();
sz = 1;
l = r = nullptr;
}
Node(int key) : key(key) {
pri = mt();
sz = 1;
l = r = nullptr;
}
};
int sz(ptr n) { return n ? n->sz : 0; };
ptr pull(ptr n) {
if (!n) return nullptr;
n->sz = sz(n->l) + 1 + sz(n->r);
return n;
}
ptr merge(ptr l, ptr r) {
if (!l || !r) return l ? l : r;
if (l->pri > r->pri) return l->r = merge(l->r, r), pull(l);
else return r->l = merge(l, r->l), pull(r);
}
template<typename... Args>
ptr merge(ptr l, Args... args) {
return merge(l, merge(args...));
}
// [-inf, i) and [i, inf]
pair<ptr, ptr> spliti(ptr n, int i) {
if (!n) return {nullptr, nullptr};
if (i <= sz(n->l)) {
auto [l, r] = spliti(n->l, i);
n->l = r;
return {l, pull(n)};
} else {
auto [l, r] = spliti(n->r, i - 1 - sz(n->l));
n->r = l;
return {pull(n), r};
}
}
void heapify(ptr n) {
if (!n) return;
ptr mx = n;
if (n->l && n->l->pri > mx->pri) mx = n->l;
if (n->r && n->r->pri > mx->pri) mx = n->r;
if (mx != n) swap(n->pri, mx->pri), heapify(mx);
}
ptr build(vector<ptr>& ns, int l = 0, int r = -69) {
if (r == -69) r = (int) ns.size() - 1;
if (l > r) return nullptr;
if (l == r) return ns[l];
int m = (l + r) / 2;
ns[m]->l = build(ns, l, m - 1);
ns[m]->r = build(ns, m + 1, r);
heapify(ns[m]);
return pull(ns[m]);
}
template <typename Op>
void tour(ptr n, Op op) {
stack<ptr> stk;
while (n || !stk.empty()) {
for (; n; n = n->l) stk.push(n);
n = stk.top(); stk.pop();
op(n);
n = n->r;
}
}
}
using namespace Treap;
/*
- wrap in namespace
- include n-way merge (and merge)
- include split at index (and size)
- include heapify + build
- include tour
*/
int n;
ptr treap;
void random_shuffle(int a, int b) {
if (b < a) return;
int len = min(b - a, n - b);
int l0 = a;
int r0 = a + len;
int l1 = b;
int r1 = b + len;
auto [lxmy, r] = spliti(treap, r1);
auto [lxm, y] = spliti(lxmy, l1);
auto [lx, m] = spliti(lxm, r0);
auto [l, x] = spliti(lx, l0);
treap = Treap::merge(l, y, m, x, r); // merge with 5+ arguments requires namespace, since it conflicts with std::merge
}
main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
// O(n) build
vector<ptr> ns(n);
for (int i = 0; i < n; i++) ns[i] = new Node(i + 1);
treap = build(ns);
for (int i = 0; i < n; i++) {
int a, b; cin >> a >> b;
random_shuffle(a - 1, b - 1);
}
tour(treap, [&] (ptr n) { cout << n->key << ' '; });
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwp1c2luZyBsbCA9IGxvbmcgbG9uZzsKCi8vIGdlbmVyYXRlZCBhdCBjYXRlcnBpbGxvdy5naXRodWIuaW8vYnlvdAoKbmFtZXNwYWNlIFRyZWFwIHsKCiAgICBtdDE5OTM3IG10KGNocm9ubzo6c3RlYWR5X2Nsb2NrOjpub3coKS50aW1lX3NpbmNlX2Vwb2NoKCkuY291bnQoKSk7CiAgICB1c2luZyBwdHIgPSBzdHJ1Y3QgTm9kZSo7CiAgICAKICAgIHN0cnVjdCBOb2RlIHsKICAgIAogICAgICAgIGludCBrZXk7CiAgICAgICAgaW50IHN6OwogICAgICAgIGludCBwcmk7CiAgICAgICAgcHRyIGwsIHI7CiAgICAKICAgICAgICBOb2RlKCkgewogICAgICAgICAgICBwcmkgPSBtdCgpOwogICAgICAgICAgICBzeiA9IDE7CiAgICAgICAgICAgIGwgPSByID0gbnVsbHB0cjsKICAgICAgICB9CiAgICAKICAgICAgICBOb2RlKGludCBrZXkpIDoga2V5KGtleSkgewogICAgICAgICAgICBwcmkgPSBtdCgpOwogICAgICAgICAgICBzeiA9IDE7CiAgICAgICAgICAgIGwgPSByID0gbnVsbHB0cjsKICAgICAgICB9CiAgICB9OwogICAgCiAgICBpbnQgc3oocHRyIG4pIHsgcmV0dXJuIG4gPyBuLT5zeiA6IDA7IH07CiAgICAKICAgIHB0ciBwdWxsKHB0ciBuKSB7CiAgICAgICAgaWYgKCFuKSByZXR1cm4gbnVsbHB0cjsKICAgICAgICBuLT5zeiA9IHN6KG4tPmwpICsgMSArIHN6KG4tPnIpOwogICAgICAgIHJldHVybiBuOwogICAgfQogICAgCiAgICBwdHIgbWVyZ2UocHRyIGwsIHB0ciByKSB7CiAgICAgICAgaWYgKCFsIHx8ICFyKSByZXR1cm4gbCA/IGwgOiByOwogICAgICAgIGlmIChsLT5wcmkgPiByLT5wcmkpIHJldHVybiBsLT5yID0gbWVyZ2UobC0+ciwgciksIHB1bGwobCk7CiAgICAgICAgZWxzZSByZXR1cm4gci0+bCA9IG1lcmdlKGwsIHItPmwpLCBwdWxsKHIpOwogICAgfQogICAgCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZS4uLiBBcmdzPgogICAgcHRyIG1lcmdlKHB0ciBsLCBBcmdzLi4uIGFyZ3MpIHsKICAgICAgICByZXR1cm4gbWVyZ2UobCwgbWVyZ2UoYXJncy4uLikpOwogICAgfQogICAgCiAgICAvLyBbLWluZiwgaSkgYW5kIFtpLCBpbmZdCiAgICBwYWlyPHB0ciwgcHRyPiBzcGxpdGkocHRyIG4sIGludCBpKSB7CiAgICAgICAgaWYgKCFuKSByZXR1cm4ge251bGxwdHIsIG51bGxwdHJ9OwogICAgICAgIGlmIChpIDw9IHN6KG4tPmwpKSB7CiAgICAgICAgICAgIGF1dG8gW2wsIHJdID0gc3BsaXRpKG4tPmwsIGkpOwogICAgICAgICAgICBuLT5sID0gcjsKICAgICAgICAgICAgcmV0dXJuIHtsLCBwdWxsKG4pfTsKICAgICAgICB9IGVsc2UgewogICAgICAgICAgICBhdXRvIFtsLCByXSA9IHNwbGl0aShuLT5yLCBpIC0gMSAtIHN6KG4tPmwpKTsKICAgICAgICAgICAgbi0+ciA9IGw7CiAgICAgICAgICAgIHJldHVybiB7cHVsbChuKSwgcn07CiAgICAgICAgfQogICAgfQogICAgCiAgICB2b2lkIGhlYXBpZnkocHRyIG4pIHsKICAgICAgICBpZiAoIW4pIHJldHVybjsKICAgICAgICBwdHIgbXggPSBuOwogICAgICAgIGlmIChuLT5sICYmIG4tPmwtPnByaSA+IG14LT5wcmkpIG14ID0gbi0+bDsKICAgICAgICBpZiAobi0+ciAmJiBuLT5yLT5wcmkgPiBteC0+cHJpKSBteCA9IG4tPnI7CiAgICAgICAgaWYgKG14ICE9IG4pIHN3YXAobi0+cHJpLCBteC0+cHJpKSwgaGVhcGlmeShteCk7CiAgICB9CiAgICAKICAgIHB0ciBidWlsZCh2ZWN0b3I8cHRyPiYgbnMsIGludCBsID0gMCwgaW50IHIgPSAtNjkpIHsKICAgICAgICBpZiAociA9PSAtNjkpIHIgPSAoaW50KSBucy5zaXplKCkgLSAxOwogICAgICAgIGlmIChsID4gcikgcmV0dXJuIG51bGxwdHI7CiAgICAgICAgaWYgKGwgPT0gcikgcmV0dXJuIG5zW2xdOwogICAgICAgIGludCBtID0gKGwgKyByKSAvIDI7CiAgICAgICAgbnNbbV0tPmwgPSBidWlsZChucywgbCwgbSAtIDEpOwogICAgICAgIG5zW21dLT5yID0gYnVpbGQobnMsIG0gKyAxLCByKTsKICAgICAgICBoZWFwaWZ5KG5zW21dKTsKICAgICAgICByZXR1cm4gcHVsbChuc1ttXSk7CiAgICB9CiAgICAKICAgIHRlbXBsYXRlIDx0eXBlbmFtZSBPcD4KICAgIHZvaWQgdG91cihwdHIgbiwgT3Agb3ApIHsKICAgICAgICBzdGFjazxwdHI+IHN0azsKICAgICAgICB3aGlsZSAobiB8fCAhc3RrLmVtcHR5KCkpIHsKICAgICAgICAgICAgZm9yICg7IG47IG4gPSBuLT5sKSBzdGsucHVzaChuKTsKICAgICAgICAgICAgbiA9IHN0ay50b3AoKTsgc3RrLnBvcCgpOwogICAgICA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