#include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
const int K = 800;
using namespace std;
typedef long long ll;
const ll inf = 8e18;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, m = 0;
ll t;
cin >> n >> t;
vector<vector<pair<int, int>>> rngs(n);
vector<int> sz(n), pre(n);
for(int i = 1; i < n; i++){
int tmp;
cin >> tmp;
for(int j = 0; j < tmp; j++){
int a, b;
cin >> a >> b;
rngs[i].emplace_back(a, b);
}
sort(rngs[i].begin(), rngs[i].end());
vector<pair<int, int>> vtmp;
int mini = 2e9;
for(int j = tmp - 1; j >= 0; j--){
auto x = rngs[i][j];
if(x.second < mini){
mini = x.second;
vtmp.push_back(x);
}
}
reverse(vtmp.begin(), vtmp.end());
rngs[i] = vtmp;
sz[i] = (int)rngs[i].size();
m += sz[i];
pre[i] = pre[i - 1] + sz[i];
}
vector<vector<pair<int, ll>>> panc(17, vector<pair<int, ll>>(m + 1)), nanc(17, vector<pair<int, ll>>(m + 1));
vector<pair<int, ll>> lst(m + 1), nxt(m + 1);
for(int i = 2; i < n; i++){
int ptr = 0;
for(int j = 0; j < sz[i - 1]; j++){
int id = pre[i - 2] + j + 1;
while(ptr < sz[i] && rngs[i][ptr].first < rngs[i - 1][j].second) ptr++;
if(ptr < sz[i]) nxt[id] = make_pair(pre[i - 1] + ptr + 1, rngs[i][ptr].first - rngs[i - 1][j].second);
else nxt[id] = make_pair(pre[i - 1] + 1, t + rngs[i][0].first - rngs[i - 1][j].second);
}
ptr = -1;
for(int j = 0; j < sz[i]; j++){
int id = pre[i - 1] + j + 1;
while(ptr < sz[i - 1] - 1 && rngs[i - 1][ptr + 1].second <= rngs[i][j].first) ptr++;
if(ptr >= 0) lst[id] = make_pair(pre[i - 2] + ptr + 1, rngs[i][j].first - rngs[i - 1][ptr].second);
else lst[id] = make_pair(pre[i - 1], t + rngs[i][j].first - rngs[i - 1][sz[i - 1] - 1].second);
}
}
for(int i = 1; i < n; i++){
for(int j = 0; j < sz[i]; j++){
int id = pre[i - 1] + j + 1;
nanc[0][id] = make_pair(id, rngs[i][j].second - rngs[i][j].first);
panc[0][id] = nanc[0][id];
}
}
for(int i = 1; i < 17; i++){
for(int j = 1; j <= m; j++){
nanc[i][j] = make_pair(nanc[i - 1][nxt[nanc[i - 1][j].first].first].first, nanc[i - 1][j].second + nanc[i - 1][nxt[nanc[i - 1][j].first].first].second + nxt[nanc[i - 1][j].first].second);
panc[i][j] = make_pair(panc[i - 1][lst[panc[i - 1][j].first].first].first, panc[i - 1][j].second + panc[i - 1][lst[panc[i - 1][j].first].first].second + lst[panc[i - 1][j].first].second);
}
}
auto nquery = [&](int pos, int kk, int k2){
ll mini = inf;
for(int times = 0; times < sz[pos]; times++){
int id = pre[pos - 1] + times + 1;
ll ans = -rngs[pos][times].second + rngs[pos][times].first;
int k = kk;
for(int i = 0; i < 17; i++){
if(k & (1 << i)){
k -= (1 << i);
ans += nanc[i][id].second;
id = nanc[i][id].first;
if(k > 0) ans += nxt[id].second;
id = nxt[id].first;
//cout << id << " " << ans << "\n";
}
}
//cout << ans << "\n";
id = pre[pos - 1] + times + 1;
k = k2;
for(int i = 0; i < 17; i++){
if(k & (1 << i)){
k -= (1 << i);
ans += panc[i][id].second;
id = panc[i][id].first;
if(k > 0) ans += lst[id].second;
id = lst[id].first;
}
}
mini = min(mini, ans);
}
return mini;
};
vector<vector<pair<int, int>>> st(17, vector<pair<int, int>>(n));
for(int j = 1; j < n; j++){
st[0][j] = make_pair(sz[j], j);
}
for(int i = 1; i < 17; i++) for(int j = 1; j < n; j++) st[i][j] = min(st[i - 1][j], st[i - 1][min(n - 1, j + (1 << (i - 1)))]);
vector<int> bases(n);
for(int i = 1; i < n; i++){
bases[i] = bases[i - 1];
if(i == (1 << (bases[i] + 1))) bases[i]++;
}
auto query = [&](int l, int r){
r--;
int len = r - l + 1;
return min(st[bases[len]][l], st[bases[len]][r - (1 << bases[len]) + 1]).second;
};
int q;
cin >> q;
vector<ll> anss(q);
vector<vector<pair<int, int>>> queries(n + 1);
for(int i = 0; i < q; i++){
int l, r;
cin >> l >> r;
if(r - l > K){
int best = query(l, r);
anss[i] = nquery(best, r - best, best - l + 1);
//cout << best << "\n";
}
else{
queries[l].emplace_back(r, i);
}
}
ll mini = inf;
for(int i = 1; i < n; i++){
if(queries[i].empty()) continue;
sort(queries[i].begin(), queries[i].end());
int szz = queries[i].size();
int ptr = 0;
vector<ll> ans(sz[i]);
vector<int> id(sz[i]);
for(int j = 0; j < sz[i]; j++) id[j] = pre[i - 1] + j + 1;
for(int j = i; j < min(n, i + K); j++){
mini = inf;
for(int k = 0; k < sz[i]; k++){
ans[k] += nanc[0][id[k]].second;
mini = min(mini, ans[k]);
}
while(ptr < szz && queries[i][ptr].first - 1 == j){
anss[queries[i][ptr].second] = mini;
ptr++;
}
for(int k = 0; k < sz[i]; k++){
ans[k] += nxt[id[k]].second;
id[k] = nxt[id[k]].first;
}
}
}
for(int i = 0; i < q; i++) cout << anss[i] << "\n";
//for(int i = 1; i <= m; i++) cout << nanc[1][i].first << " " << nanc[1][i].second << '\n';
}
// g++ -std=c++17 -Wall -Wextra -Wshadow -fsanitize=undefined -fsanitize=address -o run escape.cpp
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
53 ms |
10676 KB |
Output is correct |
3 |
Correct |
64 ms |
8684 KB |
Output is correct |
4 |
Correct |
88 ms |
12104 KB |
Output is correct |
5 |
Correct |
71 ms |
10312 KB |
Output is correct |
6 |
Correct |
111 ms |
10824 KB |
Output is correct |
7 |
Correct |
95 ms |
11076 KB |
Output is correct |
8 |
Correct |
73 ms |
8828 KB |
Output is correct |
9 |
Correct |
103 ms |
12360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
53 ms |
10676 KB |
Output is correct |
3 |
Correct |
64 ms |
8684 KB |
Output is correct |
4 |
Correct |
88 ms |
12104 KB |
Output is correct |
5 |
Correct |
71 ms |
10312 KB |
Output is correct |
6 |
Correct |
111 ms |
10824 KB |
Output is correct |
7 |
Correct |
95 ms |
11076 KB |
Output is correct |
8 |
Correct |
73 ms |
8828 KB |
Output is correct |
9 |
Correct |
103 ms |
12360 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
79 ms |
12624 KB |
Output is correct |
12 |
Correct |
78 ms |
12616 KB |
Output is correct |
13 |
Correct |
80 ms |
12516 KB |
Output is correct |
14 |
Correct |
93 ms |
12872 KB |
Output is correct |
15 |
Correct |
91 ms |
12436 KB |
Output is correct |
16 |
Correct |
47 ms |
10684 KB |
Output is correct |
17 |
Correct |
76 ms |
10824 KB |
Output is correct |
18 |
Correct |
70 ms |
12860 KB |
Output is correct |
19 |
Correct |
88 ms |
11664 KB |
Output is correct |
20 |
Correct |
68 ms |
12616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
53 ms |
10676 KB |
Output is correct |
3 |
Correct |
64 ms |
8684 KB |
Output is correct |
4 |
Correct |
88 ms |
12104 KB |
Output is correct |
5 |
Correct |
71 ms |
10312 KB |
Output is correct |
6 |
Correct |
111 ms |
10824 KB |
Output is correct |
7 |
Correct |
95 ms |
11076 KB |
Output is correct |
8 |
Correct |
73 ms |
8828 KB |
Output is correct |
9 |
Correct |
103 ms |
12360 KB |
Output is correct |
10 |
Correct |
344 ms |
68980 KB |
Output is correct |
11 |
Correct |
560 ms |
86316 KB |
Output is correct |
12 |
Correct |
602 ms |
86252 KB |
Output is correct |
13 |
Correct |
399 ms |
87632 KB |
Output is correct |
14 |
Correct |
458 ms |
86996 KB |
Output is correct |
15 |
Correct |
485 ms |
86316 KB |
Output is correct |
16 |
Correct |
368 ms |
68216 KB |
Output is correct |
17 |
Correct |
478 ms |
87576 KB |
Output is correct |
18 |
Correct |
220 ms |
73672 KB |
Output is correct |
19 |
Correct |
192 ms |
73160 KB |
Output is correct |
20 |
Correct |
225 ms |
75464 KB |
Output is correct |
21 |
Correct |
227 ms |
75176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
53 ms |
10676 KB |
Output is correct |
3 |
Correct |
64 ms |
8684 KB |
Output is correct |
4 |
Correct |
88 ms |
12104 KB |
Output is correct |
5 |
Correct |
71 ms |
10312 KB |
Output is correct |
6 |
Correct |
111 ms |
10824 KB |
Output is correct |
7 |
Correct |
95 ms |
11076 KB |
Output is correct |
8 |
Correct |
73 ms |
8828 KB |
Output is correct |
9 |
Correct |
103 ms |
12360 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
79 ms |
12624 KB |
Output is correct |
12 |
Correct |
78 ms |
12616 KB |
Output is correct |
13 |
Correct |
80 ms |
12516 KB |
Output is correct |
14 |
Correct |
93 ms |
12872 KB |
Output is correct |
15 |
Correct |
91 ms |
12436 KB |
Output is correct |
16 |
Correct |
47 ms |
10684 KB |
Output is correct |
17 |
Correct |
76 ms |
10824 KB |
Output is correct |
18 |
Correct |
70 ms |
12860 KB |
Output is correct |
19 |
Correct |
88 ms |
11664 KB |
Output is correct |
20 |
Correct |
68 ms |
12616 KB |
Output is correct |
21 |
Correct |
344 ms |
68980 KB |
Output is correct |
22 |
Correct |
560 ms |
86316 KB |
Output is correct |
23 |
Correct |
602 ms |
86252 KB |
Output is correct |
24 |
Correct |
399 ms |
87632 KB |
Output is correct |
25 |
Correct |
458 ms |
86996 KB |
Output is correct |
26 |
Correct |
485 ms |
86316 KB |
Output is correct |
27 |
Correct |
368 ms |
68216 KB |
Output is correct |
28 |
Correct |
478 ms |
87576 KB |
Output is correct |
29 |
Correct |
220 ms |
73672 KB |
Output is correct |
30 |
Correct |
192 ms |
73160 KB |
Output is correct |
31 |
Correct |
225 ms |
75464 KB |
Output is correct |
32 |
Correct |
227 ms |
75176 KB |
Output is correct |
33 |
Correct |
595 ms |
70456 KB |
Output is correct |
34 |
Correct |
552 ms |
68964 KB |
Output is correct |
35 |
Correct |
525 ms |
63944 KB |
Output is correct |
36 |
Correct |
518 ms |
62956 KB |
Output is correct |
37 |
Correct |
451 ms |
67148 KB |
Output is correct |
38 |
Correct |
366 ms |
61576 KB |
Output is correct |
39 |
Correct |
421 ms |
81892 KB |
Output is correct |
40 |
Correct |
382 ms |
50172 KB |
Output is correct |
41 |
Correct |
364 ms |
56264 KB |
Output is correct |
42 |
Correct |
507 ms |
73208 KB |
Output is correct |
43 |
Correct |
338 ms |
55560 KB |
Output is correct |
44 |
Correct |
381 ms |
57336 KB |
Output is correct |
45 |
Correct |
186 ms |
68564 KB |
Output is correct |
46 |
Correct |
171 ms |
60360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
106 ms |
56524 KB |
Output is correct |
4 |
Correct |
111 ms |
55240 KB |
Output is correct |
5 |
Correct |
105 ms |
55240 KB |
Output is correct |
6 |
Correct |
104 ms |
54952 KB |
Output is correct |
7 |
Correct |
111 ms |
55128 KB |
Output is correct |
8 |
Correct |
76 ms |
30000 KB |
Output is correct |
9 |
Correct |
113 ms |
55496 KB |
Output is correct |
10 |
Correct |
57 ms |
30108 KB |
Output is correct |
11 |
Correct |
185 ms |
73672 KB |
Output is correct |
12 |
Correct |
208 ms |
66760 KB |
Output is correct |
13 |
Correct |
174 ms |
60376 KB |
Output is correct |
14 |
Correct |
121 ms |
58056 KB |
Output is correct |
15 |
Correct |
155 ms |
58240 KB |
Output is correct |
16 |
Correct |
123 ms |
58200 KB |
Output is correct |
17 |
Correct |
136 ms |
58964 KB |
Output is correct |
18 |
Correct |
174 ms |
56588 KB |
Output is correct |
19 |
Correct |
113 ms |
54984 KB |
Output is correct |
20 |
Correct |
91 ms |
69576 KB |
Output is correct |
21 |
Correct |
124 ms |
68296 KB |
Output is correct |
22 |
Correct |
91 ms |
61896 KB |
Output is correct |
23 |
Correct |
156 ms |
61160 KB |
Output is correct |
24 |
Correct |
153 ms |
62896 KB |
Output is correct |
25 |
Correct |
98 ms |
61800 KB |
Output is correct |
26 |
Correct |
156 ms |
61212 KB |
Output is correct |
27 |
Correct |
179 ms |
73180 KB |
Output is correct |
28 |
Correct |
212 ms |
74440 KB |
Output is correct |
29 |
Correct |
205 ms |
73572 KB |
Output is correct |
30 |
Correct |
99 ms |
11456 KB |
Output is correct |
31 |
Correct |
40 ms |
4716 KB |
Output is correct |
32 |
Correct |
96 ms |
16516 KB |
Output is correct |
33 |
Correct |
44 ms |
7104 KB |
Output is correct |
34 |
Correct |
49 ms |
11692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
53 ms |
10676 KB |
Output is correct |
3 |
Correct |
64 ms |
8684 KB |
Output is correct |
4 |
Correct |
88 ms |
12104 KB |
Output is correct |
5 |
Correct |
71 ms |
10312 KB |
Output is correct |
6 |
Correct |
111 ms |
10824 KB |
Output is correct |
7 |
Correct |
95 ms |
11076 KB |
Output is correct |
8 |
Correct |
73 ms |
8828 KB |
Output is correct |
9 |
Correct |
103 ms |
12360 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
79 ms |
12624 KB |
Output is correct |
12 |
Correct |
78 ms |
12616 KB |
Output is correct |
13 |
Correct |
80 ms |
12516 KB |
Output is correct |
14 |
Correct |
93 ms |
12872 KB |
Output is correct |
15 |
Correct |
91 ms |
12436 KB |
Output is correct |
16 |
Correct |
47 ms |
10684 KB |
Output is correct |
17 |
Correct |
76 ms |
10824 KB |
Output is correct |
18 |
Correct |
70 ms |
12860 KB |
Output is correct |
19 |
Correct |
88 ms |
11664 KB |
Output is correct |
20 |
Correct |
68 ms |
12616 KB |
Output is correct |
21 |
Correct |
344 ms |
68980 KB |
Output is correct |
22 |
Correct |
560 ms |
86316 KB |
Output is correct |
23 |
Correct |
602 ms |
86252 KB |
Output is correct |
24 |
Correct |
399 ms |
87632 KB |
Output is correct |
25 |
Correct |
458 ms |
86996 KB |
Output is correct |
26 |
Correct |
485 ms |
86316 KB |
Output is correct |
27 |
Correct |
368 ms |
68216 KB |
Output is correct |
28 |
Correct |
478 ms |
87576 KB |
Output is correct |
29 |
Correct |
220 ms |
73672 KB |
Output is correct |
30 |
Correct |
192 ms |
73160 KB |
Output is correct |
31 |
Correct |
225 ms |
75464 KB |
Output is correct |
32 |
Correct |
227 ms |
75176 KB |
Output is correct |
33 |
Correct |
595 ms |
70456 KB |
Output is correct |
34 |
Correct |
552 ms |
68964 KB |
Output is correct |
35 |
Correct |
525 ms |
63944 KB |
Output is correct |
36 |
Correct |
518 ms |
62956 KB |
Output is correct |
37 |
Correct |
451 ms |
67148 KB |
Output is correct |
38 |
Correct |
366 ms |
61576 KB |
Output is correct |
39 |
Correct |
421 ms |
81892 KB |
Output is correct |
40 |
Correct |
382 ms |
50172 KB |
Output is correct |
41 |
Correct |
364 ms |
56264 KB |
Output is correct |
42 |
Correct |
507 ms |
73208 KB |
Output is correct |
43 |
Correct |
338 ms |
55560 KB |
Output is correct |
44 |
Correct |
381 ms |
57336 KB |
Output is correct |
45 |
Correct |
186 ms |
68564 KB |
Output is correct |
46 |
Correct |
171 ms |
60360 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
106 ms |
56524 KB |
Output is correct |
50 |
Correct |
111 ms |
55240 KB |
Output is correct |
51 |
Correct |
105 ms |
55240 KB |
Output is correct |
52 |
Correct |
104 ms |
54952 KB |
Output is correct |
53 |
Correct |
111 ms |
55128 KB |
Output is correct |
54 |
Correct |
76 ms |
30000 KB |
Output is correct |
55 |
Correct |
113 ms |
55496 KB |
Output is correct |
56 |
Correct |
57 ms |
30108 KB |
Output is correct |
57 |
Correct |
185 ms |
73672 KB |
Output is correct |
58 |
Correct |
208 ms |
66760 KB |
Output is correct |
59 |
Correct |
174 ms |
60376 KB |
Output is correct |
60 |
Correct |
121 ms |
58056 KB |
Output is correct |
61 |
Correct |
155 ms |
58240 KB |
Output is correct |
62 |
Correct |
123 ms |
58200 KB |
Output is correct |
63 |
Correct |
136 ms |
58964 KB |
Output is correct |
64 |
Correct |
174 ms |
56588 KB |
Output is correct |
65 |
Correct |
113 ms |
54984 KB |
Output is correct |
66 |
Correct |
91 ms |
69576 KB |
Output is correct |
67 |
Correct |
124 ms |
68296 KB |
Output is correct |
68 |
Correct |
91 ms |
61896 KB |
Output is correct |
69 |
Correct |
156 ms |
61160 KB |
Output is correct |
70 |
Correct |
153 ms |
62896 KB |
Output is correct |
71 |
Correct |
98 ms |
61800 KB |
Output is correct |
72 |
Correct |
156 ms |
61212 KB |
Output is correct |
73 |
Correct |
179 ms |
73180 KB |
Output is correct |
74 |
Correct |
212 ms |
74440 KB |
Output is correct |
75 |
Correct |
205 ms |
73572 KB |
Output is correct |
76 |
Correct |
99 ms |
11456 KB |
Output is correct |
77 |
Correct |
40 ms |
4716 KB |
Output is correct |
78 |
Correct |
96 ms |
16516 KB |
Output is correct |
79 |
Correct |
44 ms |
7104 KB |
Output is correct |
80 |
Correct |
49 ms |
11692 KB |
Output is correct |
81 |
Correct |
196 ms |
68144 KB |
Output is correct |
82 |
Correct |
198 ms |
68148 KB |
Output is correct |
83 |
Correct |
203 ms |
68144 KB |
Output is correct |
84 |
Correct |
193 ms |
67952 KB |
Output is correct |
85 |
Correct |
201 ms |
69368 KB |
Output is correct |
86 |
Correct |
141 ms |
38976 KB |
Output is correct |
87 |
Correct |
193 ms |
68828 KB |
Output is correct |
88 |
Correct |
229 ms |
67952 KB |
Output is correct |
89 |
Correct |
223 ms |
67924 KB |
Output is correct |
90 |
Correct |
160 ms |
42040 KB |
Output is correct |
91 |
Correct |
88 ms |
38968 KB |
Output is correct |
92 |
Correct |
254 ms |
72052 KB |
Output is correct |
93 |
Correct |
258 ms |
71912 KB |
Output is correct |
94 |
Correct |
278 ms |
72024 KB |
Output is correct |
95 |
Correct |
335 ms |
71980 KB |
Output is correct |
96 |
Correct |
272 ms |
68652 KB |
Output is correct |
97 |
Correct |
188 ms |
67372 KB |
Output is correct |
98 |
Correct |
176 ms |
80172 KB |
Output is correct |
99 |
Correct |
295 ms |
81708 KB |
Output is correct |
100 |
Correct |
158 ms |
71468 KB |
Output is correct |
101 |
Correct |
281 ms |
73008 KB |
Output is correct |
102 |
Correct |
298 ms |
73800 KB |
Output is correct |
103 |
Correct |
184 ms |
73140 KB |
Output is correct |
104 |
Correct |
277 ms |
71468 KB |
Output is correct |
105 |
Correct |
125 ms |
13124 KB |
Output is correct |
106 |
Correct |
89 ms |
10688 KB |
Output is correct |
107 |
Correct |
169 ms |
23940 KB |
Output is correct |
108 |
Correct |
97 ms |
12504 KB |
Output is correct |
109 |
Correct |
127 ms |
17572 KB |
Output is correct |