//besmellah
//mage mikhad chi beshe?
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define int long long
const int64_t INF = 1ll << 60;
const int N = 3e5 + 50;
int n, q, A[N], B[N], C[N], D[N], P[N], t[N], L[N], R[N], query_ans[N];
struct node {
int mxl, mnr, fil, fir, ansl, ansr, locl, locr;
};
int dist(int l, int r) {
return max(r - l, 0ll);
}
void calc(node x, int l, int &rel, int &reans) {
if (x.mxl <= x.mnr) {
rel = min(max(l, x.mxl), x.mnr);
reans += dist(rel, l);
}
else {
int dl = dist(x.fil, l) + x.ansl;
int dr = dist(x.fir, l) + x.ansr;
if (dl < dr) {
reans += dl;
rel = x.locl;
}
else {
reans += dr;
rel = x.locr;
}
}
}
node operator +(node l, node r) {
node R;
R.mxl = max(l.mxl, r.mxl);
R.mnr = min(l.mnr, r.mnr);
if (R.mxl <= R.mnr) {
R.ansl = R.ansr = 0;
R.locl = R.mxl;
R.locr = R.mnr;
R.fil = R.mxl;
R.fir = R.mnr;
return R;
}
R.fil = max(l.fil, r.fil);
R.fir = min(l.fir, r.fir);
if (l.mxl > l.mnr || R.fil > R.fir) {
R.fil = l.fil;
R.fir = l.fir;
}
R.ansl = R.ansr = 0;
int tl, tr;
calc(l, R.fil, tl, R.ansl);
calc(r, tl, R.locl, R.ansl);
calc(l, R.fir, tr, R.ansr);
calc(r, tr, R.locr, R.ansr);
return R;
}
node seg[N << 2];
//int mxl, mnr, fil, fir, ansl, ansr, locl, locr;
void build(int v, int l, int r) {
if (l + 1 == r) {
seg[v] = {L[l], R[l], L[l], R[l], 0, 0, L[l], R[l]};
return;
}
int m = l + r >> 1;
build(v << 1, l, m);
build(v << 1 | 1, m, r);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
void update(int v, int l, int r, int ind, int L, int R) {
if (l + 1 == r) {
seg[v] = {L, R, L, R, 0, 0, L, R};
return;
}
int m = l + r >> 1;
if (ind < m)
update(v << 1, l, m, ind, L, R);
else
update(v << 1 | 1, m, r, ind, L, R);
seg[v] = seg[v << 1] + seg[v << 1 | 1];
}
//void calc(node x, int l, int &rel, int &reans) {
void get(int v, int l, int r, int L, int R, int &time, int &re) {
if (L <= l && r <= R) {
calc(seg[v], time, time, re);
return;
}
int m = l + r >> 1;
if (R <= m)
get(v << 1, l, m, L, R, time, re);
else if (L >= m)
get(v << 1 | 1, m, r, L, R, time, re);
else {
get(v << 1, l, m, L, R, time, re);
get(v << 1 | 1, m, r, L, R, time, re);
}
}
void solve() {
FOR(i, 1, n) {
L[i] -= i;
R[i] -= i + 1;
}
build(1, 1, n);
FOR(i, 0, q) {
B[i] -= (t[i] == 1 ? P[i] : A[i]);
D[i] -= (t[i] == 1 ? P[i] + 1 : C[i]);
if (t[i] == 1) {
update(1, 1, n, P[i], B[i], D[i]);
}
else if (C[i] > A[i]) {
int time = B[i];
get(1, 1, n, A[i], C[i], time, query_ans[i]);
query_ans[i] += dist(D[i], time);
}
else if (C[i] == A[i])
query_ans[i] = dist(D[i], B[i]);
}
FOR(i, 1, n) {
L[i] += i;
R[i] += i + 1;
}
FOR(i, 0, q) {
B[i] += (t[i] == 1 ? P[i] : A[i]);
D[i] += (t[i] == 1 ? P[i] + 1 : C[i]);
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> q;
if (n == 1) {
while (q--) {
int t, a, b, c, d;
cin >> t >> a >> b >> c >> d;
cout << dist(d, b) << '\n';
}
return 0;
}
FOR(i, 1, n)
cin >> L[i] >> R[i];
FOR(i, 0, q) {
cin >> t[i];
if (t[i] == 1) {
cin >> P[i] >> B[i] >> D[i];
}
else {
cin >> A[i] >> B[i] >> C[i] >> D[i];
}
}
solve();
reverse(L + 1, L + n);
reverse(R + 1, R + n);
FOR(i, 0, q) {
if (t[i] == 1) {
P[i] = n - P[i];
}
else {
A[i] = n + 1 - A[i];
C[i] = n + 1 - C[i];
}
}
solve();
FOR(i, 0, q)
if (t[i] == 2) {
cout << query_ans[i] << '\n';
}
return 0;
}
//19:33:34
Compilation message
timeleap.cpp: In function 'void build(long long int, long long int, long long int)':
timeleap.cpp:71:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
71 | int m = l + r >> 1;
| ~~^~~
timeleap.cpp: In function 'void update(long long int, long long int, long long int, long long int, long long int, long long int)':
timeleap.cpp:81:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int m = l + r >> 1;
| ~~^~~
timeleap.cpp: In function 'void get(long long int, long long int, long long int, long long int, long long int, long long int&, long long int&)':
timeleap.cpp:94:23: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
94 | int m = l + r >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
708 KB |
Output is correct |
23 |
Correct |
1 ms |
856 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
2 ms |
736 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
732 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
636 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
688 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
442 ms |
104244 KB |
Output is correct |
2 |
Correct |
400 ms |
70704 KB |
Output is correct |
3 |
Correct |
440 ms |
70996 KB |
Output is correct |
4 |
Correct |
412 ms |
70544 KB |
Output is correct |
5 |
Correct |
414 ms |
104152 KB |
Output is correct |
6 |
Correct |
389 ms |
96632 KB |
Output is correct |
7 |
Correct |
424 ms |
104260 KB |
Output is correct |
8 |
Correct |
406 ms |
105380 KB |
Output is correct |
9 |
Correct |
368 ms |
70996 KB |
Output is correct |
10 |
Correct |
417 ms |
104412 KB |
Output is correct |
11 |
Correct |
418 ms |
104276 KB |
Output is correct |
12 |
Correct |
424 ms |
105044 KB |
Output is correct |
13 |
Correct |
425 ms |
105116 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
356 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
2 ms |
604 KB |
Output is correct |
12 |
Correct |
2 ms |
604 KB |
Output is correct |
13 |
Correct |
2 ms |
600 KB |
Output is correct |
14 |
Correct |
2 ms |
604 KB |
Output is correct |
15 |
Correct |
2 ms |
604 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
2 ms |
604 KB |
Output is correct |
22 |
Correct |
2 ms |
708 KB |
Output is correct |
23 |
Correct |
1 ms |
856 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
2 ms |
736 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
2 ms |
604 KB |
Output is correct |
28 |
Correct |
2 ms |
604 KB |
Output is correct |
29 |
Correct |
2 ms |
732 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
636 KB |
Output is correct |
33 |
Correct |
1 ms |
604 KB |
Output is correct |
34 |
Correct |
2 ms |
604 KB |
Output is correct |
35 |
Correct |
1 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
604 KB |
Output is correct |
37 |
Correct |
2 ms |
688 KB |
Output is correct |
38 |
Correct |
2 ms |
604 KB |
Output is correct |
39 |
Correct |
2 ms |
604 KB |
Output is correct |
40 |
Correct |
2 ms |
604 KB |
Output is correct |
41 |
Correct |
1 ms |
344 KB |
Output is correct |
42 |
Correct |
442 ms |
104244 KB |
Output is correct |
43 |
Correct |
400 ms |
70704 KB |
Output is correct |
44 |
Correct |
440 ms |
70996 KB |
Output is correct |
45 |
Correct |
412 ms |
70544 KB |
Output is correct |
46 |
Correct |
414 ms |
104152 KB |
Output is correct |
47 |
Correct |
389 ms |
96632 KB |
Output is correct |
48 |
Correct |
424 ms |
104260 KB |
Output is correct |
49 |
Correct |
406 ms |
105380 KB |
Output is correct |
50 |
Correct |
368 ms |
70996 KB |
Output is correct |
51 |
Correct |
417 ms |
104412 KB |
Output is correct |
52 |
Correct |
418 ms |
104276 KB |
Output is correct |
53 |
Correct |
424 ms |
105044 KB |
Output is correct |
54 |
Correct |
425 ms |
105116 KB |
Output is correct |
55 |
Correct |
0 ms |
348 KB |
Output is correct |
56 |
Correct |
683 ms |
103416 KB |
Output is correct |
57 |
Correct |
609 ms |
69864 KB |
Output is correct |
58 |
Correct |
646 ms |
103676 KB |
Output is correct |
59 |
Correct |
609 ms |
103632 KB |
Output is correct |
60 |
Correct |
599 ms |
69972 KB |
Output is correct |
61 |
Correct |
621 ms |
104084 KB |
Output is correct |
62 |
Correct |
651 ms |
104028 KB |
Output is correct |
63 |
Correct |
647 ms |
104056 KB |
Output is correct |
64 |
Correct |
652 ms |
104204 KB |
Output is correct |
65 |
Correct |
639 ms |
103544 KB |
Output is correct |
66 |
Correct |
683 ms |
103528 KB |
Output is correct |
67 |
Correct |
685 ms |
104020 KB |
Output is correct |
68 |
Correct |
598 ms |
79480 KB |
Output is correct |
69 |
Correct |
682 ms |
104076 KB |
Output is correct |
70 |
Correct |
630 ms |
70372 KB |
Output is correct |
71 |
Correct |
592 ms |
69292 KB |
Output is correct |
72 |
Correct |
583 ms |
75496 KB |
Output is correct |
73 |
Correct |
597 ms |
103508 KB |
Output is correct |
74 |
Correct |
631 ms |
103480 KB |
Output is correct |
75 |
Correct |
632 ms |
103680 KB |
Output is correct |
76 |
Correct |
654 ms |
104456 KB |
Output is correct |
77 |
Correct |
1 ms |
348 KB |
Output is correct |