답안 #869356

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
869356 2023-11-04T07:33:36 Z natus26 원숭이와 사과 나무 (IZhO12_apple) C++17
100 / 100
484 ms 262144 KB
#include <bits/stdc++.h>
#define task "1"
#define ll long long
#define double long double
#define ii pair<int, int>
#define li pair<ll, int>
#define fi first
#define se second
#define c_bit(i)       __builtin_popcountll(i)
#define Bit(mask, i)    ((mask >> i) & 1)
#define onbit(mask, i)  ((mask) bitor (1LL << i))
#define offbit(mask, i) ((mask) &~ (1LL << i))
using namespace std;
const int maxn = 1e5 + 5;
const ll oo = 1e18;
const int mod = 1e9 + 7;
const int dx[] = {0, 1, 0, -1} , dy[] = {1, 0, -1, 0};

template <class X, class Y> bool maximize(X &a, const Y &b)
{
    if(a < b) return a = b, true;
    return false;
}

template <class X, class Y> bool minimize(X &a, const Y &b)
{
    if(a > b) return a = b, true;
    return false;
}

struct Node{
    Node *l, *r;
    int sum, lazy;

    Node() {
        l = r = nullptr;
        sum = lazy = 0;
    }
};

Node *root = new Node();

void fall(Node *cur)
{
    if(cur -> l != nullptr) return;
    cur -> l = new Node();
    cur -> r = new Node();
}

void down(Node *cur, int l, int r)
{
    if(cur -> lazy) cur -> sum = r - l + 1;
    if(l != r && cur -> lazy){
        fall(cur);
        cur -> l -> lazy = cur -> r -> lazy = 1;
    }
    cur -> lazy = 0;
}

void update(int u, int v, Node *cur = root, int l = 1, int r = 1e9)
{
    down(cur, l, r);
    if(r < u || l > v) return;
    if(u <= l && r <= v){
        cur -> lazy = 1;
        down(cur, l, r);
        return;
    }
    fall(cur);
    int mid = l + r >> 1;
    update(u, v, cur -> l, l, mid);
    update(u, v, cur -> r, mid + 1, r);
    cur -> sum = cur -> l -> sum + cur -> r -> sum;
}

int get(int u, int v, Node *cur = root, int l = 1, int r = 1e9)
{
    down(cur, l, r);
    if(r < u || l > v) return 0;
    if(u <= l && r <= v) return cur -> sum;
    fall(cur);
    int mid = l + r >> 1;
    return get(u, v, cur -> l, l, mid) + get(u, v, cur -> r, mid + 1, r);
}

int main()
{
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    if(fopen(task".inp","r")){
       freopen(task".inp","r",stdin);
       freopen(task".out","w",stdout);
    }
    int q; cin >> q;
    int c = 0;
    while(q --){
        int type, l, r; cin >> type >> l >> r;
        if(type == 1){
            c = get(l + c, r + c);
            cout << c << '\n';
        }
        else update(l + c, r + c);
    }
    return 0;
}

Compilation message

apple.cpp: In function 'void update(int, int, Node*, int, int)':
apple.cpp:70:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   70 |     int mid = l + r >> 1;
      |               ~~^~~
apple.cpp: In function 'int get(int, int, Node*, int, int)':
apple.cpp:82:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |     int mid = l + r >> 1;
      |               ~~^~~
apple.cpp: In function 'int main()':
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(task".inp","r",stdin);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
apple.cpp:91:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |        freopen(task".out","w",stdout);
      |        ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 460 KB Output is correct
4 Correct 11 ms 5976 KB Output is correct
5 Correct 14 ms 7256 KB Output is correct
6 Correct 13 ms 7004 KB Output is correct
7 Correct 14 ms 7108 KB Output is correct
8 Correct 120 ms 52272 KB Output is correct
9 Correct 232 ms 88492 KB Output is correct
10 Correct 247 ms 99424 KB Output is correct
11 Correct 272 ms 108276 KB Output is correct
12 Correct 272 ms 112048 KB Output is correct
13 Correct 232 ms 139092 KB Output is correct
14 Correct 235 ms 140368 KB Output is correct
15 Correct 484 ms 254544 KB Output is correct
16 Correct 413 ms 256592 KB Output is correct
17 Correct 251 ms 145492 KB Output is correct
18 Correct 255 ms 145492 KB Output is correct
19 Correct 456 ms 262144 KB Output is correct
20 Correct 462 ms 262144 KB Output is correct