#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int q;
template <typename T = int>
struct DynamicST
{
struct TNode;
using PNode = TNode *;
struct TNode
{
T val, lazy;
PNode l, r;
TNode() : l(NULL), r(NULL), val(0), lazy(0) {}
};
PNode root;
DynamicST()
{
root = new TNode();
}
void downtree(PNode v, int tl, int tr)
{
if (v->lazy == 0)
return;
if (v->l == NULL)
v->l = new TNode();
if (v->r == NULL)
v->r = new TNode();
int tm = (tl + tr) >> 1;
v->l->lazy = v->lazy;
v->l->val = v->lazy * (tm - tl + 1);
v->r->lazy = v->lazy;
v->r->val = v->lazy * (tr - tm);
}
T query(PNode v, int tl, int tr, int l, int r)
{
if (l > r)
return 0;
if (l == tr && r == tr)
return v->val;
if (v->l == NULL)
v->l = new TNode();
if (v->r == NULL)
v->r = new TNode();
downtree(v, tl, tr);
int tm = (tl + tr) >> 1;
return query(v->l, tl, tm, l, min(r, tm)) + query(v->r, tm + 1, tr, max(l, tm + 1), r);
}
void update(PNode v, int tl, int tr, int l, int r, int add)
{
if (l > r)
return;
if (l == tl && r == tr)
{
// Assign query
v->val = add * (r - l + 1);
v->lazy = add;
return;
}
if (v->l == NULL)
v->l = new TNode();
if (v->r == NULL)
v->r = new TNode();
downtree(v, tl, tr);
int tm = (tl + tr) >> 1;
update(v->l, tl, tm, l, min(r, tm), add);
update(v->r, tm + 1, tr, max(l, tm + 1), r, add);
v->val = v->l->val + v->r->val;
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
// freopen("task.inp", "r", stdin);
// freopen("task.out", "w", stdout);
cin >> q;
int pre = 0;
DynamicST<> f;
while (q--)
{
int type, u, v;
cin >> type >> u >> v;
u += pre, v += pre;
if (type == 1)
{
pre = f.query(f.root, 1, 1e9, u, v);
cout << pre << '\n';
}
else
f.update(f.root, 1, 1e9, u, v, 1);
}
}
Compilation message
apple.cpp: In instantiation of 'DynamicST<T>::TNode::TNode() [with T = int]':
apple.cpp:24:16: required from 'DynamicST<T>::DynamicST() [with T = int]'
apple.cpp:89:17: required from here
apple.cpp:16:18: warning: 'DynamicST<>::TNode::r' will be initialized after [-Wreorder]
16 | PNode l, r;
| ^
apple.cpp:15:11: warning: 'int DynamicST<>::TNode::val' [-Wreorder]
15 | T val, lazy;
| ^~~
apple.cpp:17:9: warning: when initialized here [-Wreorder]
17 | TNode() : l(NULL), r(NULL), val(0), lazy(0) {}
| ^~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |