#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tpl;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 1e5 + 5, Q = 3e5 + 5;
vector<pl> nxt[18][mn], flight[mn], qry[mn];
vector<int> ord[mn];
ll ans[Q], n, T;
ll travel (int node, int flightID, int step) {
ll ans = 0;
for (int i = 0; step; step >>= 1, i++) {
if ((step & 1) == 0) continue;
ll weight; tie(flightID, weight) = nxt[i][node][flightID];
ans += weight, node += (1 << i);
}
return ans;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
// read flights info
cin >> n >> T;
for (int i = 1; i < n; i++) {
int m; cin >> m;
flight[i].resize(m);
for (int j = 0; j < m; j++) {
int a, b; cin >> a >> b;
flight[i][j] = {a, b};
}
sort(all(flight[i]));
}
// resize + setup stuffs
for (int i = 1; i < n; i++) {
ord[i].resize(flight[i].size());
for (int s = 0; s < 18; s++)
nxt[s][i].resize(flight[i].size());
for (int j = 0; j < ord[i].size(); j++) ord[i][j] = j;
sort(all(ord[i]), [&] (int a, int b)
{ return flight[i][a].second > flight[i][b].second; });
}
// build edge
for (int i = 1; i + 1 < n; i++) {
int iter = flight[i + 1].size(), best = INT_MAX, idx = -1;
int soonest = min_element(all(flight[i + 1]), [&] (pii a, pii b) { return a.second < b.second; } ) - flight[i + 1].begin();
for (int u : ord[i]) {
// calculate transition for nxt[i][u]
while (iter - 1 >= 0 && flight[i][u].second <= flight[i + 1][iter - 1].first) {
int cur = flight[i + 1][--iter].second;
if (cur < best) best = cur, idx = iter;
}
if (idx == -1) { // wait overnight and take the `soonest` flight
// cout << "state " << i << " " << u << "\n";
ll link = soonest, weight = T - flight[i][u].second + flight[i + 1][soonest].second;
nxt[0][i][u] = {link, weight};
}
else { // take the `best` flight to fly in the same day
ll link = idx, weight = flight[i + 1][idx].second - flight[i][u].second;
nxt[0][i][u] = {link, weight};
}
int link, weight; tie(link, weight) = nxt[0][i][u];
// cout << "(" << i << ", " << u << ") -> (" << i + 1 << ", " << link << "), weight " << weight << "\n";
}
}
// binary-lifting pre-processing
for (int s = 1; s < 18; s++) {
for (int i = 1; i + (1 << s) < n; i++) {
for (int j = 0; j < flight[i].size(); j++) {
ll link, weight; tie(link, weight) = nxt[s - 1][i][j];
ll nxtLink, nxtWeight; tie(nxtLink, nxtWeight) = nxt[s - 1][i + (1 << (s - 1))][link];
nxt[s][i][j] = {nxtLink, weight + nxtWeight};
}
}
}
// read queries
int q; cin >> q;
for (int i = 0; i < q; i++) {
int l, r; cin >> l >> r;
qry[l].emplace_back(r, i);
}
// process queries
for (int i = 1; i < n; i++) {
if (qry[i].empty()) continue;
sort(all(qry[i]));
function<ll(int,int)> tryFlight = [&] (int flightID, int destination) {
int step = destination - i - 1;
return travel(i, flightID, step) + flight[i][flightID].second - flight[i][flightID].first;
};
for (auto [r, idx] : qry[i]) {
ans[idx] = LLONG_MAX;
for (int u : ord[i]) ans[idx] = min(ans[idx], tryFlight(u, r));
}
// function<void(int,int,int,int)> solve = [&] (int a, int b, int optL, int optR) {
// if (i == 2) {
// cout << "solve {";
// for (int jj = a; jj <= b; jj++) cout << qry[i][jj].second << " ";
// cout << "}, ";
// cout << optL << ".." << optR << "\n";
// }
//
// if (optL == optR) {
// for (int it = a; it <= b; it++) {
// int r, idx; tie(r, idx) = qry[i][it];
// ans[idx] = tryFlight(ord[i][optL], r);
// }
// return;
// }
// int mid = (a + b) >> 1, bestID = -1;
// ll best = LLONG_MAX;
//
// int r, idx; tie(r, idx) = qry[i][mid];
// for (int tryID = optL; tryID <= optR; tryID++) {
// if (i == 2 && idx == 2) {
// cout << "wth " << ord[i][tryID] << " " << tryFlight(ord[i][tryID], r) << "\n";
// cout << "bruh " << i << " " << r << " " << travel(i, ord[i][tryID], r - i - 1) << "\n";
// }
// ll cur = tryFlight(ord[i][tryID], r);
// if (cur < best) best = cur, bestID = tryID;
// }
// ans[idx] = best;
// if (a < mid) solve(a, mid - 1, optL, bestID);
// if (mid < b) solve(mid + 1, b, bestID, optR);
// };
// solve(0, qry[i].size() - 1, 0, ord[i].size() - 1);
// for (pii item : qry[i]) {
// int r, idx; tie(r, idx) = item;
//
// res = (iter == -1 ? LLONG_MAX : travel(i, ord[i][iter], r - i - 1) + flight[i][ord[i][iter]].second - flight[i][ord[i][iter]].first);
// while (iter + 1 < ord[i].size()) {
// int u = ord[i][iter + 1];
// ll tryFinish = travel(i, u, r - i - 1) + flight[i][u].second - flight[i][u].first;
// if (i == 3) cout << "bruh " << idx << " " << travel(i, u, r - i - 1) << " " << r - i - 1 << " " << u << "\n";
// if (tryFinish < res) res = tryFinish, iter++;
// else break;
//// cout << "jump " << i << " " << u << " " << r - i - 1 << "-step " << res << "\n";
// }
// ans[idx] = res;
// }
}
// print output
for (int i = 0; i < q; i++) cout << ans[i] << "\n";
return 0;
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:50:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | for (int j = 0; j < ord[i].size(); j++) ord[i][j] = j;
| ~~^~~~~~~~~~~~~~~
Main.cpp:83:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
83 | for (int j = 0; j < flight[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~~~~
Main.cpp:108:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
108 | for (auto [r, idx] : qry[i]) {
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50000 KB |
Output is correct |
2 |
Correct |
75 ms |
61092 KB |
Output is correct |
3 |
Correct |
83 ms |
63304 KB |
Output is correct |
4 |
Correct |
107 ms |
67400 KB |
Output is correct |
5 |
Correct |
83 ms |
63824 KB |
Output is correct |
6 |
Correct |
100 ms |
67660 KB |
Output is correct |
7 |
Correct |
100 ms |
67836 KB |
Output is correct |
8 |
Correct |
79 ms |
63816 KB |
Output is correct |
9 |
Correct |
117 ms |
67764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50000 KB |
Output is correct |
2 |
Correct |
75 ms |
61092 KB |
Output is correct |
3 |
Correct |
83 ms |
63304 KB |
Output is correct |
4 |
Correct |
107 ms |
67400 KB |
Output is correct |
5 |
Correct |
83 ms |
63824 KB |
Output is correct |
6 |
Correct |
100 ms |
67660 KB |
Output is correct |
7 |
Correct |
100 ms |
67836 KB |
Output is correct |
8 |
Correct |
79 ms |
63816 KB |
Output is correct |
9 |
Correct |
117 ms |
67764 KB |
Output is correct |
10 |
Correct |
20 ms |
49744 KB |
Output is correct |
11 |
Correct |
106 ms |
65404 KB |
Output is correct |
12 |
Correct |
112 ms |
65096 KB |
Output is correct |
13 |
Correct |
113 ms |
65096 KB |
Output is correct |
14 |
Correct |
114 ms |
65260 KB |
Output is correct |
15 |
Correct |
109 ms |
65608 KB |
Output is correct |
16 |
Correct |
82 ms |
61088 KB |
Output is correct |
17 |
Correct |
108 ms |
66632 KB |
Output is correct |
18 |
Correct |
114 ms |
65608 KB |
Output is correct |
19 |
Correct |
122 ms |
67064 KB |
Output is correct |
20 |
Correct |
115 ms |
66120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50000 KB |
Output is correct |
2 |
Correct |
75 ms |
61092 KB |
Output is correct |
3 |
Correct |
83 ms |
63304 KB |
Output is correct |
4 |
Correct |
107 ms |
67400 KB |
Output is correct |
5 |
Correct |
83 ms |
63824 KB |
Output is correct |
6 |
Correct |
100 ms |
67660 KB |
Output is correct |
7 |
Correct |
100 ms |
67836 KB |
Output is correct |
8 |
Correct |
79 ms |
63816 KB |
Output is correct |
9 |
Correct |
117 ms |
67764 KB |
Output is correct |
10 |
Correct |
260 ms |
113480 KB |
Output is correct |
11 |
Correct |
371 ms |
132168 KB |
Output is correct |
12 |
Correct |
360 ms |
132168 KB |
Output is correct |
13 |
Correct |
323 ms |
127820 KB |
Output is correct |
14 |
Correct |
359 ms |
132340 KB |
Output is correct |
15 |
Correct |
361 ms |
132424 KB |
Output is correct |
16 |
Correct |
269 ms |
112968 KB |
Output is correct |
17 |
Correct |
358 ms |
132424 KB |
Output is correct |
18 |
Correct |
301 ms |
114956 KB |
Output is correct |
19 |
Correct |
221 ms |
112456 KB |
Output is correct |
20 |
Correct |
244 ms |
113224 KB |
Output is correct |
21 |
Correct |
242 ms |
113480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50000 KB |
Output is correct |
2 |
Correct |
75 ms |
61092 KB |
Output is correct |
3 |
Correct |
83 ms |
63304 KB |
Output is correct |
4 |
Correct |
107 ms |
67400 KB |
Output is correct |
5 |
Correct |
83 ms |
63824 KB |
Output is correct |
6 |
Correct |
100 ms |
67660 KB |
Output is correct |
7 |
Correct |
100 ms |
67836 KB |
Output is correct |
8 |
Correct |
79 ms |
63816 KB |
Output is correct |
9 |
Correct |
117 ms |
67764 KB |
Output is correct |
10 |
Correct |
20 ms |
49744 KB |
Output is correct |
11 |
Correct |
106 ms |
65404 KB |
Output is correct |
12 |
Correct |
112 ms |
65096 KB |
Output is correct |
13 |
Correct |
113 ms |
65096 KB |
Output is correct |
14 |
Correct |
114 ms |
65260 KB |
Output is correct |
15 |
Correct |
109 ms |
65608 KB |
Output is correct |
16 |
Correct |
82 ms |
61088 KB |
Output is correct |
17 |
Correct |
108 ms |
66632 KB |
Output is correct |
18 |
Correct |
114 ms |
65608 KB |
Output is correct |
19 |
Correct |
122 ms |
67064 KB |
Output is correct |
20 |
Correct |
115 ms |
66120 KB |
Output is correct |
21 |
Correct |
260 ms |
113480 KB |
Output is correct |
22 |
Correct |
371 ms |
132168 KB |
Output is correct |
23 |
Correct |
360 ms |
132168 KB |
Output is correct |
24 |
Correct |
323 ms |
127820 KB |
Output is correct |
25 |
Correct |
359 ms |
132340 KB |
Output is correct |
26 |
Correct |
361 ms |
132424 KB |
Output is correct |
27 |
Correct |
269 ms |
112968 KB |
Output is correct |
28 |
Correct |
358 ms |
132424 KB |
Output is correct |
29 |
Correct |
301 ms |
114956 KB |
Output is correct |
30 |
Correct |
221 ms |
112456 KB |
Output is correct |
31 |
Correct |
244 ms |
113224 KB |
Output is correct |
32 |
Correct |
242 ms |
113480 KB |
Output is correct |
33 |
Correct |
277 ms |
105288 KB |
Output is correct |
34 |
Correct |
272 ms |
105304 KB |
Output is correct |
35 |
Correct |
269 ms |
102088 KB |
Output is correct |
36 |
Correct |
270 ms |
102216 KB |
Output is correct |
37 |
Correct |
272 ms |
105876 KB |
Output is correct |
38 |
Correct |
266 ms |
105436 KB |
Output is correct |
39 |
Correct |
343 ms |
120144 KB |
Output is correct |
40 |
Correct |
223 ms |
95816 KB |
Output is correct |
41 |
Correct |
289 ms |
105040 KB |
Output is correct |
42 |
Correct |
414 ms |
121180 KB |
Output is correct |
43 |
Correct |
298 ms |
106364 KB |
Output is correct |
44 |
Correct |
329 ms |
110152 KB |
Output is correct |
45 |
Correct |
262 ms |
103456 KB |
Output is correct |
46 |
Correct |
191 ms |
93508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
49744 KB |
Output is correct |
2 |
Correct |
35 ms |
49744 KB |
Output is correct |
3 |
Correct |
385 ms |
83236 KB |
Output is correct |
4 |
Correct |
372 ms |
83060 KB |
Output is correct |
5 |
Correct |
364 ms |
83016 KB |
Output is correct |
6 |
Correct |
378 ms |
83280 KB |
Output is correct |
7 |
Correct |
383 ms |
83016 KB |
Output is correct |
8 |
Correct |
393 ms |
83272 KB |
Output is correct |
9 |
Correct |
367 ms |
83272 KB |
Output is correct |
10 |
Execution timed out |
3043 ms |
81092 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
50000 KB |
Output is correct |
2 |
Correct |
75 ms |
61092 KB |
Output is correct |
3 |
Correct |
83 ms |
63304 KB |
Output is correct |
4 |
Correct |
107 ms |
67400 KB |
Output is correct |
5 |
Correct |
83 ms |
63824 KB |
Output is correct |
6 |
Correct |
100 ms |
67660 KB |
Output is correct |
7 |
Correct |
100 ms |
67836 KB |
Output is correct |
8 |
Correct |
79 ms |
63816 KB |
Output is correct |
9 |
Correct |
117 ms |
67764 KB |
Output is correct |
10 |
Correct |
20 ms |
49744 KB |
Output is correct |
11 |
Correct |
106 ms |
65404 KB |
Output is correct |
12 |
Correct |
112 ms |
65096 KB |
Output is correct |
13 |
Correct |
113 ms |
65096 KB |
Output is correct |
14 |
Correct |
114 ms |
65260 KB |
Output is correct |
15 |
Correct |
109 ms |
65608 KB |
Output is correct |
16 |
Correct |
82 ms |
61088 KB |
Output is correct |
17 |
Correct |
108 ms |
66632 KB |
Output is correct |
18 |
Correct |
114 ms |
65608 KB |
Output is correct |
19 |
Correct |
122 ms |
67064 KB |
Output is correct |
20 |
Correct |
115 ms |
66120 KB |
Output is correct |
21 |
Correct |
260 ms |
113480 KB |
Output is correct |
22 |
Correct |
371 ms |
132168 KB |
Output is correct |
23 |
Correct |
360 ms |
132168 KB |
Output is correct |
24 |
Correct |
323 ms |
127820 KB |
Output is correct |
25 |
Correct |
359 ms |
132340 KB |
Output is correct |
26 |
Correct |
361 ms |
132424 KB |
Output is correct |
27 |
Correct |
269 ms |
112968 KB |
Output is correct |
28 |
Correct |
358 ms |
132424 KB |
Output is correct |
29 |
Correct |
301 ms |
114956 KB |
Output is correct |
30 |
Correct |
221 ms |
112456 KB |
Output is correct |
31 |
Correct |
244 ms |
113224 KB |
Output is correct |
32 |
Correct |
242 ms |
113480 KB |
Output is correct |
33 |
Correct |
277 ms |
105288 KB |
Output is correct |
34 |
Correct |
272 ms |
105304 KB |
Output is correct |
35 |
Correct |
269 ms |
102088 KB |
Output is correct |
36 |
Correct |
270 ms |
102216 KB |
Output is correct |
37 |
Correct |
272 ms |
105876 KB |
Output is correct |
38 |
Correct |
266 ms |
105436 KB |
Output is correct |
39 |
Correct |
343 ms |
120144 KB |
Output is correct |
40 |
Correct |
223 ms |
95816 KB |
Output is correct |
41 |
Correct |
289 ms |
105040 KB |
Output is correct |
42 |
Correct |
414 ms |
121180 KB |
Output is correct |
43 |
Correct |
298 ms |
106364 KB |
Output is correct |
44 |
Correct |
329 ms |
110152 KB |
Output is correct |
45 |
Correct |
262 ms |
103456 KB |
Output is correct |
46 |
Correct |
191 ms |
93508 KB |
Output is correct |
47 |
Correct |
30 ms |
49744 KB |
Output is correct |
48 |
Correct |
35 ms |
49744 KB |
Output is correct |
49 |
Correct |
385 ms |
83236 KB |
Output is correct |
50 |
Correct |
372 ms |
83060 KB |
Output is correct |
51 |
Correct |
364 ms |
83016 KB |
Output is correct |
52 |
Correct |
378 ms |
83280 KB |
Output is correct |
53 |
Correct |
383 ms |
83016 KB |
Output is correct |
54 |
Correct |
393 ms |
83272 KB |
Output is correct |
55 |
Correct |
367 ms |
83272 KB |
Output is correct |
56 |
Execution timed out |
3043 ms |
81092 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |