제출 #636064

#제출 시각아이디문제언어결과실행 시간메모리
636064tvladm2009Monkey and Apple-trees (IZhO12_apple)C++14
0 / 100
2102 ms56636 KiB
#include <iostream>

using namespace std;

const int LIM = 1e9;

struct Node {
    Node *lson;
    Node *rson;
    int sum;
    bool lazy;

    Node() {
        sum = 0;
        lazy = false;
        lson = NULL;
        rson = NULL;
    }
};

void push(Node *v, Node *&lson, Node *&rson, int l, int r) {
    if (l == r) {
        return;
    }
    if (v->lazy == true) {
        int mid = (l + r) / 2;
        if (lson == NULL) {
            lson = new Node();
        }
        if (rson == NULL) {
            rson = new Node();
        }
        lson->lazy = true;
        lson->sum = l - mid + 1;
        rson->lazy = true;
        rson->sum = r - mid;
        v->lazy = false;
    }
}

void update(Node *&root, int l, int r, int p, int q) {
    if (p > q) {
        return;
    }
    if (root == NULL) {
        root = new Node();
    }
    if (p <= l && r <= q) {
        root->lazy = true;
        root->sum = r - l + 1;
    } else {
        push(root, root->lson, root->rson, l, r);
        int mid = (l + r) / 2;
        update(root->lson, l, mid, p, min(mid, q));
        update(root->rson, mid + 1, r, max(p, mid + 1), q);
        int sumlson = 0, sumrson = 0;
        if (root->lson == NULL) {
            sumlson = 0;
        } else {
            sumlson = root->lson->sum;
        }
        if (root->rson == NULL) {
            sumrson = 0;
        } else {
            sumrson = root->rson->sum;
        }
        root->sum = sumlson + sumrson;
    }
}

int query(Node *&root, int l, int r, int p, int q) {
    if (p > q) {
        return 0;
    }
    if (root == NULL) {
        return 0;
    }
    if (q <= l && r <= q) {
        return root->sum;
    } else {
        push(root, root->lson, root->rson, l, r);
        int mid = (l + r) / 2;
        return query(root->lson, l, mid, p, min(mid, q)) + query(root->rson, mid + 1, r, max(p, mid + 1), q);
    }
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int q;
    cin >> q;
    int c = 0;
    Node *root = new Node();
    while (q--) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            c = query(root, 1, LIM, x + c, y + c);
            cout << c << "\n";
        } else {
            update(root, 1, LIM, x + c, y + c);
        }
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...