Submission #335316

# Submission time Handle Problem Language Result Execution time Memory
335316 2020-12-11T23:25:47 Z jovan_b Monkey and Apple-trees (IZhO12_apple) C++17
100 / 100
50 ms 3052 KB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;

#define pb push_back

int intersect(int l, int r, int tl, int tr){
    return max(0, min(tr, r)-max(l, tl)+1);
}

struct segment{
    int val;
    bool lazy;
    segment* c[2];
    segment() {c[0] = c[1] = NULL;}
    void upd(int l, int r, int tl, int tr){
        if(val == r-l+1) return;
        if(tl <= l && r <= tr){
            val = r-l+1;
            return;
        }
        int mid = (ll(l)+r)/2;
        if(intersect(l, mid, tl, tr)){
            if(!c[0]) c[0] = new segment();
            c[0]->upd(l, mid, tl, tr);
        }
        if(intersect(mid+1, r, tl, tr)){
            if(!c[1]) c[1] = new segment();
            c[1]->upd(mid+1, r, tl, tr);
        }
        val = 0;
        if(c[0]) val += c[0]->val;
        if(c[1]) val += c[1]->val;
    }
    int query(int l, int r, int tl, int tr){
        if(val == r-l+1) return intersect(l, r, tl, tr);
        if(tl <= l && r <= tr) return val;
        int mid = (ll(l)+r)/2;
        int res = 0;
        if(intersect(l, mid, tl, tr) && c[0]) res += c[0]->query(l, mid, tl, tr);
        if(intersect(mid+1, r, tl, tr) && c[1]) res += c[1]->query(mid+1, r, tl, tr);
        return res;
    }
};

int main(){
    ios_base::sync_with_stdio(false), cin.tie(0);
    cout.precision(10);
    cout << fixed;

    segment *ROOT = new segment();
    int m;
    cin >> m;
    const int n = 1000000000;
    int res = 0;
    for(int i=1; i<=m; i++){
        int t, l, r;
        cin >> t >> l >> r;
        l += res;
        r += res;
        if(t == 1){
            res = ROOT->query(1, n, l, r);
            cout << res << "\n";
        }
        else{
            ROOT->upd(1, n, l, r);
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 364 KB Output is correct
2 Correct 1 ms 364 KB Output is correct
3 Correct 1 ms 364 KB Output is correct
4 Correct 5 ms 492 KB Output is correct
5 Correct 5 ms 492 KB Output is correct
6 Correct 6 ms 492 KB Output is correct
7 Correct 5 ms 492 KB Output is correct
8 Correct 25 ms 1516 KB Output is correct
9 Correct 45 ms 2540 KB Output is correct
10 Correct 45 ms 2540 KB Output is correct
11 Correct 42 ms 2540 KB Output is correct
12 Correct 42 ms 2540 KB Output is correct
13 Correct 50 ms 2924 KB Output is correct
14 Correct 50 ms 2924 KB Output is correct
15 Correct 40 ms 3052 KB Output is correct
16 Correct 44 ms 3052 KB Output is correct
17 Correct 37 ms 2924 KB Output is correct
18 Correct 37 ms 2924 KB Output is correct
19 Correct 41 ms 3052 KB Output is correct
20 Correct 41 ms 3052 KB Output is correct