답안 #1117894

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1117894 2024-11-24T09:22:27 Z gdragon 원숭이와 사과 나무 (IZhO12_apple) C++17
0 / 100
1 ms 336 KB
#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e5 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
struct Node {
    int lz = 0;
    long long val = 0;
    Node* left = NULL, *right = NULL;
} *root;
int q;
void read() {
    cin >> q;
}
void update(Node* &head, int l, int r, int u, int v) {
    if (u > r || v < l) return;
    if (head == NULL) head = new Node();
    if (head->val == r - l + 1) return;
    //cerr << l << ' ' << r << ' ' << u << ' ' << v << endl;
    if (u <= l && r <= v) {
        head->lz = 1;
        head->val = r - l + 1;
        return;
    }
    // cerr << "RUN: " << l << ' ' << r << ' ' << u << ' ' << v << endl;
    int mid = (l + r) >> 1;
    if (v <= mid) update(head->left, l, mid, u, v);
    else if (u > mid) update(head->right, mid + 1, r, u, v);
    else {
        update(head->left, l, mid, u, v);
        update(head->right, mid + 1, r, u, v);
    }
    head->val = (head->left == NULL ? 0 : head->left->val) + 
                (head->right == NULL ? 0 : head->right->val);
    // cout << l << ' ' << r << ' ' << head->val << endl;
}
long long getSum(Node* head, int l, int r, int u, int v, int lz) {
    if (head == NULL) {
        if (lz) return r - l + 1;
        return 0;
    }
    if (u > r || v < l) return 0;
    // cerr << l << ' ' << r << ' ' << u << ' ' << v << endl;
    if (u <= l && r <= v) {
        // cerr << l << ' ' << r << ' ' << head->val << endl;
        return head->val;
    }
    int mid = (l + r) >> 1;
    if (v <= mid) return getSum(head->left, l, mid, u, v, head->lz | lz);
    else if (u > mid) return getSum(head->right, mid + 1, r, u, v, head->lz | lz);
    else {
        return getSum(head->left, l, mid, u, v, head->lz | lz) + getSum(head->right, mid + 1, r, u, v, head->lz | lz);
    }
}
void solve() {
    root = new Node();
    long long C = 0;
    while(q--) {
        long long type, l, r; cin >> type >> l >> r;
        l += C; r += C;
        if (type == 2) update(root, 1, (int)1e9, l, r);
        else {
            C = getSum(root, 1, (int)1e9, l, r, 0);
            cout << C << endl;
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    if (fopen(TASK".inp", "r")) {
        freopen(TASK".inp", "r", stdin);
        freopen(TASK".out", "w", stdout);
    }
    int test = 1;
    // cin >> test;
    while(test--) {
        read();
        solve();
    }
    return 0;
}

Compilation message

apple.cpp: In function 'int main()':
apple.cpp:85:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   85 |         freopen(TASK".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:86:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   86 |         freopen(TASK".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 336 KB Output is correct
2 Incorrect 1 ms 336 KB Output isn't correct
3 Halted 0 ms 0 KB -