#include <bits/stdc++.h>
using namespace std;
const int INF = 1e8;
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.resize(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);
};
auto Query = [&](int l, int r, int t1, int t2) {
if (l == r) {
return (long long) max(t1 - t2, 0);
} else if (l < r) {
t1 -= l, t2 -= r;
long long res = Left.Query(l, --r, t1);
return res + max(t1 - t2, 0);
} else if (l > r) {
l = N - l, r = N - r;
t1 -= l, t2 -= r;
long long res = Right.Query(l, --r, t1);
return res + max(t1 - t2, 0);
}
};
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;
}
Compilation message
timeleap.cpp: In lambda function:
timeleap.cpp:142:3: warning: control reaches end of non-void function [-Wreturn-type]
};
^
# |
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 |
5 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 |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
7 ms |
640 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 |
512 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 |
512 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 |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
7 ms |
512 KB |
Output is correct |
29 |
Correct |
7 ms |
512 KB |
Output is correct |
30 |
Correct |
6 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 |
512 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 |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
512 KB |
Output is correct |
40 |
Correct |
6 ms |
512 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
666 ms |
54548 KB |
Output is correct |
2 |
Correct |
641 ms |
52088 KB |
Output is correct |
3 |
Correct |
631 ms |
52600 KB |
Output is correct |
4 |
Correct |
626 ms |
51192 KB |
Output is correct |
5 |
Correct |
675 ms |
54392 KB |
Output is correct |
6 |
Correct |
658 ms |
53760 KB |
Output is correct |
7 |
Correct |
644 ms |
55072 KB |
Output is correct |
8 |
Correct |
682 ms |
57080 KB |
Output is correct |
9 |
Correct |
640 ms |
51960 KB |
Output is correct |
10 |
Correct |
655 ms |
54904 KB |
Output is correct |
11 |
Correct |
666 ms |
54520 KB |
Output is correct |
12 |
Correct |
682 ms |
57592 KB |
Output is correct |
13 |
Correct |
682 ms |
58360 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 |
5 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 |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
4 ms |
384 KB |
Output is correct |
9 |
Correct |
5 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 |
512 KB |
Output is correct |
14 |
Correct |
6 ms |
512 KB |
Output is correct |
15 |
Correct |
6 ms |
512 KB |
Output is correct |
16 |
Correct |
7 ms |
640 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 |
512 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 |
512 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 |
6 ms |
512 KB |
Output is correct |
27 |
Correct |
6 ms |
512 KB |
Output is correct |
28 |
Correct |
7 ms |
512 KB |
Output is correct |
29 |
Correct |
7 ms |
512 KB |
Output is correct |
30 |
Correct |
6 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 |
512 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 |
7 ms |
512 KB |
Output is correct |
39 |
Correct |
6 ms |
512 KB |
Output is correct |
40 |
Correct |
6 ms |
512 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
666 ms |
54548 KB |
Output is correct |
43 |
Correct |
641 ms |
52088 KB |
Output is correct |
44 |
Correct |
631 ms |
52600 KB |
Output is correct |
45 |
Correct |
626 ms |
51192 KB |
Output is correct |
46 |
Correct |
675 ms |
54392 KB |
Output is correct |
47 |
Correct |
658 ms |
53760 KB |
Output is correct |
48 |
Correct |
644 ms |
55072 KB |
Output is correct |
49 |
Correct |
682 ms |
57080 KB |
Output is correct |
50 |
Correct |
640 ms |
51960 KB |
Output is correct |
51 |
Correct |
655 ms |
54904 KB |
Output is correct |
52 |
Correct |
666 ms |
54520 KB |
Output is correct |
53 |
Correct |
682 ms |
57592 KB |
Output is correct |
54 |
Correct |
682 ms |
58360 KB |
Output is correct |
55 |
Correct |
5 ms |
384 KB |
Output is correct |
56 |
Correct |
670 ms |
51120 KB |
Output is correct |
57 |
Correct |
643 ms |
48504 KB |
Output is correct |
58 |
Correct |
677 ms |
52344 KB |
Output is correct |
59 |
Correct |
676 ms |
52472 KB |
Output is correct |
60 |
Correct |
653 ms |
49272 KB |
Output is correct |
61 |
Correct |
722 ms |
53808 KB |
Output is correct |
62 |
Correct |
680 ms |
53624 KB |
Output is correct |
63 |
Correct |
684 ms |
53496 KB |
Output is correct |
64 |
Correct |
691 ms |
53972 KB |
Output is correct |
65 |
Correct |
667 ms |
51704 KB |
Output is correct |
66 |
Correct |
663 ms |
51832 KB |
Output is correct |
67 |
Correct |
681 ms |
53496 KB |
Output is correct |
68 |
Correct |
620 ms |
49656 KB |
Output is correct |
69 |
Correct |
677 ms |
54776 KB |
Output is correct |
70 |
Correct |
647 ms |
49656 KB |
Output is correct |
71 |
Correct |
646 ms |
47608 KB |
Output is correct |
72 |
Correct |
647 ms |
49404 KB |
Output is correct |
73 |
Correct |
678 ms |
53616 KB |
Output is correct |
74 |
Correct |
693 ms |
54008 KB |
Output is correct |
75 |
Correct |
677 ms |
54520 KB |
Output is correct |
76 |
Correct |
688 ms |
55160 KB |
Output is correct |
77 |
Correct |
5 ms |
384 KB |
Output is correct |