답안 #971787

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
971787 2024-04-29T09:38:27 Z NoLove 원숭이와 사과 나무 (IZhO12_apple) C++14
100 / 100
288 ms 159700 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;
} st[(int)1e7];
// vector<Data> st(2);

int n, type, x, y;

int id = 2;
void cre(Data& v) {
    if (v.l != -1) return;
    v.l = id++;
    v.r = id++;
}

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:95:5: note: in expansion of macro 'FOR'
   95 |     FOR(i, 1, n) {
      |     ^~~
apple.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   88 |         freopen("hi.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
apple.cpp:89:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |        freopen("hi.out", "w", stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 68 ms 156876 KB Output is correct
2 Correct 42 ms 156756 KB Output is correct
3 Correct 42 ms 156868 KB Output is correct
4 Correct 49 ms 157012 KB Output is correct
5 Correct 49 ms 157020 KB Output is correct
6 Correct 49 ms 157048 KB Output is correct
7 Correct 51 ms 157008 KB Output is correct
8 Correct 116 ms 158032 KB Output is correct
9 Correct 176 ms 159060 KB Output is correct
10 Correct 190 ms 159060 KB Output is correct
11 Correct 192 ms 158976 KB Output is correct
12 Correct 180 ms 159080 KB Output is correct
13 Correct 195 ms 159484 KB Output is correct
14 Correct 167 ms 159316 KB Output is correct
15 Correct 232 ms 159700 KB Output is correct
16 Correct 231 ms 159656 KB Output is correct
17 Correct 166 ms 159560 KB Output is correct
18 Correct 182 ms 159372 KB Output is correct
19 Correct 288 ms 159572 KB Output is correct
20 Correct 225 ms 159576 KB Output is correct