#include <bits/stdc++.h>
using namespace std;
class DynamicSegTree
{
struct DSegTreeItem
{
int val, lazy;
DSegTreeItem *l, *r;
};
public:
DynamicSegTree(int n)
{
this->nodes.reserve(1e7);
this->make_new();
this->size = n;
}
void rangeUpdate(int x, int y, DSegTreeItem *cur, int l, int r)
{
dynamics(cur);
if (cur->lazy)
propagateLazy(cur, l, r);
if (y <= l || x >= r)
return;
if (l >= x && r <= y)
{
cur->lazy = 1;
propagateLazy(cur, l, r);
return;
}
rangeUpdate(x, y, cur->l, l, midpoint(l, r));
rangeUpdate(x, y, cur->r, midpoint(l, r), r);
cur->val = cur->l->val + cur->r->val;
}
int query(int x, int y, DSegTreeItem *cur, int l, int r)
{
dynamics(cur);
if (cur->lazy)
propagateLazy(cur, l, r);
if (y <= l || x >= r)
return 0;
if (l >= x && r <= y)
return cur->val;
return query(x, y, cur->l, l, midpoint(l, r)) + query(x, y, cur->r, midpoint(l, r), r);
}
void rangeUpdate(int x, int y)
{
rangeUpdate(x, y, nodes[0], 0, size);
}
int query(int x, int y)
{
return query(x, y, nodes[0], 0, size);
}
private:
int size;
vector<DSegTreeItem *> nodes;
DSegTreeItem *make_new()
{
DSegTreeItem *nx = new DSegTreeItem({0, 0, nullptr, nullptr});
nodes.push_back(nx);
return nodes.back();
}
void dynamics(DSegTreeItem *cur)
{
if (cur->l == nullptr)
cur->l = make_new();
if (cur->r == nullptr)
cur->r = make_new();
}
void propagateLazy(DSegTreeItem *cur, int l, int r)
{
if (l != r - 1)
{
cur->l->lazy = 1;
cur->r->lazy = 1;
}
cur->val = r - l;
cur->lazy = 0;
}
};
void solveCase()
{
int m = 0;
cin >> m;
DynamicSegTree dst(1e9 + 1);
int c = 0;
while (m--)
{
int d = 0, x = 0, y = 0;
cin >> d >> x >> y;
if (d == 1)
c = dst.query(x + c, y + c + 1), cout << c << '\n';
else
dst.rangeUpdate(x + c, y + c + 1);
}
}
int32_t main()
{
mt19937 rng((unsigned int)chrono::steady_clock::now().time_since_epoch().count());
if (rng() % 100000 == 0)
assert(false);
ios::sync_with_stdio(false), cin.tie(NULL);
solveCase();
}
Compilation message
apple.cpp: In member function 'void DynamicSegTree::rangeUpdate(int, int, DynamicSegTree::DSegTreeItem*, int, int)':
apple.cpp:32:38: error: 'midpoint' was not declared in this scope
32 | rangeUpdate(x, y, cur->l, l, midpoint(l, r));
| ^~~~~~~~
apple.cpp: In member function 'int DynamicSegTree::query(int, int, DynamicSegTree::DSegTreeItem*, int, int)':
apple.cpp:45:39: error: 'midpoint' was not declared in this scope
45 | return query(x, y, cur->l, l, midpoint(l, r)) + query(x, y, cur->r, midpoint(l, r), r);
| ^~~~~~~~