#include <bits/stdc++.h>
using namespace std;
namespace std {
#ifndef LOCAL
#define cerr \
if (0) cerr
#endif
#define sz(x) ((int)(x.size()))
} // namespace std
const int Q = 500'005;
const int N = 3 * Q;
int64_t get_sum(int l, int r) {
return 1ll * (l + r) * (r - l + 1) / 2;
}
struct Node {
int cnt;
int64_t sum;
Node(int cnt = 0, int64_t sum = 0) : cnt(cnt), sum(sum) {}
Node operator+(const Node& rhs) const {
return Node(cnt + rhs.cnt, sum + rhs.sum);
}
Node& operator+=(const Node& rhs) {
cnt += rhs.cnt;
sum += rhs.sum;
return *this;
}
Node operator-(const Node& rhs) const {
return Node(cnt - rhs.cnt, sum - rhs.sum);
}
};
int q;
array<int, 3> qr[Q];
int prv[N], nxt[N];
pair<int, int> segs_at[N];
pair<int, int> new_segs[N];
int index_seg[N];
Node ft[N];
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
#ifdef LOCAL
#define task "a"
#else
#define task "TSEQ"
#endif
if (fopen(task ".inp", "r")) {
freopen(task ".inp", "r", stdin);
freopen(task ".out", "w", stdout);
}
cin >> q;
set<array<int, 3>> segs;
segs.insert({0, 0, 1});
segs_at[1] = {0, 0};
int low = 1;
int idx = 1;
for (int i = 1; i <= q; i++) {
for (int j = 0; j < 3; j++) {
cin >> qr[i][j];
}
auto rematch = [&](int pos, vector<pair<int, int>> x) {
vector<pair<int, int>> nx;
for (auto [l, r] : x) {
if (l <= r) {
nx.emplace_back(l, r);
}
}
nx.swap(x);
int curr = idx + 1;
prv[curr] = prv[pos];
nxt[prv[pos]] = curr;
for (int j = 1; j < sz(x); j++) {
prv[curr + j] = curr + j - 1;
nxt[curr + j - 1] = curr + j;
}
if (nxt[pos]) {
prv[nxt[pos]] = curr + sz(x) - 1;
nxt[curr + sz(x) - 1] = nxt[pos];
}
for (int j = 0; j < sz(x); j++) {
segs.insert({x[j].first, x[j].second, curr + j});
segs_at[curr + j] = {x[j].first, x[j].second};
}
idx += sz(x);
};
if (qr[i][0] == 1) {
auto [_, x, k] = qr[i];
int high = low + k - 1;
auto it = segs.upper_bound(array<int, 3>{x, (int)1e9, (int)1e9});
assert(it != segs.begin());
it--;
auto [l, r, pos] = *it;
assert(r >= x);
// [l, x] -> [low, high] -> [x + 1, r]
// cerr << "1split " << l << " " << r << "into ["
// << l << " " << x << "] ["
// << low << " " << high << "] ["
// << x + 1 << " " << r << "]\n";
rematch(pos, {{l, x}, {low, high}, {x + 1, r}});
segs.erase({l, r, pos});
low += k;
}
if (qr[i][0] == 2) {
auto [_, y, h] = qr[i];
auto it = segs.upper_bound(array<int, 3>{y, (int)1e9});
assert(it != segs.begin());
it--;
auto [l, r, pos] = *it;
assert(r >= y);
rematch(pos, {{l, y}, {y + 1, r}});
segs.erase({l, r, pos});
}
}
int num_segs = 0;
for (int i = 0; (i = nxt[i]);) {
new_segs[++num_segs] = segs_at[i];
index_seg[num_segs] = num_segs;
}
for (int i = 1; i <= num_segs; i++) {
segs_at[i] = new_segs[i];
}
sort(index_seg + 1, index_seg + num_segs + 1, [&](int i, int j) { return segs_at[i].second < segs_at[j].second; });
auto update = [&](int i, int delta) {
auto [l, r] = segs_at[i];
if (l > r) return;
Node curr = Node((r - l + 1) * delta, get_sum(l, r) * delta);
while (i <= num_segs) {
ft[i] += curr;
i += i & -i;
}
};
auto get = [&](int i) -> Node {
Node curr(0, 0);
while (i > 0) {
curr += ft[i];
i -= i & -i;
}
return curr;
};
auto lowerbound = [&](int v) -> int {
int cnt = 0;
int pos = 0;
for (int i = 20; i >= 0; i--) {
if (pos + (1 << i) <= num_segs && cnt + ft[pos + (1 << i)].cnt < v) {
pos += 1 << i;
cnt += ft[pos].cnt;
}
}
return pos + 1;
};
auto get_prefix = [&](int l) -> int64_t {
if (l == 0) {
return 0LL;
}
int pos = lowerbound(l);
auto [curr_cnt, curr_sum] = get(pos);
auto [lo, hi] = segs_at[pos];
curr_cnt -= l;
if (curr_cnt) {
curr_sum -= get_sum(hi - curr_cnt + 1, hi);
}
return curr_sum;
};
low = 1;
int j = 1;
for (int i = 1; i <= q; i++) {
while (j <= num_segs && segs_at[index_seg[j]].second < low) {
update(index_seg[j], 1);
j++;
}
if (qr[i][0] == 1) {
auto [_, x, k] = qr[i];
low += k;
}
if (qr[i][0] == 2) {
auto [_, y, h] = qr[i];
int lo = 1, hi = num_segs;
int ans = -1;
while (lo <= hi) {
int mid = (lo + hi) >> 1;
if (segs_at[index_seg[mid]].second <= y) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
// cerr<<"que "<<_<<" "<<y<<" "<<h<<": " <<
assert(ans != -1 && ans < num_segs);
ans = index_seg[ans];
int trc = get(ans).cnt;
int can = trc + h;
int pos = lowerbound(can);
int dem = 0;
for (int j = ans + 1; j < pos; j++) {
int curr = get(j).cnt - get(j - 1).cnt;
if (!curr) continue;
h -= curr;
update(j, -1);
segs_at[j].first = 1e9;
}
auto& [l, r] = segs_at[pos];
assert(get(pos).cnt - get(pos - 1).cnt > 0);
update(pos, -1);
l += h;
update(pos, 1);
}
if (qr[i][0] == 3) {
auto [_, l, r] = qr[i];
cout << get_prefix(r) - get_prefix(l - 1) << "\n";
}
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKbmFtZXNwYWNlIHN0ZCB7CiNpZm5kZWYgTE9DQUwKI2RlZmluZSBjZXJyIFwKICBpZiAoMCkgY2VycgojZW5kaWYKI2RlZmluZSBzeih4KSAoKGludCkoeC5zaXplKCkpKQp9ICAvLyBuYW1lc3BhY2Ugc3RkCgpjb25zdCBpbnQgUSA9IDUwMCcwMDU7CmNvbnN0IGludCBOID0gMyAqIFE7CgppbnQ2NF90IGdldF9zdW0oaW50IGwsIGludCByKSB7CiAgcmV0dXJuIDFsbCAqIChsICsgcikgKiAociAtIGwgKyAxKSAvIDI7Cn0KCnN0cnVjdCBOb2RlIHsKICBpbnQgY250OwogIGludDY0X3Qgc3VtOwogIE5vZGUoaW50IGNudCA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GkgPSAxOyBpIDw9IHE7IGkrKykgewogICAgZm9yIChpbnQgaiA9IDA7IGogPCAzOyBqKyspIHsKICAgICAgY2luID4+IHFyW2ldW2pdOwogICAgfQogICAgYXV0byByZW1hdGNoID0gWyZdKGludCBwb3MsIHZlY3RvcjxwYWlyPGludCwgaW50Pj4geCkgewogICAgICB2ZWN0b3I8cGFpcjxpbnQsIGludD4+IG54OwogICAgICBmb3IgKGF1dG8gW2wsIHJdIDogeCkgewogICAgICAgIGlmIChsIDw9IHIpIHsKICAgICAgICAgIG54LmVtcGxhY2VfYmFjayhsLCByKTsKICAgICAgICB9CiAgICAgIH0KICAgICAgbnguc3dhcCh4KTsKICAgICAgaW50IGN1cnIgPSBpZHggKyAxOwogICAgICBwcnZbY3Vycl0gPSBwcnZbcG9zXTsKICAgICAgbnh0W3Bydltwb3NdXSA9IGN1cnI7CiAgICAgIGZvciAoaW50IGogPSAxOyBqIDwgc3ooeCk7IGorKykgewogICAgICAgIHBydltjdXJyICsgal0gPSBjdXJyICsgaiAtIDE7CiAgICAgICAgbnh0W2N1cnIgKyBqIC0gMV0gPSBjdXJyICsgajsKICAgICAgfQogICAgICBpZiAobnh0W3Bvc10pIHsKICAgICAgICBwcnZbbnh0W3Bvc11dID0gY3VyciArIHN6KHgpIC0gMTsKICAgICAgICBueHRbY3VyciArIHN6KHgpIC0gMV0gPSBueHRbcG9zXTsKICAgICAgfQogICAgICBmb3IgKGludCBqID0gMDsgaiA8IHN6KHgpOyBqKyspIHsKICAgICAgICBzZWdzLmluc2VydCh7eFtqXS5maXJzdCwgeFtqXS5zZWNvbmQsIGN1cnIgKyBqfSk7CiAgICAgICAgc2Vnc19hdFtjdXJyICsgal0gPSB7eFtqXS5maXJzdCwgeFtqXS5zZWNvbmR9OwogICAgICB9CiAgICAgIGlkeCArPSBzeih4KTsKICAgIH07CiAgICBpZiAocXJbaV1bMF0gPT0gMSkgewogICAgICBhdXRvIFtfLCB4LCBrXSA9IHFyW2ldOwogICAgICBpb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VncyA9IDA7CiAgZm9yIChpbnQgaSA9IDA7IChpID0gbnh0W2ldKTspIHsKICAgIG5ld19zZWdzWysrbnVtX3NlZ3NdID0gc2Vnc19hdFtpXTsKICAgIGluZGV4X3NlZ1tudW1fc2Vnc10gPSBudW1fc2VnczsKICB9CiAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbnVtX3NlZ3M7IGkrKykgewogICAgc2Vnc19hdFtpXSA9IG5ld19zZWdzW2ldOwogIH0KICBzb3J0KGluZGV4X3NlZyArIDEsIGluZGV4X3NlZyArIG51bV9zZWdzICsgMSwgWyZdKGludCBpLCBpbnQgaikgeyByZXR1cm4gc2Vnc19hdFtpXS5zZWNvbmQgPCBzZWdzX2F0W2pdLnNlY29uZDsgfSk7CiAgYXV0byB1cGRhdGUgPSBbJl0oaW50IGksIGludCBkZWx0YSkgewogICAgYXV0byBbbCwgcl0gPSBzZWdzX2F0W2ldOwogICAgaWYgKGwgPiByKSByZXR1cm47CiAgICBOb2RlIGN1cnIgPSBOb2RlKChyIC0gbCArIDEpICogZGVsdGEsIGdldF9zdW0obCwgcikgKiBkZWx0YSk7CiAgICB3aGlsZSAoaSA8PSBudW1fc2VncykgewogICAgICBmdFtpXSArPSBjdXJyOwogICAgICBpICs9IGkgJiAtaTsKICAgIH0KICB9OwogIGF1dG8gZ2V0ID0gWyZdKGludCBpKSAtPiBOb2RlIHsKICAgIE5vZGUgY3VycigwLCAwKTsKICAgIHdoaWxlIChpID4gMCkgewogICAgICBjdXJyICs9IGZ0W2ldOwogICAgICBpIC09IGkgJiAtaTsKICAgIH0KICAgIHJldHVybiBjdXJyOwogIH07CiAgYXV0byBsb3dlcmJvdW5kID0gWyZdKGludCB2KSAtPiBpbnQgewogICAgaW50IGNudCA9IDA7CiAgICBpbnQgcG9zID0gMDsKICAgIGZvciAoaW50IGkgPSAyMDsgaSA+PSAwOyBpLS0pIHsKICAgICAgaWYgKHBvcyArICgxIDw8IGkpIDw9IG51bV9zZWdzICYmIGNudCArIG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PiAxOwogICAgICAgIGlmIChzZWdzX2F0W2luZGV4X3NlZ1ttaWRdXS5zZWNvbmQgPD0geSkgewogICAgICAgICAgYW5zID0gbWlkOwogICAgICAgICAgbG8gPSBtaWQgKyAxOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICBoaSA9IG1pZCAtIDE7CiAgICAgICAgfQogICAgICB9CiAgICAgIC8vIGNlcnI8PCJxdWUgIjw8Xzw8IiAiPDx5PDwiICI8PGg8PCI6ICIgPDwKICAgICAgYXNzZXJ0KGFucyAhPSAtMSAmJiBhbnMgPCBudW1fc2Vncyk7CiAgICAgIGFucyA9IGluZGV4X3NlZ1thbnNdOwogICAgICBpbnQgdHJjID0gZ2V0KGFucykuY250OwogICAgICBpbnQgY2FuID0gdHJjICsgaDsKICAgICAgaW50IHBvcyA9IGxvd2VyYm91bmQoY2FuKTsKICAgICAgaW50IGRlbSA9IDA7CiAgICAgIGZvciAoaW50IGogPSBhbnMgKyAxOyBqIDwgcG9zOyBqKyspIHsKICAgICAgICBpbnQgY3VyciA9IGdldChqKS5jbnQgLSBnZXQoaiAtIDEpLmNudDsKICAgICAgICBpZiAoIWN1cnIpIGNvbnRpbnVlOwogICAgICAgIGggLT0gY3VycjsKICAgICAgICB1cGRhdGUoaiwgLTEpOwogICAgICAgIHNlZ3NfYXRbal0uZmlyc3QgPSAxZTk7CiAgICAgIH0KICAgICAgYXV0byYgW2wsIHJdID0gc2Vnc19hdFtwb3NdOwogICAgICBhc3NlcnQoZ2V0KHBvcykuY250IC0gZ2V0KHBvcyAtIDEpLmNudCA+IDApOwogICAgICB1cGRhdGUocG9zLCAtMSk7CiAgICAgIGwgKz0gaDsKICAgICAgdXBkYXRlKHBvcywgMSk7CiAgICB9CiAgICBpZiAocXJbaV1bMF0gPT0gMykgewogICAgICBhdXRvIFtfLCBsLCByXSA9IHFyW2ldOwogICAgICBjb3V0IDw8IGdldF9wcmVmaXgocikgLSBnZXRfcHJlZml4KGwgLSAxKSA8PCAiXG4iOwogICAgfQogIH0KICByZXR1cm4gMDsKfQo=