#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL * 1e18;
struct Node { long long ll, lr, rl, rr; };
Node comb(Node X, Node Y) {
Node ret;
ret.ll = max(X.ll + Y.rl, X.lr + Y.ll);
ret.lr = max({X.ll + Y.rr, X.lr + Y.lr, 0LL});
ret.rl = max({X.rl + Y.rl, X.rr + Y.ll, 0LL});
ret.rr = max(X.rl + Y.rr, X.rr + Y.lr);
return ret;
}
struct segtree {
vector<Node> T;
segtree(int sz) {
T.resize(4*sz);
}
Node get(int idx, int s, int e, int l, int r) {
if(r < s || e < l) return {-INF, 0, 0, -INF};
if(l <= s && e <= r) return T[idx];
int m = s+e >> 1;
return comb(get(idx*2, s, m, l, r), get(idx*2+1, m+1, e, l, r));
}
void upd(int idx, int s, int e, int p, int S, int E) {
if(p < s || e < p) return;
if(s == e) {
T[idx].ll = S;
T[idx].rr = -E;
T[idx].lr = T[idx].rl = 0;
return;
}
int m = s+e >> 1;
upd(idx*2, s, m, p, S, E);
upd(idx*2+1, m+1, e, p, S, E);
T[idx] = comb(T[idx*2], T[idx*2+1]);
}
};
int L[300009], R[300009];
int main() {
int N, Q; scanf("%d%d",&N,&Q);
for(int i=1; i<N; i++) scanf("%d%d",&L[i],&R[i]);
segtree AC(N), CA(N);
for(int i=1; i<N; i++) AC.upd(1, 1, N-1, i, L[i] - i, R[i] - 1 - i);
for(int i=1; i<N; i++) CA.upd(1, 1, N-1, i, L[N-i] - i, R[N-i] - 1 - i);
while(Q--) {
int ty; scanf("%d",&ty);
if(ty == 1) {
int P, S, E; scanf("%d%d%d",&P,&S,&E);
AC.upd(1, 1, N-1, P, S - P, E - 1 - P);
P = N - P;
CA.upd(1, 1, N-1, P, S - P, E - 1 - P);
}
if(ty == 2) {
int A, B, C, D; scanf("%d%d%d%d",&A,&B,&C,&D);
if(A < C) {
B -= A; D -= C;
Node st = {B, 0, -INF, -B}, en = {D, 0, -INF, -D};
printf("%lld\n", comb(comb(st, AC.get(1, 1, N-1, A, C-1)), en).lr);
}
if(C < A) {
A = N - A + 1; C = N - C + 1;
B -= A; D -= C;
Node st = {B, 0, -INF, -B}, en = {D, 0, -INF, -D};
printf("%lld\n", comb(comb(st, CA.get(1, 1, N-1, A, C-1)), en).lr);
}
if(A == C) printf("%lld\n", 1LL * max(B - D, 0));
}
}
return 0;
}
Compilation message
timeleap.cpp: In member function 'Node segtree::get(int, int, int, int, int)':
timeleap.cpp:25:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
timeleap.cpp: In member function 'void segtree::upd(int, int, int, int, int, int)':
timeleap.cpp:36:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = s+e >> 1;
~^~
timeleap.cpp: In function 'int main()':
timeleap.cpp:46:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int N, Q; scanf("%d%d",&N,&Q);
~~~~~^~~~~~~~~~~~~~
timeleap.cpp:47:30: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<N; i++) scanf("%d%d",&L[i],&R[i]);
~~~~~^~~~~~~~~~~~~~~~~~~~
timeleap.cpp:53:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int ty; scanf("%d",&ty);
~~~~~^~~~~~~~~~
timeleap.cpp:55:22: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int P, S, E; scanf("%d%d%d",&P,&S,&E);
~~~~~^~~~~~~~~~~~~~~~~~~
timeleap.cpp:61:25: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
int A, B, C, D; scanf("%d%d%d%d",&A,&B,&C,&D);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
640 KB |
Output is correct |
16 |
Correct |
4 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
4 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
4 ms |
640 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
640 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
4 ms |
684 KB |
Output is correct |
32 |
Correct |
4 ms |
640 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
4 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
640 KB |
Output is correct |
39 |
Correct |
3 ms |
640 KB |
Output is correct |
40 |
Correct |
4 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
936 ms |
78748 KB |
Output is correct |
2 |
Correct |
923 ms |
72256 KB |
Output is correct |
3 |
Correct |
927 ms |
73208 KB |
Output is correct |
4 |
Correct |
914 ms |
70868 KB |
Output is correct |
5 |
Correct |
931 ms |
76280 KB |
Output is correct |
6 |
Correct |
909 ms |
74872 KB |
Output is correct |
7 |
Correct |
917 ms |
77432 KB |
Output is correct |
8 |
Correct |
941 ms |
80396 KB |
Output is correct |
9 |
Correct |
884 ms |
71380 KB |
Output is correct |
10 |
Correct |
964 ms |
76952 KB |
Output is correct |
11 |
Correct |
941 ms |
76160 KB |
Output is correct |
12 |
Correct |
963 ms |
81656 KB |
Output is correct |
13 |
Correct |
986 ms |
82912 KB |
Output is correct |
14 |
Correct |
2 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
2 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
2 ms |
384 KB |
Output is correct |
9 |
Correct |
2 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
640 KB |
Output is correct |
12 |
Correct |
3 ms |
640 KB |
Output is correct |
13 |
Correct |
4 ms |
640 KB |
Output is correct |
14 |
Correct |
3 ms |
640 KB |
Output is correct |
15 |
Correct |
4 ms |
640 KB |
Output is correct |
16 |
Correct |
4 ms |
640 KB |
Output is correct |
17 |
Correct |
4 ms |
640 KB |
Output is correct |
18 |
Correct |
4 ms |
640 KB |
Output is correct |
19 |
Correct |
4 ms |
640 KB |
Output is correct |
20 |
Correct |
4 ms |
640 KB |
Output is correct |
21 |
Correct |
4 ms |
640 KB |
Output is correct |
22 |
Correct |
4 ms |
640 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
4 ms |
640 KB |
Output is correct |
25 |
Correct |
4 ms |
640 KB |
Output is correct |
26 |
Correct |
4 ms |
512 KB |
Output is correct |
27 |
Correct |
3 ms |
640 KB |
Output is correct |
28 |
Correct |
4 ms |
640 KB |
Output is correct |
29 |
Correct |
4 ms |
640 KB |
Output is correct |
30 |
Correct |
4 ms |
640 KB |
Output is correct |
31 |
Correct |
4 ms |
684 KB |
Output is correct |
32 |
Correct |
4 ms |
640 KB |
Output is correct |
33 |
Correct |
4 ms |
640 KB |
Output is correct |
34 |
Correct |
3 ms |
640 KB |
Output is correct |
35 |
Correct |
4 ms |
640 KB |
Output is correct |
36 |
Correct |
4 ms |
640 KB |
Output is correct |
37 |
Correct |
4 ms |
640 KB |
Output is correct |
38 |
Correct |
3 ms |
640 KB |
Output is correct |
39 |
Correct |
3 ms |
640 KB |
Output is correct |
40 |
Correct |
4 ms |
640 KB |
Output is correct |
41 |
Correct |
2 ms |
384 KB |
Output is correct |
42 |
Correct |
936 ms |
78748 KB |
Output is correct |
43 |
Correct |
923 ms |
72256 KB |
Output is correct |
44 |
Correct |
927 ms |
73208 KB |
Output is correct |
45 |
Correct |
914 ms |
70868 KB |
Output is correct |
46 |
Correct |
931 ms |
76280 KB |
Output is correct |
47 |
Correct |
909 ms |
74872 KB |
Output is correct |
48 |
Correct |
917 ms |
77432 KB |
Output is correct |
49 |
Correct |
941 ms |
80396 KB |
Output is correct |
50 |
Correct |
884 ms |
71380 KB |
Output is correct |
51 |
Correct |
964 ms |
76952 KB |
Output is correct |
52 |
Correct |
941 ms |
76160 KB |
Output is correct |
53 |
Correct |
963 ms |
81656 KB |
Output is correct |
54 |
Correct |
986 ms |
82912 KB |
Output is correct |
55 |
Correct |
2 ms |
256 KB |
Output is correct |
56 |
Correct |
954 ms |
73252 KB |
Output is correct |
57 |
Correct |
951 ms |
68316 KB |
Output is correct |
58 |
Correct |
1027 ms |
75376 KB |
Output is correct |
59 |
Correct |
1010 ms |
75980 KB |
Output is correct |
60 |
Correct |
978 ms |
70444 KB |
Output is correct |
61 |
Correct |
1002 ms |
78248 KB |
Output is correct |
62 |
Correct |
983 ms |
77944 KB |
Output is correct |
63 |
Correct |
979 ms |
77724 KB |
Output is correct |
64 |
Correct |
979 ms |
78456 KB |
Output is correct |
65 |
Correct |
973 ms |
74192 KB |
Output is correct |
66 |
Correct |
957 ms |
74976 KB |
Output is correct |
67 |
Correct |
977 ms |
77944 KB |
Output is correct |
68 |
Correct |
968 ms |
71684 KB |
Output is correct |
69 |
Correct |
1027 ms |
80632 KB |
Output is correct |
70 |
Correct |
928 ms |
71032 KB |
Output is correct |
71 |
Correct |
924 ms |
68400 KB |
Output is correct |
72 |
Correct |
922 ms |
71180 KB |
Output is correct |
73 |
Correct |
974 ms |
78840 KB |
Output is correct |
74 |
Correct |
1008 ms |
79028 KB |
Output is correct |
75 |
Correct |
1006 ms |
80504 KB |
Output is correct |
76 |
Correct |
996 ms |
80476 KB |
Output is correct |
77 |
Correct |
2 ms |
384 KB |
Output is correct |