# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
703855 | mihai145 | Monkey and Apple-trees (IZhO12_apple) | C++14 | 44 ms | 21580 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//
// Created by mihai145 on 28.02.2023.
// Test problem: https://oj.uz/problem/view/IZhO12_apple
//
#include <iostream>
using namespace std;
const int NMAX = 2 * 100000;
class DynamicSegmentTree {
private:
int kNodes, v[4 * NMAX];
pair<int, int> sons[4 * NMAX];
bool lazy[4 * NMAX];
void init(int idx) {
sons[idx].first = ++kNodes;
sons[idx].second = ++kNodes;
}
void push(int idx, int l, int r) {
if (lazy[idx]) {
v[idx] = r - l + 1;
lazy[sons[idx].first] = true;
lazy[sons[idx].second] = true;
}
}
public:
void Init() {
for (int i = 1; i < 4 * NMAX; i++) {
v[i] = 0;
sons[i].first = -1, sons[i].second = -1;
lazy[i] = false;
}
kNodes = 1;
}
void Update(int idx, int l, int r, int ql, int qr) {
if (sons[idx].first == -1) {
init(idx);
}
push(idx, l, r);
if (r < ql || qr < l) return;
if (ql <= l && r <= qr) {
lazy[idx] = true;
push(idx, l, r);
return;
}
int mid = (l + r) >> 1;
Update(sons[idx].first, l, mid, ql, qr);
Update(sons[idx].second, mid + 1, r, ql, qr);
v[idx] = v[sons[idx].first] + v[sons[idx].second];
}
int Query(int idx, int l, int r, int ql, int qr) {
if (sons[idx].first == -1) {
init(idx);
}
push(idx, l, r);
if (r < ql || qr < l) return 0;
if (ql <= l && r <= qr) return v[idx];
int mid = (l + r) >> 1;
int a1 = Query(sons[idx].first, l, mid, ql, qr), a2 = Query(sons[idx].second, mid + 1, r, ql, qr);
return a1 + a2;
}
};
DynamicSegmentTree dst;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
dst.Init();
int m; cin >> m;
int d, x, y, c = 0;
for (int i = 1; i <= m; i++) {
cin >> d >> x >> y;
x += c, y += c;
if (d == 1) {
c = dst.Query(1, 1, 1e9, x, y);
cout << c << '\n';
} else {
dst.Update(1, 1, 1e9, x, y);
}
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |