Submission #219693

# Submission time Handle Problem Language Result Execution time Memory
219693 2020-04-06T01:33:20 Z rama_pang Bitaro, who Leaps through Time (JOI19_timeleap) C++14
100 / 100
744 ms 42232 KB
#include <bits/stdc++.h>
using namespace std;

class TimeLeap { // segment tree
 private:
  struct Node {
    bool collision;

    int st, et; // interval where if we start in the beginning of the segment in this time interval, we do not need to 
                // timeleap at all until the collision

    long long timeleap; // the number of timeleaps needed to get from just the collision to the end of the segment. We 
                        // start at et if we collide with an L, or start at st if we collide with an R. Note that this 
                        // start points will be travelled through no matter what.

    int endtime; // endtime if there is a collision (since if there is, the endtime is forced)

    Node() {}
    Node(bool c, int s, int e, long long t, int et) : collision(c), st(s), et(e), timeleap(t), endtime(et) {}

    long long Travel(int &t) { // how many timeleaps are needed (note that t is modified to become the final time)
      long long res;
      if (!collision) { // we do not need to timeleap once we are in interval [st, et]
        if (t > et) { // travel back to et, then we do not need to timeleap again 
                      // (this is optimal since et is the last time where we do not need to timleap)
          res = t - et;
          t = et;
        } else { // we do not need to timeleap
          res = 0;
          t = max(t, st);
        }
      } else { // we collideR, so we first go to st/et from before collision then adding the rest of timeleaps
        res = max(t - st, 0) + timeleap;
        t = endtime;
      }
      return res;
    }

    friend Node merge(Node a, Node b) {
      Node res;
      if (!a.collision && !b.collision) {
        if (max(a.st, b.st) <= min(a.et, b.et)) { // no collision
          res = Node(false, max(a.st, b.st), min(a.et, b.et), 0, -1);
        } else if (a.et < b.st) { // there is a new collision with L
          res.collision = true;
          res.st = res.et = res.endtime = a.et;
          res.timeleap = b.Travel(res.endtime);
        } else if (a.st > b.et) { // there is a new collision with R
          res.collision = true;
          res.st = res.et = res.endtime = a.st;
          res.timeleap = b.Travel(res.endtime);
        } 
      } else if (a.collision) { // we collide in a first, so we simply add b to the answer if we travel from a.endtime
        res = a;
        res.timeleap += b.Travel(res.endtime);
      } else if (b.collision) { // we collide in b first
        res = b;
        if (a.et < b.st) {
          res.st = res.et = res.endtime = a.et;
          res.timeleap = b.Travel(res.endtime);
        } else if (a.st > b.st) {
          res.st = res.et = res.endtime = a.st;
          res.timeleap = b.Travel(res.endtime);
        }
      }
      return res;
    }
  };

  int sz;
  vector<Node> tree;

  void Update(int n, int tl, int tr, int pos, int newL, int newR) {
    if (tl == tr) return void(tree[n] = Node(false, newL, newR, 0, -1));
    int mid = (tl + tr) / 2;
    int lc = n + 1;
    int rc = n + 2 * (mid - tl + 1);
    if (pos <= mid) {
      Update(lc, tl, mid, pos, newL, newR);
    } else {
      Update(rc, mid + 1, tr, pos, newL, newR);
    }
    tree[n] = merge(tree[lc], tree[rc]);
  }

  long long Query(int n, int tl, int tr, int l, int r, int &t) {
    if (tr < l || r < tl || tl > tr || l > r) return 0;
    if (l <= tl && tr <= r) return tree[n].Travel(t);
    int mid = (tl + tr) / 2;
    int lc = n + 1;
    int rc = n + 2 * (mid - tl + 1);
    long long res = 0;
    res += Query(lc, tl, mid, l, r, t);
    res += Query(rc, mid + 1, tr, l, r, t);
    return res;
  }

 public:
  TimeLeap(int n) : sz(n), tree(vector<Node>(2 * sz)) {}

  void Update(int pos, int newL, int newR) {
    return Update(1, 0, sz - 1, pos, newL, newR);
  }

  long long Query(int l, int r, int &t) {
    return Query(1, 0, sz - 1, l, r, t);
  }  
};

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0), cout.tie(0);

  int N, Q;
  cin >> N >> Q;
  N--;

  TimeLeap Left(N), Right(N);

  auto Update = [&](int pos, int newL, int newR) {
    Left.Update(pos, newL - pos, newR - pos - 1);
    pos = N - pos - 1;
    Right.Update(pos, newL - pos, newR - pos - 1);
    return;
  };

  auto Query = [&](int l, int r, int t1, int t2) {
    long long res = 0;
    if (l == r) {
      res = max(t1 - t2, 0);
    } else if (l < r) {
      t1 -= l, t2 -= r;
      res = Left.Query(l, --r, t1) + max(t1 - t2, 0);
    } else if (l > r) {
      l = N - l, r = N - r;
      t1 -= l, t2 -= r;
      res = Right.Query(l, --r, t1) + max(t1 - t2, 0);
    }
    return res;
  };

  for (int i = 0; i < N; i++) {
    int L, R;
    cin >> L >> R;
    Update(i, L, R);
  }

  for (int i = 0; i < Q; i++) {
    int T;
    cin >> T;
    if (T == 1) {
      int P, S, E;
      cin >> P >> S >> E;
      Update(--P, S, E);
    } else if (T == 2) {
      int A, B, C, D;
      cin >> A >> B >> C >> D;
      cout << Query(--A, --C, B, D) << "\n";
    }
  }

  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 384 KB Output is correct
14 Correct 6 ms 384 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 6 ms 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 7 ms 512 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 7 ms 512 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 6 ms 512 KB Output is correct
33 Correct 6 ms 384 KB Output is correct
34 Correct 6 ms 512 KB Output is correct
35 Correct 6 ms 512 KB Output is correct
36 Correct 6 ms 512 KB Output is correct
37 Correct 6 ms 512 KB Output is correct
38 Correct 6 ms 512 KB Output is correct
39 Correct 7 ms 512 KB Output is correct
40 Correct 6 ms 512 KB Output is correct
41 Correct 4 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 674 ms 39160 KB Output is correct
2 Correct 619 ms 36856 KB Output is correct
3 Correct 631 ms 37368 KB Output is correct
4 Correct 630 ms 36216 KB Output is correct
5 Correct 628 ms 38904 KB Output is correct
6 Correct 633 ms 38136 KB Output is correct
7 Correct 652 ms 39444 KB Output is correct
8 Correct 642 ms 41040 KB Output is correct
9 Correct 620 ms 36728 KB Output is correct
10 Correct 630 ms 39544 KB Output is correct
11 Correct 661 ms 39032 KB Output is correct
12 Correct 657 ms 41508 KB Output is correct
13 Correct 674 ms 42232 KB Output is correct
14 Correct 5 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 384 KB Output is correct
2 Correct 5 ms 384 KB Output is correct
3 Correct 4 ms 384 KB Output is correct
4 Correct 5 ms 384 KB Output is correct
5 Correct 5 ms 384 KB Output is correct
6 Correct 5 ms 384 KB Output is correct
7 Correct 4 ms 384 KB Output is correct
8 Correct 4 ms 384 KB Output is correct
9 Correct 4 ms 384 KB Output is correct
10 Correct 4 ms 384 KB Output is correct
11 Correct 6 ms 512 KB Output is correct
12 Correct 6 ms 512 KB Output is correct
13 Correct 6 ms 384 KB Output is correct
14 Correct 6 ms 384 KB Output is correct
15 Correct 6 ms 512 KB Output is correct
16 Correct 6 ms 512 KB Output is correct
17 Correct 6 ms 512 KB Output is correct
18 Correct 6 ms 512 KB Output is correct
19 Correct 6 ms 384 KB Output is correct
20 Correct 6 ms 512 KB Output is correct
21 Correct 6 ms 512 KB Output is correct
22 Correct 6 ms 384 KB Output is correct
23 Correct 6 ms 512 KB Output is correct
24 Correct 6 ms 512 KB Output is correct
25 Correct 6 ms 512 KB Output is correct
26 Correct 7 ms 512 KB Output is correct
27 Correct 6 ms 512 KB Output is correct
28 Correct 6 ms 512 KB Output is correct
29 Correct 6 ms 512 KB Output is correct
30 Correct 7 ms 512 KB Output is correct
31 Correct 6 ms 512 KB Output is correct
32 Correct 6 ms 512 KB Output is correct
33 Correct 6 ms 384 KB Output is correct
34 Correct 6 ms 512 KB Output is correct
35 Correct 6 ms 512 KB Output is correct
36 Correct 6 ms 512 KB Output is correct
37 Correct 6 ms 512 KB Output is correct
38 Correct 6 ms 512 KB Output is correct
39 Correct 7 ms 512 KB Output is correct
40 Correct 6 ms 512 KB Output is correct
41 Correct 4 ms 384 KB Output is correct
42 Correct 674 ms 39160 KB Output is correct
43 Correct 619 ms 36856 KB Output is correct
44 Correct 631 ms 37368 KB Output is correct
45 Correct 630 ms 36216 KB Output is correct
46 Correct 628 ms 38904 KB Output is correct
47 Correct 633 ms 38136 KB Output is correct
48 Correct 652 ms 39444 KB Output is correct
49 Correct 642 ms 41040 KB Output is correct
50 Correct 620 ms 36728 KB Output is correct
51 Correct 630 ms 39544 KB Output is correct
52 Correct 661 ms 39032 KB Output is correct
53 Correct 657 ms 41508 KB Output is correct
54 Correct 674 ms 42232 KB Output is correct
55 Correct 5 ms 384 KB Output is correct
56 Correct 642 ms 36464 KB Output is correct
57 Correct 620 ms 33912 KB Output is correct
58 Correct 643 ms 37420 KB Output is correct
59 Correct 744 ms 37920 KB Output is correct
60 Correct 639 ms 35004 KB Output is correct
61 Correct 656 ms 39160 KB Output is correct
62 Correct 669 ms 38904 KB Output is correct
63 Correct 670 ms 38700 KB Output is correct
64 Correct 691 ms 39096 KB Output is correct
65 Correct 638 ms 36856 KB Output is correct
66 Correct 634 ms 37372 KB Output is correct
67 Correct 649 ms 38828 KB Output is correct
68 Correct 632 ms 36020 KB Output is correct
69 Correct 661 ms 40056 KB Output is correct
70 Correct 618 ms 35448 KB Output is correct
71 Correct 627 ms 34040 KB Output is correct
72 Correct 633 ms 35448 KB Output is correct
73 Correct 676 ms 39132 KB Output is correct
74 Correct 663 ms 39376 KB Output is correct
75 Correct 684 ms 40128 KB Output is correct
76 Correct 686 ms 40316 KB Output is correct
77 Correct 5 ms 384 KB Output is correct