Submission #971781

# Submission time Handle Problem Language Result Execution time Memory
971781 2024-04-29T09:26:29 Z NoLove Monkey and Apple-trees (IZhO12_apple) C++14
0 / 100
86 ms 65796 KB
/**
 *    author : Lăng Trọng Đạt
 *    created: 29-04-2024 
**/
#include <bits/stdc++.h>
using namespace std;
#ifndef LANG_DAT
#define db(...) ;
#endif // LANG_DAT
#define int long long
#define mp make_pair
#define f first
#define se second
#define pb push_back
#define all(v) (v).begin(), (v).end()
using pii = pair<int, int>;
using vi = vector<int>;
#define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
void mx(int& a, int b) { if (b > a) a = b; }
void mi(int& a, int b) { if (b < a) a = b; }
#define si(x) (int)(x.size())
const int INF = 1e18;
const int MOD = 1e9 + 7;

struct Data {
    int cnt = 0, l = -1, r;
    int red = false;
};
vector<Data> st(2);

int n, type, x, y;

void cre(Data& v) {
    if (v.l != -1) return;
    v.l = si(st);
    st.pb(Data());
    v.r = si(st);
    st.pb(Data());
}

void push(Data& v, int l, int r) {
    if (v.red) {
        v.cnt = v.red*(r - l + 1);
        if (l < r) {
            cre(v);
            st[v.l].red = v.red; 
            st[v.r].red = v.red;
        }
        v.red = false;
    }
}
void upd(int v = 1, int l = 1, int r = 1e9) {
    if (l > y or x > r) return;
    push(st[v], l, r);
    if (x <= l && r <= y) {
        // db(v, l, r)
        st[v].red = true;
    } else {
        int mid = (l + r) / 2;
        cre(st[v]);
        
        upd(st[v].l, l, mid);
        upd(st[v].r, mid + 1, r);
        
        push(st[st[v].l], l, mid);
        push(st[st[v].r], mid + 1, r);
        
        st[v].cnt = st[st[v].l].cnt + st[st[v].r].cnt;
        // if (st[v].cnt > 0)
        // db(v, l, r, st[v].cnt)
    }
}
int get(int v = 1, int l = 1, int r = 1e9) {
    if (l > y or x > r) return 0;
    push(st[v], l, r);
    // db(st[v].cnt, l, r)
    if (x <= l && r <= y) {
        return st[v].cnt;
    } else {
        int mid = (l + r) / 2;
        cre(st[v]);
        return get(st[v].l, l, mid) + 
                get(st[v].r, mid + 1, r);
    }
}
int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    if (fopen("hi.inp", "r")) {
        freopen("hi.inp", "r", stdin);
       freopen("hi.out", "w", stdout);
    } 

    cin >> n; 
    st.reserve(20*n);   
    int c = 0;
    FOR(i, 1, n) {
        cin >> type >> x >> y;
        // db(i, si(st), x, y)
        x += c; y += c;
        if (type == 2) upd();
        else {
            c = get();
            cout << c << "\n";
        }
    }
}

Compilation message

apple.cpp: In function 'int32_t main()':
apple.cpp:18:31: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
   18 | #define FOR(i, a, b) for (int (i) = a; (i) <= (b); i++)
      |                               ^
apple.cpp:96:5: note: in expansion of macro 'FOR'
   96 |     FOR(i, 1, n) {
      |     ^~~
apple.cpp:89:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
apple.cpp:90:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   90 |        freopen("hi.out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 600 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 10 ms 5468 KB Output is correct
5 Correct 11 ms 6748 KB Output is correct
6 Correct 11 ms 6332 KB Output is correct
7 Correct 11 ms 6744 KB Output is correct
8 Runtime error 86 ms 65796 KB Execution killed with signal 11
9 Halted 0 ms 0 KB -