Submission #888541

# Submission time Handle Problem Language Result Execution time Memory
888541 2023-12-17T17:10:05 Z dwuy Monkey and Apple-trees (IZhO12_apple) C++14
100 / 100
224 ms 56800 KB
#include <bits/stdc++.h>
using namespace std;

struct Node{
    bool lz;
    int sum;
    Node *l, *r;

    Node(){
        this->lz = 0;
        this->sum = 0;
        this->l = this->r = NULL;
    }
};

struct SMT{
    int n;
    Node *root;

    SMT(int n=0){
        this->n = n;
        this->root = new Node();
    }

    void update(Node *cur, int l, int r, const int &u, const int &v){
        if(l>v || r<u) return;
        if(l>=u && r<=v){
            cur->lz = 1;
            cur->sum = (r-l+1);
            return;
        }
        int mid = (l+r)>>1;
        if(!cur->l && u<=mid) cur->l = new Node();
        if(!cur->r && mid<v) cur->r = new Node();
        update(cur->l, l, mid, u, v);
        update(cur->r, mid+1, r, u, v);
        cur->sum = (cur->l? cur->l->sum : 0) + (cur->r? cur->r->sum : 0);
        if(cur->lz) cur->sum = r - l + 1;
    }

    void update(int l, int r){
        update(root, 1, n, l, r);
    }

    int get(Node *cur, int l, int r, const int &u, const int &v){
        if(l>v || r<u || !cur || !cur->sum) return 0;
        if(l>=u && r<=v) return cur->sum;
        int mid = (l+r)>>1;
        int L = get(cur->l, l, mid, u, v);
        int R = get(cur->r, mid+1, r, u, v);
        return cur->lz? min(r, v) - max(l, u) + 1 : L + R;
    }

    int get(int l, int r){
        return get(root, 1, n, l, r);
    }
};

void solve(){
    int q;
    cin >> q;
    SMT smt(1000000000);
    int C = 0;
    while(q--){
        int oper, l, r;
        cin >> oper >> l >> r;
        l += C; r += C;
        if(oper == 2) smt.update(l, r);
        else{
            C = smt.get(l, r);
            cout << C << endl;
        }
    }
}

int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);

    solve();

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 0 ms 344 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 11 ms 1628 KB Output is correct
5 Correct 14 ms 2060 KB Output is correct
6 Correct 14 ms 1884 KB Output is correct
7 Correct 17 ms 1884 KB Output is correct
8 Correct 92 ms 13432 KB Output is correct
9 Correct 179 ms 23892 KB Output is correct
10 Correct 177 ms 26192 KB Output is correct
11 Correct 194 ms 27984 KB Output is correct
12 Correct 178 ms 28396 KB Output is correct
13 Correct 168 ms 29780 KB Output is correct
14 Correct 168 ms 30548 KB Output is correct
15 Correct 223 ms 55632 KB Output is correct
16 Correct 218 ms 55636 KB Output is correct
17 Correct 172 ms 31568 KB Output is correct
18 Correct 165 ms 31312 KB Output is correct
19 Correct 220 ms 56660 KB Output is correct
20 Correct 224 ms 56800 KB Output is correct