#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define V vector
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
string _reset = "\u001b[0m", _yellow = "\u001b[33m", _bold = "\u001b[1m";
void DBG() { cerr << "]" << _reset << endl; }
template<class H, class...T> void DBG(H h, T ...t) {
cerr << to_string(h);
if(sizeof ...(t)) cerr << ", ";
DBG(t...);
}
#ifdef CHEISSMART
#define debug(...) cerr << _yellow << _bold << "Line(" << __LINE__ << ") -> [" << #__VA_ARGS__ << "]: [", DBG(__VA_ARGS__)
#else
#define debug(...)
#endif
const int INF = 1e9 + 7, N = 3e5 + 7;
int n, q;
struct node {
int l, r, x, y;
ll cost;
bool type; //0: only l, r
node(int _l = 0, int _r = 0) {
l = _l, r = _r;
x = y = 0, type = 0, cost = 0;
assert(l <= r);
}
pair<int, ll> eval(int pos) {
ll ans = 0;
int pre = pos;
pos = max(pos, l), pos = min(pos, r);
ans += max(0, pre - pos);
if(type) {
ans += cost;
pre = pos, pos = x;
ans += max(0, pre - pos);
pos = y;
}
return {pos, ans};
}
friend node operator + (node lhs, node rhs) {
node res;
if(lhs.type) {
res.l = lhs.l, res.r = lhs.r, res.x = lhs.x, res.type = 1;
auto [pos, cost] = rhs.eval(lhs.y);
res.cost = lhs.cost + cost;
res.y = pos;
} else {
if(max(lhs.l, rhs.l) <= min(lhs.r, rhs.r)) {
res = rhs;
res.l = max(lhs.l, rhs.l);
res.r = min(lhs.r, rhs.r);
} else {
res.l = lhs.l, res.r = lhs.r, res.type = 1;
if(rhs.l > lhs.r) {
res.x = rhs.l;
} else {
assert(rhs.r < lhs.l);
res.x = rhs.r;
}
tie(res.y, res.cost) = rhs.eval(res.x);
}
}
return res;
}
};
struct DS {
node t[N * 4];
void upd(int pos, node x, int v = 1, int tl = 0, int tr = n) {
if(tr - tl == 1) {
assert(pos == tl);
t[v] = x;
return;
}
int tm = (tl + tr) / 2;
if(pos < tm)
upd(pos, x, v * 2, tl, tm);
else
upd(pos, x, v * 2 + 1, tm, tr);
t[v] = t[v * 2] + t[v * 2 + 1];
}
void upd(int i, int l, int r) {
l -= i, r -= i;
upd(i, node(l, r));
}
node qry(int l, int r, int v = 1, int tl = 0, int tr = n) {
if(l <= tl && tr <= r)
return t[v];
int tm = (tl + tr) / 2;
if(r <= tm)
return qry(l, r, v * 2, tl, tm);
else if(l >= tm)
return qry(l, r, v * 2 + 1, tm, tr);
else
return qry(l, r, v * 2, tl, tm) + qry(l, r, v * 2 + 1, tm, tr);
}
};
DS t1, t2;
signed main()
{
IO_OP;
cin >> n >> q;
for(int i = 0; i < n - 1; i++) {
int l, r;
cin >> l >> r;
r--;
t1.upd(i, l, r);
t2.upd(n - i - 2, l, r);
}
while(q--) {
int op;
cin >> op;
if(op == 1) {
int p, l, r;
cin >> p >> l >> r;
r--, p--;
t1.upd(p, l, r);
t2.upd(n - p - 2, l, r);
} else {
int a, b, c, d;
cin >> a >> b >> c >> d;
a--, c--;
if(a == c) {
cout << max(b - d, 0) << '\n';
} else if(a < c) {
b -= a, d -= c;
auto u = t1.qry(a, c);
auto [pos, cost] = u.eval(b);
cost += max(pos - d, 0);
cout << cost << '\n';
} else { // a > c
a--;
a = n - a - 2, c = n - c - 2;
c++;
b -= a, d -= c;
auto u = t2.qry(a, c);
auto [pos, cost] = u.eval(b);
cost += max(pos - d, 0);
cout << cost << '\n';
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
75332 KB |
Output is correct |
2 |
Correct |
35 ms |
75388 KB |
Output is correct |
3 |
Correct |
40 ms |
75340 KB |
Output is correct |
4 |
Correct |
33 ms |
75380 KB |
Output is correct |
5 |
Correct |
33 ms |
75340 KB |
Output is correct |
6 |
Correct |
34 ms |
75400 KB |
Output is correct |
7 |
Correct |
34 ms |
75332 KB |
Output is correct |
8 |
Correct |
35 ms |
75356 KB |
Output is correct |
9 |
Correct |
35 ms |
75376 KB |
Output is correct |
10 |
Correct |
36 ms |
75468 KB |
Output is correct |
11 |
Correct |
35 ms |
75384 KB |
Output is correct |
12 |
Correct |
34 ms |
75396 KB |
Output is correct |
13 |
Correct |
41 ms |
75424 KB |
Output is correct |
14 |
Correct |
38 ms |
75468 KB |
Output is correct |
15 |
Correct |
35 ms |
75464 KB |
Output is correct |
16 |
Correct |
42 ms |
75452 KB |
Output is correct |
17 |
Correct |
35 ms |
75480 KB |
Output is correct |
18 |
Correct |
34 ms |
75356 KB |
Output is correct |
19 |
Correct |
35 ms |
75408 KB |
Output is correct |
20 |
Correct |
36 ms |
75416 KB |
Output is correct |
21 |
Correct |
40 ms |
75448 KB |
Output is correct |
22 |
Correct |
46 ms |
75480 KB |
Output is correct |
23 |
Correct |
36 ms |
75468 KB |
Output is correct |
24 |
Correct |
34 ms |
75356 KB |
Output is correct |
25 |
Correct |
36 ms |
75424 KB |
Output is correct |
26 |
Correct |
34 ms |
75468 KB |
Output is correct |
27 |
Correct |
35 ms |
75332 KB |
Output is correct |
28 |
Correct |
35 ms |
75416 KB |
Output is correct |
29 |
Correct |
42 ms |
75332 KB |
Output is correct |
30 |
Correct |
36 ms |
75408 KB |
Output is correct |
31 |
Correct |
43 ms |
75444 KB |
Output is correct |
32 |
Correct |
41 ms |
75492 KB |
Output is correct |
33 |
Correct |
36 ms |
75392 KB |
Output is correct |
34 |
Correct |
35 ms |
75332 KB |
Output is correct |
35 |
Correct |
35 ms |
75444 KB |
Output is correct |
36 |
Correct |
35 ms |
75368 KB |
Output is correct |
37 |
Correct |
37 ms |
75396 KB |
Output is correct |
38 |
Correct |
36 ms |
75332 KB |
Output is correct |
39 |
Correct |
34 ms |
75444 KB |
Output is correct |
40 |
Correct |
34 ms |
75404 KB |
Output is correct |
41 |
Correct |
33 ms |
75380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
703 ms |
79600 KB |
Output is correct |
2 |
Correct |
694 ms |
79556 KB |
Output is correct |
3 |
Correct |
629 ms |
79548 KB |
Output is correct |
4 |
Correct |
633 ms |
79628 KB |
Output is correct |
5 |
Correct |
668 ms |
79796 KB |
Output is correct |
6 |
Correct |
652 ms |
79672 KB |
Output is correct |
7 |
Correct |
698 ms |
79740 KB |
Output is correct |
8 |
Correct |
730 ms |
79752 KB |
Output is correct |
9 |
Correct |
708 ms |
79812 KB |
Output is correct |
10 |
Correct |
671 ms |
79672 KB |
Output is correct |
11 |
Correct |
681 ms |
79644 KB |
Output is correct |
12 |
Correct |
696 ms |
79752 KB |
Output is correct |
13 |
Correct |
813 ms |
79788 KB |
Output is correct |
14 |
Correct |
33 ms |
75368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
75332 KB |
Output is correct |
2 |
Correct |
35 ms |
75388 KB |
Output is correct |
3 |
Correct |
40 ms |
75340 KB |
Output is correct |
4 |
Correct |
33 ms |
75380 KB |
Output is correct |
5 |
Correct |
33 ms |
75340 KB |
Output is correct |
6 |
Correct |
34 ms |
75400 KB |
Output is correct |
7 |
Correct |
34 ms |
75332 KB |
Output is correct |
8 |
Correct |
35 ms |
75356 KB |
Output is correct |
9 |
Correct |
35 ms |
75376 KB |
Output is correct |
10 |
Correct |
36 ms |
75468 KB |
Output is correct |
11 |
Correct |
35 ms |
75384 KB |
Output is correct |
12 |
Correct |
34 ms |
75396 KB |
Output is correct |
13 |
Correct |
41 ms |
75424 KB |
Output is correct |
14 |
Correct |
38 ms |
75468 KB |
Output is correct |
15 |
Correct |
35 ms |
75464 KB |
Output is correct |
16 |
Correct |
42 ms |
75452 KB |
Output is correct |
17 |
Correct |
35 ms |
75480 KB |
Output is correct |
18 |
Correct |
34 ms |
75356 KB |
Output is correct |
19 |
Correct |
35 ms |
75408 KB |
Output is correct |
20 |
Correct |
36 ms |
75416 KB |
Output is correct |
21 |
Correct |
40 ms |
75448 KB |
Output is correct |
22 |
Correct |
46 ms |
75480 KB |
Output is correct |
23 |
Correct |
36 ms |
75468 KB |
Output is correct |
24 |
Correct |
34 ms |
75356 KB |
Output is correct |
25 |
Correct |
36 ms |
75424 KB |
Output is correct |
26 |
Correct |
34 ms |
75468 KB |
Output is correct |
27 |
Correct |
35 ms |
75332 KB |
Output is correct |
28 |
Correct |
35 ms |
75416 KB |
Output is correct |
29 |
Correct |
42 ms |
75332 KB |
Output is correct |
30 |
Correct |
36 ms |
75408 KB |
Output is correct |
31 |
Correct |
43 ms |
75444 KB |
Output is correct |
32 |
Correct |
41 ms |
75492 KB |
Output is correct |
33 |
Correct |
36 ms |
75392 KB |
Output is correct |
34 |
Correct |
35 ms |
75332 KB |
Output is correct |
35 |
Correct |
35 ms |
75444 KB |
Output is correct |
36 |
Correct |
35 ms |
75368 KB |
Output is correct |
37 |
Correct |
37 ms |
75396 KB |
Output is correct |
38 |
Correct |
36 ms |
75332 KB |
Output is correct |
39 |
Correct |
34 ms |
75444 KB |
Output is correct |
40 |
Correct |
34 ms |
75404 KB |
Output is correct |
41 |
Correct |
33 ms |
75380 KB |
Output is correct |
42 |
Correct |
703 ms |
79600 KB |
Output is correct |
43 |
Correct |
694 ms |
79556 KB |
Output is correct |
44 |
Correct |
629 ms |
79548 KB |
Output is correct |
45 |
Correct |
633 ms |
79628 KB |
Output is correct |
46 |
Correct |
668 ms |
79796 KB |
Output is correct |
47 |
Correct |
652 ms |
79672 KB |
Output is correct |
48 |
Correct |
698 ms |
79740 KB |
Output is correct |
49 |
Correct |
730 ms |
79752 KB |
Output is correct |
50 |
Correct |
708 ms |
79812 KB |
Output is correct |
51 |
Correct |
671 ms |
79672 KB |
Output is correct |
52 |
Correct |
681 ms |
79644 KB |
Output is correct |
53 |
Correct |
696 ms |
79752 KB |
Output is correct |
54 |
Correct |
813 ms |
79788 KB |
Output is correct |
55 |
Correct |
33 ms |
75368 KB |
Output is correct |
56 |
Correct |
760 ms |
77504 KB |
Output is correct |
57 |
Correct |
743 ms |
77532 KB |
Output is correct |
58 |
Correct |
819 ms |
77524 KB |
Output is correct |
59 |
Correct |
780 ms |
77640 KB |
Output is correct |
60 |
Correct |
746 ms |
77620 KB |
Output is correct |
61 |
Correct |
776 ms |
77576 KB |
Output is correct |
62 |
Correct |
788 ms |
77548 KB |
Output is correct |
63 |
Correct |
795 ms |
77500 KB |
Output is correct |
64 |
Correct |
789 ms |
77508 KB |
Output is correct |
65 |
Correct |
813 ms |
77584 KB |
Output is correct |
66 |
Correct |
820 ms |
77504 KB |
Output is correct |
67 |
Correct |
750 ms |
77620 KB |
Output is correct |
68 |
Correct |
749 ms |
77520 KB |
Output is correct |
69 |
Correct |
816 ms |
77636 KB |
Output is correct |
70 |
Correct |
770 ms |
77536 KB |
Output is correct |
71 |
Correct |
721 ms |
77488 KB |
Output is correct |
72 |
Correct |
755 ms |
77492 KB |
Output is correct |
73 |
Correct |
754 ms |
77488 KB |
Output is correct |
74 |
Correct |
818 ms |
77456 KB |
Output is correct |
75 |
Correct |
770 ms |
77488 KB |
Output is correct |
76 |
Correct |
788 ms |
77464 KB |
Output is correct |
77 |
Correct |
34 ms |
75332 KB |
Output is correct |