// generated at caterpillow.github.io/byot
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
// You can implement your own monoid here for custom operations.
struct Value {
ll sum;
Value operator+(const Value& oth) const {
Value res {};
res.sum = sum + oth.sum;
return res;
}
};
const Value vid = {0};
mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
using ptr = struct Node*;
struct Node {
Value val;
Value agg;
int sz;
int pri;
ptr l, r;
ptr par;
Node() {
pri = mt();
val = vid;
agg = vid;
sz = 1;
l = r = nullptr;
par = nullptr;
}
Node(Value val) : val(val), agg(val) {
pri = mt();
sz = 1;
l = r = nullptr;
par = nullptr;
}
~Node() {
delete l;
delete r;
}
};
int sz(ptr n) { return n ? n->sz : 0; };
Value agg(ptr n) { return n ? n->agg : vid; }
ptr pull(ptr n) {
if (!n) return nullptr;
if (n->l) n->l->par = n;
if (n->r) n->r->par = n;
n->sz = sz(n->l) + 1 + sz(n->r);
n->agg = agg(n->l) + n->val + agg(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);
}
// [-inf, i) and [i, inf]
pair<ptr, ptr> spliti(ptr n, int i) {
if (!n) return {nullptr, nullptr};
n->par = 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};
}
}
ptr root(ptr n) {
while (n->par) n = n->par;
return n;
}
/*
- include merge
- include split by index (and size)
- include range sum (and range aggregates, and values)
- include root (and parent pointers)
*/
main() {
cin.tie(0)->sync_with_stdio(0);
int q; cin >> q;
map<int, ptr> nodes;
for (int i = 1; i <= q; i++) {
int t; cin >> t;
if (t == 1) {
int y; cin >> y;
nodes[i] = new Node({y});
}
if (t == 2) {
int y, z; cin >> y >> z;
ptr r1 = root(nodes[y]), r2 = root(nodes[z]);
if (r1 == r2) continue;
merge(r1, r2);
}
if (t == 3) {
int y, z; cin >> y >> z;
spliti(root(nodes[y]), z);
}
if (t == 4) {
int y; cin >> y;
cout << root(nodes[y])->agg.sum << '\n';
}
}
}
Ly8gZ2VuZXJhdGVkIGF0IGNhdGVycGlsbG93LmdpdGh1Yi5pby9ieW90CgojaW5jbHVkZSA8Yml0cy9zdGRjKysuaD4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CnVzaW5nIGxsID0gbG9uZyBsb25nOwoKLy8gWW91IGNhbiBpbXBsZW1lbnQgeW91ciBvd24gbW9ub2lkIGhlcmUgZm9yIGN1c3RvbSBvcGVyYXRpb25zLgpzdHJ1Y3QgVmFsdWUgewogICAgbGwgc3VtOwoKICAgIFZhbHVlIG9wZXJhdG9yKyhjb25zdCBWYWx1ZSYgb3RoKSBjb25zdCB7CiAgICAgICAgVmFsdWUgcmVzIHt9OwogICAgICAgIHJlcy5zdW0gPSBzdW0gKyBvdGguc3VtOwogICAgICAgIHJldHVybiByZXM7CiAgICB9Cn07Cgpjb25zdCBWYWx1ZSB2aWQgPSB7MH07CgptdDE5OTM3IG10KGNocm9ubzo6c3RlYWR5X2Nsb2NrOjpub3coKS50aW1lX3NpbmNlX2Vwb2NoKCkuY291bnQoKSk7CnVzaW5nIHB0ciA9IHN0cnVjdCBOb2RlKjsKCnN0cnVjdCBOb2RlIHsKICAgIFZhbHVlIHZhbDsKICAgIFZhbHVlIGFnZzsKCiAgICBpbnQgc3o7CiAgICBpbnQgcHJpOwogICAgcHRyIGwsIHI7CiAgICBwdHIgcGFyOwoKICAgIE5vZGUoKSB7CiAgICAgICAgcHJpID0gbXQoKTsKICAgICAgICB2YWwgPSB2aWQ7CiAgICAgICAgYWdnID0gdmlkOwogICAgICAgIHN6ID0gMTsKICAgICAgICBsID0gciA9IG51bGxwdHI7CiAgICAgICAgcGFyID0gbnVsbHB0cjsKICAgIH0KCiAgICBOb2RlKFZhbHVlIHZhbCkgOiB2YWwodmFsKSwgYWdnKHZhbC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R1cm4ge2wsIHB1bGwobil9OwogICAgfSBlbHNlIHsKICAgICAgICBhdXRvIFtsLCByXSA9IHNwbGl0aShuLT5yLCBpIC0gMSAtIHN6KG4tPmwpKTsKICAgICAgICBuLT5yID0gbDsKICAgICAgICByZXR1cm4ge3B1bGwobiksIHJ9OwogICAgfQp9CgpwdHIgcm9vdChwdHIgbikgewogICAgd2hpbGUgKG4tPnBhcikgbiA9IG4tPnBhcjsKICAgIHJldHVybiBuOwp9CgovKgoKLSBpbmNsdWRlIG1lcmdlCi0gaW5jbHVkZSBzcGxpdCBieSBpbmRleCAoYW5kIHNpemUpCi0gaW5jbHVkZSByYW5nZSBzdW0gKGFuZCByYW5nZSBhZ2dyZWdhdGVzLCBhbmQgdmFsdWVzKQotIGluY2x1ZGUgcm9vdCAoYW5kIHBhcmVudCBwb2ludGVycykKCiovCgptYWluKCkgewogICAgY2luLnRpZSgwKS0+c3luY193aXRoX3N0ZGlvKDApOwogICAgCiAgICBpbnQgcTsgY2luID4+IHE7CiAgICBtYXA8aW50LCBwdHI+IG5vZGVzOwogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gcTsgaSsrKSB7CiAgICAgICAgaW50IHQ7IGNpbiA+PiB0OwogICAgICAgIGlmICh0ID09IDEpIHsKICAgICAgICAgICAgaW50IHk7IGNpbiA+PiB5OwogICAgICAgICAgICBub2Rlc1tpXSA9IG5ldyBOb2RlKHt5fSk7CiAgICAgICAgfQogICAgICAgIGlmICh0ID09IDIpIHsKICAgICAgICAgICAgaW50IHksIHo7IGNpbiA+PiB5ID4+IHo7CiAgICAgICAgICAgIHB0ciByMSA9IHJvb3Qobm9kZXNbeV0pLCByMiA9IHJvb3Qobm9kZXNbel0pOwogICAgICAgICAgICBpZiAocjEgPT0gcjIpIGNvbnRpbnVlOwogICAgICAgICAgICBtZXJnZShyMSwgcjIpOwogICAgICAgIH0KICAgICAgICBpZiAodCA9PSAzKSB7CiAgICAgICAgICAgIGludCB5LCB6OyBjaW4gPj4geSA+PiB6OwogICAgICAgICAgICBzcGxpdGkocm9vdChub2Rlc1t5XSksIHopOwogICAgICAgIH0KICAgICAgICBpZiAodCA9PSA0KSB7CiAgICAgICAgICAgIGludCB5OyBjaW4gPj4geTsKICAgICAgICAgICAgY291dCA8PCByb290KG5vZGVzW3ldKS0+YWdnLnN1bSA8PCAnXG4nOwogICAgICAgIH0KICAgIH0KfQ==