Submission #1059202

# Submission time Handle Problem Language Result Execution time Memory
1059202 2024-08-14T18:38:53 Z manhlinh1501 Monkey and Apple-trees (IZhO12_apple) C++17
0 / 100
0 ms 348 KB
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 1e5 + 5;
const int oo32 = 1e9 + 5;

#define left ___left
#define right ___right

int Q, C = 0;

struct node {
    int sum = 0, lazy = 0;
    node *child[2] = {nullptr, nullptr};
};

void down(node *cur, int l, int r) {
    if(cur == nullptr or cur -> lazy == 0) return;
    if(l != r) {

        if(cur -> child[0] == nullptr) (cur -> child[0]) = new node();
        if(cur -> child[1] == nullptr) (cur -> child[1]) = new node();

        (cur -> child[0] -> lazy) += (cur -> lazy);
        (cur -> child[1] -> lazy) += (cur -> lazy);
    }
    (cur -> sum) = (cur -> lazy) * (r - l + 1);
    (cur -> lazy) = 0;
}

void add(node *cur, int l, int r, int u, int v) {
    down(cur, l, r);
    if(r < u or l > v) return;
    if(u <= l and r <= v) {
        (cur -> lazy) = 1;
        down(cur, l, r);
        return;
    }
    int m = (l + r) / 2;

    if((cur -> child[0]) == nullptr) (cur -> child[0]) = new node();
    if((cur -> child[1]) == nullptr) (cur -> child[1]) = new node();

    add(cur -> child[0], l, m, u, v);
    add(cur -> child[1], m + 1, r, u, v);

    (cur -> sum) = (cur -> child[0] -> sum) + (cur -> child[1] -> sum);
}

int get(node *cur, int l, int r, int u, int v) {
    down(cur, l, r);
    if(cur == nullptr or r < u or l > v) return 0;
    if(u <= l and r <= v) return (cur -> sum);
    int m = (l + r) / 2;
    int res = 0;
    if(cur -> child[0] != nullptr)
        res += get(cur -> child[0], l, m, u, v);
    if(cur -> child[1] != nullptr)
        res += get(cur -> child[1], m + 1, r, u, v);
    return res;
}

node *root = new node();

signed main() {
#define TASK "code"

    if (fopen(TASK ".inp", "r")) {
        freopen(TASK ".inp", "r", stdin);
        freopen(TASK ".out", "w", stdout);
    }

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    cin >> Q;

    while(Q--) {
        int type, l, r;
        cin >> type >> l >> r;
        l += C;
        r += C;
        if(type == 2) {
            add(root, 1, oo32, l, r);
//            cerr << l << " " << r << " " << get(root, 1, oo32, l, r) << "\n";
        } else {
            C = get(root, 1, oo32, l, r);
            cout << C << "\n";
        }
    }

    return (0 ^ 0);
}

Compilation message

apple.cpp: In function 'int main()':
apple.cpp:69:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   69 |         freopen(TASK ".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:70:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   70 |         freopen(TASK ".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Incorrect 0 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -