Submission #387978

# Submission time Handle Problem Language Result Execution time Memory
387978 2021-04-09T15:41:12 Z Aryan_Raina Monkey and Apple-trees (IZhO12_apple) C++14
100 / 100
579 ms 205660 KB
#include <bits/stdc++.h>
using namespace std;
 
#define ll long long
#define ld long double
#define ar array
 
const int INF = 1e15;
const int MOD = 1e9+7;
 
template<class T> struct Node {
    Node *lc = 0, *rc = 0;
    int lx, rx; T value, operation;
 
    const T NEUTRAL_ELEMENT = 0;
    const T NO_OPERATION = 0;
 
    T calc_op(T a, T b) {
        return a + b;
    }
 
    void modify_op(T &a, T b, int len) {
        if (b == NO_OPERATION) return;
        a = b*len;
    }
 
    Node(int _lx, int _rx) : lx(_lx), rx(_rx) {
        value = NEUTRAL_ELEMENT;
        operation = NO_OPERATION;
    }
    Node(vector<T> &v, int _lx, int _rx) : lx(_lx), rx(_rx) {
        operation = NO_OPERATION;
        if (rx - lx == 1) value = v[lx];
        else {
            int m = (lx + rx) >> 1;
            lc = new Node(v, lx, m), rc = new Node(v, m, rx);
            value = calc_op(lc->value, rc->value);
        }
    }  
 
    void propogate() {
        if (rx - lx == 1) return;
        int m = (lx + rx) >> 1;
        if (!lc) lc = new Node(lx, m);
        if (!rc) rc = new Node(m, rx);
        modify_op(lc->operation, operation, 1);
        modify_op(rc->operation, operation, 1);
        modify_op(lc->value, operation, m-lx);
        modify_op(rc->value, operation, rx-m);
        operation = NO_OPERATION;
    }
 
    void set(int i, int v) {
        if (rx - lx == 1) return void(value = v);
        propogate();
        int m = (lx + rx) >> 1;
        if (i < m) lc->set(i, v);
        else rc->set(i, v);
        value = calc_op(lc->value, rc->value);
    }
 
    void modify(int l, int r, int v) {
        if (l >= rx || lx >= r) return;
        if (l <= lx && rx <= r) {
            modify_op(operation, v, 1);
            modify_op(value, v, rx - lx);
            return;
        }
        propogate();
        lc->modify(l, r, v); rc->modify(l, r, v);
        value = calc_op(lc->value, rc->value);
    }
 
    T calc(int l, int r) {
        if (l >= rx || lx >= r) return NEUTRAL_ELEMENT;
        if (l <= lx && rx <= r) return value;
        propogate();
        int x = calc_op(lc->calc(l, r), rc->calc(l, r));
        return x;
    }
 
};
 
int32_t main() {
    cin.tie(0)->sync_with_stdio(0);
    int q; cin>>q;
    Node<int> smh(0, 1e9+7);
    int c = 0;
    while (q--) {
        //cout<<q<<endl;
        int t, l, r; cin>>t>>l>>r;
        l += c, r += c;
        if (t == 1) {
            c = smh.calc(l-1, r);
            cout<<c<<"\n";
        } else if (t == 2) {
            smh.modify(l-1, r, 1);
        }
    }
}  

Compilation message

apple.cpp:8:17: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+15' to '2147483647' [-Woverflow]
    8 | const int INF = 1e15;
      |                 ^~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 208 KB Output is correct
2 Correct 1 ms 248 KB Output is correct
3 Correct 1 ms 204 KB Output is correct
4 Correct 15 ms 4860 KB Output is correct
5 Correct 19 ms 5784 KB Output is correct
6 Correct 18 ms 5580 KB Output is correct
7 Correct 18 ms 5840 KB Output is correct
8 Correct 156 ms 43672 KB Output is correct
9 Correct 322 ms 75596 KB Output is correct
10 Correct 428 ms 83484 KB Output is correct
11 Correct 345 ms 90044 KB Output is correct
12 Correct 349 ms 92632 KB Output is correct
13 Correct 303 ms 107912 KB Output is correct
14 Correct 306 ms 108976 KB Output is correct
15 Correct 491 ms 199672 KB Output is correct
16 Correct 579 ms 201072 KB Output is correct
17 Correct 313 ms 112752 KB Output is correct
18 Correct 299 ms 112740 KB Output is correct
19 Correct 530 ms 205660 KB Output is correct
20 Correct 510 ms 205424 KB Output is correct