제출 #703859

#제출 시각아이디문제언어결과실행 시간메모리
703859mihai145원숭이와 사과 나무 (IZhO12_apple)C++14
100 / 100
358 ms153720 KiB
// // Created by mihai145 on 28.02.2023. // Test problem: https://oj.uz/problem/view/IZhO12_apple // #include <iostream> using namespace std; const int LG = 30; const int NMAX = LG * 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 timeMemoryGrader output
Fetching results...