#include <bits/stdc++.h>
using namespace std;
using ii = pair<int, int>;
using ll = long long;
#define mp make_pair
#define pb push_back
#define eb emplace_back
int N, T, Q, _idx, M[100005], min_width[100005], anc[25][100005];
ll dep[100005];
vector<int> idx[100005];
vector<ii> V[100005];
ll f(int u, int k) {
int v = u;
for (int j = 20; j >= 0; j--) {
if (k & (1 << j)) {
v = anc[j][v];
}
}
assert(v);
return dep[u] - dep[v];
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> N >> T;
for (int i = 1; i < N; i++) {
cin >> M[i];
min_width[i] = 2e9;
for (int j = 0, a, b; j < M[i]; j++) {
cin >> a >> b;
min_width[i] = min(min_width[i], b - a);
V[i].eb(a, b);
idx[i].pb(++_idx);
}
sort(V[i].begin(), V[i].end());
for (int j = (int)V[i].size() - 1, sf = (int)2e9; j >= 0; j--) {
sf = min(sf, V[i][j].second);
V[i][j].second = sf;
}
}
for (int i = N - 2; i >= 1; i--) {
for (int j = 0; j < M[i]; j++) {
assert(!V[i + 1].empty());
int tmp = lower_bound(V[i + 1].begin(), V[i + 1].end(), mp(V[i][j].second, -1)) - V[i + 1].begin();
ii potential_to = mp(V[i + 1][0].second + T - V[i][j].second, idx[i + 1][0]);
if (tmp < (int)V[i + 1].size()) {
potential_to = min(potential_to, mp(V[i + 1][tmp].second - V[i][j].second, idx[i + 1][tmp]));
}
anc[0][idx[i][j]] = potential_to.second;
for (int k = 1; k <= 20; k++) {
if (anc[k - 1][idx[i][j]] == 0) {
break;
}
anc[k][idx[i][j]] = anc[k - 1][anc[k - 1][idx[i][j]]];
}
dep[idx[i][j]] = dep[potential_to.second] + potential_to.first;
}
}
for (int i = 1; i < N; i++) {
int mn = *min_element(idx[i].begin(), idx[i].end());
sort(idx[i].begin(), idx[i].end(), [&](const auto &lhs, const auto &rhs) {
return V[i][lhs - mn].second < V[i][rhs - mn].second;
});
vector<ii> copy_V(V[i].begin(), V[i].end());
for (int j = 0; j < M[i]; j++) {
V[i][j] = copy_V[idx[i][j] - mn];
}
}
cin >> Q;
for (int L, R; Q--; ) {
cin >> L >> R;
if (L + 1 == R) {
cout << min_width[L] << '\n';
continue;
}
int lo = 0, hi = M[L] - 1;
//~ while (hi - lo >= 3) {
//~ int m1 = lo + (hi - lo) / 3;
//~ int m2 = hi - (hi - lo) / 3;
//~ ll f1 = f(idx[L][m1], R - L - 1) + V[L][m1].second - V[L][m1].first;
//~ ll f2 = f(idx[L][m2], R - L - 1) + V[L][m2].second - V[L][m2].first;
//~ if (f1 < f2) {
//~ hi = m2;
//~ } else {
//~ lo = m1;
//~ }
//~ }
ll ans = (ll)1e18;
for (int i = lo; i <= hi; i++) {
ans = min(ans, f(idx[L][i], R - L - 1) + V[L][i].second - V[L][i].first);
}
cout << ans << '\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
44 ms |
9764 KB |
Output is correct |
3 |
Correct |
57 ms |
13992 KB |
Output is correct |
4 |
Correct |
73 ms |
14860 KB |
Output is correct |
5 |
Correct |
58 ms |
13904 KB |
Output is correct |
6 |
Correct |
75 ms |
14892 KB |
Output is correct |
7 |
Correct |
86 ms |
14908 KB |
Output is correct |
8 |
Correct |
57 ms |
13904 KB |
Output is correct |
9 |
Correct |
74 ms |
14712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
44 ms |
9764 KB |
Output is correct |
3 |
Correct |
57 ms |
13992 KB |
Output is correct |
4 |
Correct |
73 ms |
14860 KB |
Output is correct |
5 |
Correct |
58 ms |
13904 KB |
Output is correct |
6 |
Correct |
75 ms |
14892 KB |
Output is correct |
7 |
Correct |
86 ms |
14908 KB |
Output is correct |
8 |
Correct |
57 ms |
13904 KB |
Output is correct |
9 |
Correct |
74 ms |
14712 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
101 ms |
14672 KB |
Output is correct |
12 |
Correct |
100 ms |
14168 KB |
Output is correct |
13 |
Correct |
96 ms |
14388 KB |
Output is correct |
14 |
Correct |
95 ms |
14360 KB |
Output is correct |
15 |
Correct |
97 ms |
14676 KB |
Output is correct |
16 |
Correct |
52 ms |
9580 KB |
Output is correct |
17 |
Correct |
76 ms |
16244 KB |
Output is correct |
18 |
Correct |
88 ms |
15444 KB |
Output is correct |
19 |
Correct |
82 ms |
16084 KB |
Output is correct |
20 |
Correct |
91 ms |
14744 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
44 ms |
9764 KB |
Output is correct |
3 |
Correct |
57 ms |
13992 KB |
Output is correct |
4 |
Correct |
73 ms |
14860 KB |
Output is correct |
5 |
Correct |
58 ms |
13904 KB |
Output is correct |
6 |
Correct |
75 ms |
14892 KB |
Output is correct |
7 |
Correct |
86 ms |
14908 KB |
Output is correct |
8 |
Correct |
57 ms |
13904 KB |
Output is correct |
9 |
Correct |
74 ms |
14712 KB |
Output is correct |
10 |
Correct |
132 ms |
21584 KB |
Output is correct |
11 |
Correct |
202 ms |
24292 KB |
Output is correct |
12 |
Correct |
219 ms |
24312 KB |
Output is correct |
13 |
Correct |
171 ms |
23240 KB |
Output is correct |
14 |
Correct |
240 ms |
24364 KB |
Output is correct |
15 |
Correct |
244 ms |
24404 KB |
Output is correct |
16 |
Correct |
137 ms |
21488 KB |
Output is correct |
17 |
Correct |
213 ms |
24176 KB |
Output is correct |
18 |
Correct |
80 ms |
20560 KB |
Output is correct |
19 |
Correct |
78 ms |
20304 KB |
Output is correct |
20 |
Correct |
84 ms |
20560 KB |
Output is correct |
21 |
Correct |
82 ms |
20372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
44 ms |
9764 KB |
Output is correct |
3 |
Correct |
57 ms |
13992 KB |
Output is correct |
4 |
Correct |
73 ms |
14860 KB |
Output is correct |
5 |
Correct |
58 ms |
13904 KB |
Output is correct |
6 |
Correct |
75 ms |
14892 KB |
Output is correct |
7 |
Correct |
86 ms |
14908 KB |
Output is correct |
8 |
Correct |
57 ms |
13904 KB |
Output is correct |
9 |
Correct |
74 ms |
14712 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
101 ms |
14672 KB |
Output is correct |
12 |
Correct |
100 ms |
14168 KB |
Output is correct |
13 |
Correct |
96 ms |
14388 KB |
Output is correct |
14 |
Correct |
95 ms |
14360 KB |
Output is correct |
15 |
Correct |
97 ms |
14676 KB |
Output is correct |
16 |
Correct |
52 ms |
9580 KB |
Output is correct |
17 |
Correct |
76 ms |
16244 KB |
Output is correct |
18 |
Correct |
88 ms |
15444 KB |
Output is correct |
19 |
Correct |
82 ms |
16084 KB |
Output is correct |
20 |
Correct |
91 ms |
14744 KB |
Output is correct |
21 |
Correct |
132 ms |
21584 KB |
Output is correct |
22 |
Correct |
202 ms |
24292 KB |
Output is correct |
23 |
Correct |
219 ms |
24312 KB |
Output is correct |
24 |
Correct |
171 ms |
23240 KB |
Output is correct |
25 |
Correct |
240 ms |
24364 KB |
Output is correct |
26 |
Correct |
244 ms |
24404 KB |
Output is correct |
27 |
Correct |
137 ms |
21488 KB |
Output is correct |
28 |
Correct |
213 ms |
24176 KB |
Output is correct |
29 |
Correct |
80 ms |
20560 KB |
Output is correct |
30 |
Correct |
78 ms |
20304 KB |
Output is correct |
31 |
Correct |
84 ms |
20560 KB |
Output is correct |
32 |
Correct |
82 ms |
20372 KB |
Output is correct |
33 |
Correct |
181 ms |
19540 KB |
Output is correct |
34 |
Correct |
217 ms |
19484 KB |
Output is correct |
35 |
Correct |
173 ms |
19792 KB |
Output is correct |
36 |
Correct |
186 ms |
19108 KB |
Output is correct |
37 |
Correct |
160 ms |
20220 KB |
Output is correct |
38 |
Correct |
128 ms |
22096 KB |
Output is correct |
39 |
Correct |
177 ms |
24496 KB |
Output is correct |
40 |
Correct |
162 ms |
18780 KB |
Output is correct |
41 |
Correct |
144 ms |
20332 KB |
Output is correct |
42 |
Correct |
201 ms |
22608 KB |
Output is correct |
43 |
Correct |
153 ms |
20304 KB |
Output is correct |
44 |
Correct |
190 ms |
22136 KB |
Output is correct |
45 |
Correct |
75 ms |
18768 KB |
Output is correct |
46 |
Correct |
69 ms |
16912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
7000 KB |
Output is correct |
2 |
Correct |
2 ms |
8792 KB |
Output is correct |
3 |
Correct |
532 ms |
14036 KB |
Output is correct |
4 |
Correct |
539 ms |
14400 KB |
Output is correct |
5 |
Correct |
560 ms |
14044 KB |
Output is correct |
6 |
Correct |
521 ms |
13652 KB |
Output is correct |
7 |
Correct |
546 ms |
13648 KB |
Output is correct |
8 |
Correct |
551 ms |
14260 KB |
Output is correct |
9 |
Correct |
514 ms |
14492 KB |
Output is correct |
10 |
Correct |
42 ms |
9032 KB |
Output is correct |
11 |
Correct |
82 ms |
21708 KB |
Output is correct |
12 |
Correct |
77 ms |
19540 KB |
Output is correct |
13 |
Correct |
79 ms |
16720 KB |
Output is correct |
14 |
Correct |
59 ms |
17748 KB |
Output is correct |
15 |
Correct |
68 ms |
17828 KB |
Output is correct |
16 |
Correct |
57 ms |
16996 KB |
Output is correct |
17 |
Correct |
64 ms |
17488 KB |
Output is correct |
18 |
Correct |
146 ms |
14240 KB |
Output is correct |
19 |
Correct |
427 ms |
14152 KB |
Output is correct |
20 |
Correct |
329 ms |
20892 KB |
Output is correct |
21 |
Correct |
281 ms |
19144 KB |
Output is correct |
22 |
Correct |
433 ms |
16980 KB |
Output is correct |
23 |
Correct |
331 ms |
18096 KB |
Output is correct |
24 |
Execution timed out |
2013 ms |
14908 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
8792 KB |
Output is correct |
2 |
Correct |
44 ms |
9764 KB |
Output is correct |
3 |
Correct |
57 ms |
13992 KB |
Output is correct |
4 |
Correct |
73 ms |
14860 KB |
Output is correct |
5 |
Correct |
58 ms |
13904 KB |
Output is correct |
6 |
Correct |
75 ms |
14892 KB |
Output is correct |
7 |
Correct |
86 ms |
14908 KB |
Output is correct |
8 |
Correct |
57 ms |
13904 KB |
Output is correct |
9 |
Correct |
74 ms |
14712 KB |
Output is correct |
10 |
Correct |
2 ms |
6744 KB |
Output is correct |
11 |
Correct |
101 ms |
14672 KB |
Output is correct |
12 |
Correct |
100 ms |
14168 KB |
Output is correct |
13 |
Correct |
96 ms |
14388 KB |
Output is correct |
14 |
Correct |
95 ms |
14360 KB |
Output is correct |
15 |
Correct |
97 ms |
14676 KB |
Output is correct |
16 |
Correct |
52 ms |
9580 KB |
Output is correct |
17 |
Correct |
76 ms |
16244 KB |
Output is correct |
18 |
Correct |
88 ms |
15444 KB |
Output is correct |
19 |
Correct |
82 ms |
16084 KB |
Output is correct |
20 |
Correct |
91 ms |
14744 KB |
Output is correct |
21 |
Correct |
132 ms |
21584 KB |
Output is correct |
22 |
Correct |
202 ms |
24292 KB |
Output is correct |
23 |
Correct |
219 ms |
24312 KB |
Output is correct |
24 |
Correct |
171 ms |
23240 KB |
Output is correct |
25 |
Correct |
240 ms |
24364 KB |
Output is correct |
26 |
Correct |
244 ms |
24404 KB |
Output is correct |
27 |
Correct |
137 ms |
21488 KB |
Output is correct |
28 |
Correct |
213 ms |
24176 KB |
Output is correct |
29 |
Correct |
80 ms |
20560 KB |
Output is correct |
30 |
Correct |
78 ms |
20304 KB |
Output is correct |
31 |
Correct |
84 ms |
20560 KB |
Output is correct |
32 |
Correct |
82 ms |
20372 KB |
Output is correct |
33 |
Correct |
181 ms |
19540 KB |
Output is correct |
34 |
Correct |
217 ms |
19484 KB |
Output is correct |
35 |
Correct |
173 ms |
19792 KB |
Output is correct |
36 |
Correct |
186 ms |
19108 KB |
Output is correct |
37 |
Correct |
160 ms |
20220 KB |
Output is correct |
38 |
Correct |
128 ms |
22096 KB |
Output is correct |
39 |
Correct |
177 ms |
24496 KB |
Output is correct |
40 |
Correct |
162 ms |
18780 KB |
Output is correct |
41 |
Correct |
144 ms |
20332 KB |
Output is correct |
42 |
Correct |
201 ms |
22608 KB |
Output is correct |
43 |
Correct |
153 ms |
20304 KB |
Output is correct |
44 |
Correct |
190 ms |
22136 KB |
Output is correct |
45 |
Correct |
75 ms |
18768 KB |
Output is correct |
46 |
Correct |
69 ms |
16912 KB |
Output is correct |
47 |
Correct |
2 ms |
7000 KB |
Output is correct |
48 |
Correct |
2 ms |
8792 KB |
Output is correct |
49 |
Correct |
532 ms |
14036 KB |
Output is correct |
50 |
Correct |
539 ms |
14400 KB |
Output is correct |
51 |
Correct |
560 ms |
14044 KB |
Output is correct |
52 |
Correct |
521 ms |
13652 KB |
Output is correct |
53 |
Correct |
546 ms |
13648 KB |
Output is correct |
54 |
Correct |
551 ms |
14260 KB |
Output is correct |
55 |
Correct |
514 ms |
14492 KB |
Output is correct |
56 |
Correct |
42 ms |
9032 KB |
Output is correct |
57 |
Correct |
82 ms |
21708 KB |
Output is correct |
58 |
Correct |
77 ms |
19540 KB |
Output is correct |
59 |
Correct |
79 ms |
16720 KB |
Output is correct |
60 |
Correct |
59 ms |
17748 KB |
Output is correct |
61 |
Correct |
68 ms |
17828 KB |
Output is correct |
62 |
Correct |
57 ms |
16996 KB |
Output is correct |
63 |
Correct |
64 ms |
17488 KB |
Output is correct |
64 |
Correct |
146 ms |
14240 KB |
Output is correct |
65 |
Correct |
427 ms |
14152 KB |
Output is correct |
66 |
Correct |
329 ms |
20892 KB |
Output is correct |
67 |
Correct |
281 ms |
19144 KB |
Output is correct |
68 |
Correct |
433 ms |
16980 KB |
Output is correct |
69 |
Correct |
331 ms |
18096 KB |
Output is correct |
70 |
Execution timed out |
2013 ms |
14908 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |