답안 #941515

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
941515 2024-03-09T04:32:26 Z ifateen 원숭이와 사과 나무 (IZhO12_apple) C++14
0 / 100
283 ms 97864 KB
#include <bits/stdc++.h>
using namespace std;

#define mid ((tl + tr) >> 1)

struct Node {
    int sum;
    bool lazy_set = false;
    int l = 0, r = 0;
} tree[3'000'005];

int ptr = 2;
int newchild() {
    return ptr++;
}

void propagate(int tl, int tr, int node) {
    if (tree[node].lazy_set) {
        tree[node].sum = tr - tl + 1;
        if (tl == tr) {
            tree[node].lazy_set = false;
            return;
        }
        if (!tree[node].l) tree[node].l = newchild();
        if (!tree[node].r) tree[node].r = newchild();
        tree[tree[node].l].lazy_set = true;
        tree[tree[node].r].lazy_set = true;
    }
    tree[node].lazy_set = false;
}

void update(int tl, int tr, int node, int l, int r) {
    propagate(tl, tr, node);
    if (tl > r || tr < l) return;
    if (tl >= l && tr <= r) {
        tree[node].lazy_set = true;
        propagate(tl, tr, node);
        return;
    }
    if (!tree[node].l) tree[node].l = newchild();
    if (!tree[node].r) tree[node].r = newchild();
    update(tl, mid, tree[node].l, l, r);
    update(mid + 1, tr, tree[node].r, l, r);
    tree[node].sum = tree[tree[node].l].sum + tree[tree[node].r].sum;
}

int query(int tl, int tr, int node, int l, int r) {
    propagate(tl, tr, node);
    if (tl > r || tr < l) return 0;
    if (tl >= l && tr <= r) {
        return tree[node].sum;
    }
    int ret = 0;
    if (tree[node].l) ret += query(tl, mid, tree[node].l, l, r);
    if (tree[node].r) ret += query(mid + 1, tr, tree[node].r, l, r);
    return ret;
}

int main() {
    for (auto& i : tree) i.l = 0, i.r = 0, i.sum = 0, i.lazy_set = false;
    int q;
    cin >> q;
    int c = 0;
    while (q--) {
        int t, l, r;
        cin >> t >> l >> r;
        l += c, r += c;
        if (t == 1) cout << (c = query(1, 1'000'000'000, 1, l, r)) << '\n';
        else update(1, 1'000'000'000, 1, l, r);
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 47188 KB Output is correct
2 Correct 14 ms 47196 KB Output is correct
3 Correct 14 ms 47396 KB Output is correct
4 Correct 29 ms 47452 KB Output is correct
5 Correct 33 ms 47396 KB Output is correct
6 Correct 32 ms 47452 KB Output is correct
7 Correct 33 ms 47448 KB Output is correct
8 Correct 126 ms 48212 KB Output is correct
9 Correct 283 ms 49588 KB Output is correct
10 Runtime error 278 ms 97864 KB Execution killed with signal 11
11 Halted 0 ms 0 KB -