#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 301010
#define MAXS 20
#define INF 1000000000000000001
#define bb ' '
#define ln '\n'
#define Ln '\n'
struct node {
ll down, up, fin, cst;
node() {
down = -INF;
up = INF;
fin = -1;
cst = 0;
}
node(ll down, ll up) :down(down), up(up), fin(down == up ? down : -1), cst(0) { }
node(ll down, ll up, ll fin, ll cst) :down(down), up(up), fin(fin), cst(cst) { }
};
inline node operator+(node n1, node n2) {
int c1, c2;
c1 = n1.down == n1.up;
c2 = n2.down == n2.up;
if (!c1 && !c2) {
if (n1.down >= n2.up) return node(n1.down, n1.down, n2.up, n1.down - n2.up);
if (n1.up <= n2.down) return node(n1.up, n1.up, n2.down, n2.down - n1.up);
return node(max(n1.down, n2.down), min(n1.up, n2.up), -1, 0);
}
if (c1 && !c2) {
node ret;
ret.cst = n1.cst;
ret.up = ret.down = n1.up;
if (n1.fin > n2.up) ret.fin = n2.up, ret.cst += n1.fin - n2.up;
else if (n1.fin < n2.down) ret.fin = n2.down, ret.cst += n2.down - n1.fin;
else ret.fin = n1.fin;
return ret;
}
if (!c1 && c2) {
node ret;
ret.fin = n2.fin;
ret.cst = n2.cst;
if (n1.down > n2.up) ret.up = ret.down = n1.down, ret.cst += n1.down - n2.up;
else if (n1.up < n2.up) ret.up = ret.down = n1.up, ret.cst += n2.up - n1.up;
else ret.up = ret.down = n2.up;
return ret;
}
if (c1 && c2) {
node ret;
ret.up = ret.down = n1.up;
ret.fin = n2.fin;
ret.cst = n1.cst + n2.cst + abs(n1.fin - n2.up);
return ret;
}
}
inline ll get(node n, ll sy, ll ey) {
ll fe;
ll ans = 0;
if (n.up == n.down) fe = n.fin, ans += n.cst + abs(n.up - sy);
else {
if (n.down <= sy && sy <= n.up) fe = sy;
else if (n.down > sy) fe = n.down, ans += n.down - sy;
else fe = n.up, ans += sy - n.up;
}
return ans + abs(ey - fe);
}
node narr[MAX];
namespace segtree {
node tree[MAX * 4];
void init(int s, int e, int loc = 1) {
if (s == e) {
tree[loc] = narr[s];
return;
}
int m = (s + e) >> 1;
init(s, m, loc * 2);
init(m + 1, e, loc * 2 + 1);
tree[loc] = tree[loc * 2] + tree[loc * 2 + 1];
}
void upd(int s, int e, int x, node n, int loc = 1) {
if (e < x || x < s) return;
if (s == e) {
tree[loc] = n;
return;
}
int m = (s + e) >> 1;
upd(s, m, x, n, loc * 2);
upd(m + 1, e, x, n, loc * 2 + 1);
tree[loc] = tree[loc * 2] + tree[loc * 2 + 1];
}
node query(int s, int e, int l, int r, int loc = 1) {
if (l <= s && e <= r) return tree[loc];
if (e < l || r < s) return node();
int m = (s + e) >> 1;
return query(s, m, l, r, loc * 2) + query(m + 1, e, l, r, loc * 2 + 1);
}
}
int N, M;
ll P[MAX];
ll S[MAX];
ll E[MAX];
vector<int> qv[MAX];
ll A[MAX];
ll B[MAX];
ll C[MAX];
ll D[MAX];
ll qt[MAX];
ll L[MAX];
ll R[MAX];
ll ans[MAX];
void solve() {
int i;
for (i = 1; i <= N; i++) narr[i] = node(L[i] - i, R[i] - i - 1);
segtree::init(1, N - 1);
for (i = 0; i <= M + 1; i++) {
if (1 <= i && i <= M) segtree::upd(1, N - 1, P[i], node(S[i] - P[i], E[i] - P[i] - 1));
for (auto v : qv[i]) {
ans[v] = get(segtree::query(1, N - 1, A[v], C[v] - 1), B[v] - A[v], D[v] - C[v]);
ans[v] = (ans[v] - (D[v] - C[v] - B[v] + A[v])) / 2;
}
}
}
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int Q;
cin >> N >> Q;
int i;
for (i = 1; i < N; i++) cin >> L[i] >> R[i];
int op;
for (i = 1; i <= Q; i++) {
cin >> op;
if (op == 1) {
M++;
cin >> P[M] >> S[M] >> E[M];
}
else {
cin >> A[i] >> B[i] >> C[i] >> D[i], qt[i] = M;
if (A[i] == C[i]) ans[i] = max(0ll, B[i] - D[i]);
}
}
if (N == 1) {
for (i = 1; i <= Q; i++) if (A[i]) cout << ans[i] << Ln;
return 0;
}
for (i = 1; i <= Q; i++) if (A[i] && A[i] < C[i]) qv[qt[i]].push_back(i);
solve();
for (i = 1; i <= N / 2; i++) swap(L[i], L[N - i]), swap(R[i], R[N - i]);
for (i = 1; i <= M; i++) P[i] = N - P[i];
for (i = 1; i <= Q; i++) if (A[i]) A[i] = N + 1 - A[i], C[i] = N + 1 - C[i];
for (i = 0; i <= M; i++) qv[i].clear();
for (i = 1; i <= Q; i++) if (A[i] && A[i] < C[i]) qv[qt[i]].push_back(i);
solve();
for (i = 1; i <= Q; i++) if (A[i]) cout << ans[i] << ln;
}
Compilation message
timeleap.cpp: In function 'node operator+(node, node)':
timeleap.cpp:61:1: warning: control reaches end of non-void function [-Wreturn-type]
61 | }
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
54484 KB |
Output is correct |
2 |
Correct |
25 ms |
54524 KB |
Output is correct |
3 |
Correct |
26 ms |
54532 KB |
Output is correct |
4 |
Correct |
26 ms |
54520 KB |
Output is correct |
5 |
Correct |
26 ms |
54512 KB |
Output is correct |
6 |
Correct |
25 ms |
54576 KB |
Output is correct |
7 |
Correct |
25 ms |
54576 KB |
Output is correct |
8 |
Correct |
27 ms |
54544 KB |
Output is correct |
9 |
Correct |
26 ms |
54556 KB |
Output is correct |
10 |
Correct |
26 ms |
54528 KB |
Output is correct |
11 |
Correct |
29 ms |
54668 KB |
Output is correct |
12 |
Correct |
26 ms |
54624 KB |
Output is correct |
13 |
Correct |
26 ms |
54628 KB |
Output is correct |
14 |
Correct |
26 ms |
54612 KB |
Output is correct |
15 |
Correct |
27 ms |
54580 KB |
Output is correct |
16 |
Correct |
26 ms |
54644 KB |
Output is correct |
17 |
Correct |
29 ms |
54644 KB |
Output is correct |
18 |
Correct |
31 ms |
54572 KB |
Output is correct |
19 |
Correct |
26 ms |
54592 KB |
Output is correct |
20 |
Correct |
26 ms |
54680 KB |
Output is correct |
21 |
Correct |
28 ms |
54668 KB |
Output is correct |
22 |
Correct |
29 ms |
54676 KB |
Output is correct |
23 |
Correct |
28 ms |
54660 KB |
Output is correct |
24 |
Correct |
29 ms |
54608 KB |
Output is correct |
25 |
Correct |
30 ms |
54660 KB |
Output is correct |
26 |
Correct |
27 ms |
54692 KB |
Output is correct |
27 |
Correct |
29 ms |
54656 KB |
Output is correct |
28 |
Correct |
27 ms |
54612 KB |
Output is correct |
29 |
Correct |
29 ms |
54620 KB |
Output is correct |
30 |
Correct |
28 ms |
54688 KB |
Output is correct |
31 |
Correct |
27 ms |
54612 KB |
Output is correct |
32 |
Correct |
26 ms |
54692 KB |
Output is correct |
33 |
Correct |
28 ms |
54644 KB |
Output is correct |
34 |
Correct |
31 ms |
54616 KB |
Output is correct |
35 |
Correct |
26 ms |
54652 KB |
Output is correct |
36 |
Correct |
26 ms |
54640 KB |
Output is correct |
37 |
Correct |
26 ms |
54604 KB |
Output is correct |
38 |
Correct |
27 ms |
54664 KB |
Output is correct |
39 |
Correct |
27 ms |
54644 KB |
Output is correct |
40 |
Correct |
27 ms |
54716 KB |
Output is correct |
41 |
Correct |
26 ms |
54476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
508 ms |
77784 KB |
Output is correct |
2 |
Correct |
515 ms |
77512 KB |
Output is correct |
3 |
Correct |
519 ms |
77740 KB |
Output is correct |
4 |
Correct |
535 ms |
77444 KB |
Output is correct |
5 |
Correct |
528 ms |
77732 KB |
Output is correct |
6 |
Correct |
533 ms |
77760 KB |
Output is correct |
7 |
Correct |
510 ms |
77976 KB |
Output is correct |
8 |
Correct |
546 ms |
94336 KB |
Output is correct |
9 |
Correct |
505 ms |
92984 KB |
Output is correct |
10 |
Correct |
526 ms |
93504 KB |
Output is correct |
11 |
Correct |
506 ms |
93376 KB |
Output is correct |
12 |
Correct |
534 ms |
94192 KB |
Output is correct |
13 |
Correct |
544 ms |
94376 KB |
Output is correct |
14 |
Correct |
26 ms |
54484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
54484 KB |
Output is correct |
2 |
Correct |
25 ms |
54524 KB |
Output is correct |
3 |
Correct |
26 ms |
54532 KB |
Output is correct |
4 |
Correct |
26 ms |
54520 KB |
Output is correct |
5 |
Correct |
26 ms |
54512 KB |
Output is correct |
6 |
Correct |
25 ms |
54576 KB |
Output is correct |
7 |
Correct |
25 ms |
54576 KB |
Output is correct |
8 |
Correct |
27 ms |
54544 KB |
Output is correct |
9 |
Correct |
26 ms |
54556 KB |
Output is correct |
10 |
Correct |
26 ms |
54528 KB |
Output is correct |
11 |
Correct |
29 ms |
54668 KB |
Output is correct |
12 |
Correct |
26 ms |
54624 KB |
Output is correct |
13 |
Correct |
26 ms |
54628 KB |
Output is correct |
14 |
Correct |
26 ms |
54612 KB |
Output is correct |
15 |
Correct |
27 ms |
54580 KB |
Output is correct |
16 |
Correct |
26 ms |
54644 KB |
Output is correct |
17 |
Correct |
29 ms |
54644 KB |
Output is correct |
18 |
Correct |
31 ms |
54572 KB |
Output is correct |
19 |
Correct |
26 ms |
54592 KB |
Output is correct |
20 |
Correct |
26 ms |
54680 KB |
Output is correct |
21 |
Correct |
28 ms |
54668 KB |
Output is correct |
22 |
Correct |
29 ms |
54676 KB |
Output is correct |
23 |
Correct |
28 ms |
54660 KB |
Output is correct |
24 |
Correct |
29 ms |
54608 KB |
Output is correct |
25 |
Correct |
30 ms |
54660 KB |
Output is correct |
26 |
Correct |
27 ms |
54692 KB |
Output is correct |
27 |
Correct |
29 ms |
54656 KB |
Output is correct |
28 |
Correct |
27 ms |
54612 KB |
Output is correct |
29 |
Correct |
29 ms |
54620 KB |
Output is correct |
30 |
Correct |
28 ms |
54688 KB |
Output is correct |
31 |
Correct |
27 ms |
54612 KB |
Output is correct |
32 |
Correct |
26 ms |
54692 KB |
Output is correct |
33 |
Correct |
28 ms |
54644 KB |
Output is correct |
34 |
Correct |
31 ms |
54616 KB |
Output is correct |
35 |
Correct |
26 ms |
54652 KB |
Output is correct |
36 |
Correct |
26 ms |
54640 KB |
Output is correct |
37 |
Correct |
26 ms |
54604 KB |
Output is correct |
38 |
Correct |
27 ms |
54664 KB |
Output is correct |
39 |
Correct |
27 ms |
54644 KB |
Output is correct |
40 |
Correct |
27 ms |
54716 KB |
Output is correct |
41 |
Correct |
26 ms |
54476 KB |
Output is correct |
42 |
Correct |
508 ms |
77784 KB |
Output is correct |
43 |
Correct |
515 ms |
77512 KB |
Output is correct |
44 |
Correct |
519 ms |
77740 KB |
Output is correct |
45 |
Correct |
535 ms |
77444 KB |
Output is correct |
46 |
Correct |
528 ms |
77732 KB |
Output is correct |
47 |
Correct |
533 ms |
77760 KB |
Output is correct |
48 |
Correct |
510 ms |
77976 KB |
Output is correct |
49 |
Correct |
546 ms |
94336 KB |
Output is correct |
50 |
Correct |
505 ms |
92984 KB |
Output is correct |
51 |
Correct |
526 ms |
93504 KB |
Output is correct |
52 |
Correct |
506 ms |
93376 KB |
Output is correct |
53 |
Correct |
534 ms |
94192 KB |
Output is correct |
54 |
Correct |
544 ms |
94376 KB |
Output is correct |
55 |
Correct |
26 ms |
54484 KB |
Output is correct |
56 |
Correct |
594 ms |
95344 KB |
Output is correct |
57 |
Correct |
656 ms |
94596 KB |
Output is correct |
58 |
Correct |
602 ms |
95600 KB |
Output is correct |
59 |
Correct |
564 ms |
95788 KB |
Output is correct |
60 |
Correct |
649 ms |
95000 KB |
Output is correct |
61 |
Correct |
622 ms |
96040 KB |
Output is correct |
62 |
Correct |
580 ms |
96000 KB |
Output is correct |
63 |
Correct |
612 ms |
95956 KB |
Output is correct |
64 |
Correct |
601 ms |
96080 KB |
Output is correct |
65 |
Correct |
574 ms |
95472 KB |
Output is correct |
66 |
Correct |
627 ms |
95500 KB |
Output is correct |
67 |
Correct |
609 ms |
95952 KB |
Output is correct |
68 |
Correct |
553 ms |
94744 KB |
Output is correct |
69 |
Correct |
592 ms |
96308 KB |
Output is correct |
70 |
Correct |
560 ms |
94940 KB |
Output is correct |
71 |
Correct |
582 ms |
94128 KB |
Output is correct |
72 |
Correct |
547 ms |
94508 KB |
Output is correct |
73 |
Correct |
603 ms |
95560 KB |
Output is correct |
74 |
Correct |
603 ms |
95652 KB |
Output is correct |
75 |
Correct |
615 ms |
95800 KB |
Output is correct |
76 |
Correct |
592 ms |
96348 KB |
Output is correct |
77 |
Correct |
25 ms |
54476 KB |
Output is correct |