Submission #864363

# Submission time Handle Problem Language Result Execution time Memory
864363 2023-10-22T15:54:43 Z anhphant Monkey and Apple-trees (IZhO12_apple) C++14
0 / 100
318 ms 262144 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'

ll M;

struct DYNAMIC_SEGMENT_TREE {
     struct Node {
        ll L, R, sum, lazy, ls, rs;
     };
     vector<Node> ST;

     DYNAMIC_SEGMENT_TREE() {
        ST = {{1, ll(1e9), 0, 0, -1, -1}};
     }

     void f(ll id) {
        ST[id].sum = ST[id].R - ST[id].L + 1;
        ST[id].lazy = 1;
     }

     void push_down(ll id) {
        ll MID = (ST[id].L + ST[id].R) / 2;
        if (ST[id].ls == -1) {
            ST[id].ls = ST.size();
            ST.push_back({ST[id].L, MID, 0, 0, -1, -1});
        }
        if (ST[id].rs == -1) {
            ST[id].rs = ST.size();
            ST.push_back({MID + 1, ST[id].R, 0, 0, -1, -1});
        }

        if (ST[id].lazy == 0) return;
        f(ST[id].ls);
        f(ST[id].rs);
        ST[id].lazy = 0;
     }

     void push_up(ll id) {
        ll MID = (ST[id].L + ST[id].R) / 2;
        if (ST[id].ls == -1) {
            ST[id].ls = ST.size();
            ST.push_back({ST[id].L, MID, 0, 0, -1, -1});
        }
        if (ST[id].rs == -1) {
            ST[id].rs = ST.size();
            ST.push_back({MID + 1, ST[id].R, 0, 0, -1, -1});
        }
        ST[id].sum = ST[ST[id].ls].sum + ST[ST[id].rs].sum;
     }

     void update(ll id, ll L, ll R) {
        push_down(id);
        if (ST[id].R < L || R < ST[id].L) return;
        //cerr << "update : " << id << " " << ST[id].L << " " << ST[id].R << endl;
        if (L <= ST[id].L && ST[id].R <= R) {
            f(id);
            push_down(id);
            return;
        }

        ll MID = (ST[id].L + ST[id].R) / 2;
        if (ST[id].ls == -1) {
            ST[id].ls = ST.size();
            ST.push_back({ST[id].L, MID, 0, 0, -1, -1});
        }
        if (ST[id].rs == -1) {
            ST[id].rs = ST.size();
            ST.push_back({MID + 1, ST[id].R, 0, 0, -1, -1});
        }

        update(ST[id].ls, L, R);
        update(ST[id].rs, L, R);
        push_down(ST[id].ls);
        push_down(ST[id].rs);
        push_up(id);
     }

     ll getans(ll id, ll L, ll R) {
        push_down(id);
        if (ST[id].R < L || R < ST[id].L) return 0;
        if (L <= ST[id].L && ST[id].R <= R) return ST[id].sum;
        push_down(id);
        return getans(ST[id].ls, L, R) + getans(ST[id].rs, L, R);
     }

     void debug() {
        for(int i = 0; i < ST.size(); ++i) {
            cerr << "i = " << i << " : " << ST[i].L << " " << ST[i].R << " " << ST[i].sum << " "
                << ST[i].lazy << " " << ST[i].ls << " " << ST[i].rs << endl;
        }
     }
};

int main() {
    ios_base :: sync_with_stdio(0);
    cin.tie(0); cout.tie(0); cerr.tie(0);
    if (fopen("FILE.INP", "r")) {
        freopen("FILE.INP", "r", stdin);
        freopen("FILE.OUT", "w", stdout);
    }

    cin >> M;
    DYNAMIC_SEGMENT_TREE St;

    ll C = 0;
    for(int _ = 1; _ <= M; ++_) {
        ll D, L, R;
        cin >> D >> L >> R;
        if (D == 2) {
            St.update(0, L + C, R + C);
        }
        else {
            C = St.getans(0, L + C, R + C);
            cout << C << endl;
        }
        //cerr << "query " << _ << endl;
        //St.debug();
        //cerr << endl;
    }
}

Compilation message

apple.cpp: In member function 'void DYNAMIC_SEGMENT_TREE::debug()':
apple.cpp:89:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<DYNAMIC_SEGMENT_TREE::Node>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   89 |         for(int i = 0; i < ST.size(); ++i) {
      |                        ~~^~~~~~~~~~~
apple.cpp: In function 'int main()':
apple.cpp:100:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  100 |         freopen("FILE.INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
apple.cpp:101:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  101 |         freopen("FILE.OUT", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 18 ms 14796 KB Output is correct
5 Correct 24 ms 13576 KB Output is correct
6 Correct 25 ms 14284 KB Output is correct
7 Correct 20 ms 14332 KB Output is correct
8 Correct 138 ms 100552 KB Output is correct
9 Correct 294 ms 198040 KB Output is correct
10 Correct 302 ms 198804 KB Output is correct
11 Correct 318 ms 197812 KB Output is correct
12 Correct 303 ms 198224 KB Output is correct
13 Runtime error 270 ms 262144 KB Execution killed with signal 9
14 Halted 0 ms 0 KB -