#include <bits/stdc++.h>
using namespace std;
#define int long long
const int SQRT = 317;
const int INF = 1e18;
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,T;
cin >> n >> T;
vector<vector<int>> lift(1,vector<int>(17));
vector<vector<int>> liftcost(1,vector<int>(17));
int c = 0;
vector<vector<pair<int,int>>> pairers(n+1);
vector<vector<int>> bestranges(n+1);
vector<pair<int,int>> finalranges(1);
vector<int> finalrangescost(1);
for(int i=1;i<n;i++){
int m;cin>>m;
vector<pair<int,int>> ranges(m);
for(auto&[b,a]:ranges)cin>>a>>b;
sort(ranges.begin(),ranges.end());
int last_start = -1;
for(auto&[b,a]:ranges){
if(a<=last_start)continue;
last_start = a;
int idx=++c;
pairers[i].emplace_back(a,idx);
lift.emplace_back(vector<int>(17));
liftcost.emplace_back(vector<int>(17));
liftcost[idx][0]=b-a;
finalranges.emplace_back(a,b);
finalrangescost.emplace_back(0);
}
}
for(int i=1;i<n-1;i++){
for(auto&[a,idx]:pairers[i]){
int b = finalranges[idx].second;
auto iter = lower_bound(pairers[i+1].begin(),pairers[i+1].end(),make_pair(b,0ll));
if(iter==pairers[i+1].end()){
finalrangescost[idx]=T-b+pairers[i+1].front().first;
lift[idx][0]=pairers[i+1].front().second;
} else {
finalrangescost[idx]=(iter->first)-b;
lift[idx][0]=iter->second;
}
liftcost[idx][0]+=finalrangescost[idx];
}
}
for(int bit=1;bit<17;bit++){
for(int i=1;i<=c;i++){
lift[i][bit]=lift[lift[i][bit-1]][bit-1];
liftcost[i][bit]=liftcost[lift[i][bit-1]][bit-1]+liftcost[i][bit-1];
}
}
vector<vector<int>> bestanssqrt(n+1,vector<int>(SQRT+1,INF));
for(int i=1;i<n;i++){
vector<tuple<int,int,int>> options;
for(auto&[a,idx]:pairers[i]){
int currcost = finalranges[idx].second-finalranges[idx].first;
int last_range = idx;
for(int j=i+1;j<=min(n,i+SQRT);j++){
bestanssqrt[i][j-i]=min(bestanssqrt[i][j-i],currcost);
currcost+=finalrangescost[last_range];
last_range = lift[last_range][0];
currcost+=finalranges[last_range].second-finalranges[last_range].first;
}
options.emplace_back(last_range,currcost,idx);
}
options.emplace_back(0,0,0);
sort(options.begin(),options.end());
for(int j=1;j<options.size();j++){
if(get<0>(options[j])!=get<0>(options[j-1]))bestranges[i].emplace_back(get<2>(options[j]));
}
}
auto gett = [&](int x,int target){
for(int bit=0;bit<17;bit++)if(target&(1<<bit))x=lift[x][bit];
return x;
};
auto get = [&](int x,int target){
int ans = -finalrangescost[gett(x,target-1)];
for(int bit=0;bit<17;bit++)if(target&(1<<bit)){
ans+=liftcost[x][bit];
x=lift[x][bit];
}
return ans;
};
int q;
cin >> q;
for(int i=1;i<=q;i++){
int l,r;cin>>l>>r;
if(r-l<=SQRT){
cout << bestanssqrt[l][r-l] << '\n';
continue;
}
int ans = INF;
for(int&j:bestranges[l]){
ans=min(ans,get(j,r-l));
}
cout << ans << '\n';
}
}
Compilation message
Main.cpp: In function 'int32_t main()':
Main.cpp:76:16: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::tuple<long long int, long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int j=1;j<options.size();j++){
| ~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3420 KB |
Output is correct |
3 |
Correct |
55 ms |
7508 KB |
Output is correct |
4 |
Correct |
93 ms |
12884 KB |
Output is correct |
5 |
Correct |
64 ms |
11344 KB |
Output is correct |
6 |
Correct |
85 ms |
12824 KB |
Output is correct |
7 |
Correct |
84 ms |
12880 KB |
Output is correct |
8 |
Correct |
57 ms |
9332 KB |
Output is correct |
9 |
Correct |
96 ms |
12788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3420 KB |
Output is correct |
3 |
Correct |
55 ms |
7508 KB |
Output is correct |
4 |
Correct |
93 ms |
12884 KB |
Output is correct |
5 |
Correct |
64 ms |
11344 KB |
Output is correct |
6 |
Correct |
85 ms |
12824 KB |
Output is correct |
7 |
Correct |
84 ms |
12880 KB |
Output is correct |
8 |
Correct |
57 ms |
9332 KB |
Output is correct |
9 |
Correct |
96 ms |
12788 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
50 ms |
7980 KB |
Output is correct |
12 |
Correct |
48 ms |
7764 KB |
Output is correct |
13 |
Correct |
48 ms |
7860 KB |
Output is correct |
14 |
Correct |
50 ms |
7680 KB |
Output is correct |
15 |
Correct |
55 ms |
8788 KB |
Output is correct |
16 |
Correct |
32 ms |
4432 KB |
Output is correct |
17 |
Correct |
68 ms |
10836 KB |
Output is correct |
18 |
Correct |
52 ms |
8980 KB |
Output is correct |
19 |
Correct |
69 ms |
10792 KB |
Output is correct |
20 |
Correct |
53 ms |
8624 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3420 KB |
Output is correct |
3 |
Correct |
55 ms |
7508 KB |
Output is correct |
4 |
Correct |
93 ms |
12884 KB |
Output is correct |
5 |
Correct |
64 ms |
11344 KB |
Output is correct |
6 |
Correct |
85 ms |
12824 KB |
Output is correct |
7 |
Correct |
84 ms |
12880 KB |
Output is correct |
8 |
Correct |
57 ms |
9332 KB |
Output is correct |
9 |
Correct |
96 ms |
12788 KB |
Output is correct |
10 |
Correct |
370 ms |
243928 KB |
Output is correct |
11 |
Correct |
499 ms |
309408 KB |
Output is correct |
12 |
Correct |
514 ms |
309412 KB |
Output is correct |
13 |
Correct |
438 ms |
307872 KB |
Output is correct |
14 |
Correct |
528 ms |
309212 KB |
Output is correct |
15 |
Correct |
493 ms |
310176 KB |
Output is correct |
16 |
Correct |
352 ms |
244388 KB |
Output is correct |
17 |
Correct |
511 ms |
309584 KB |
Output is correct |
18 |
Correct |
315 ms |
274344 KB |
Output is correct |
19 |
Correct |
297 ms |
273828 KB |
Output is correct |
20 |
Correct |
316 ms |
274364 KB |
Output is correct |
21 |
Correct |
311 ms |
274336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3420 KB |
Output is correct |
3 |
Correct |
55 ms |
7508 KB |
Output is correct |
4 |
Correct |
93 ms |
12884 KB |
Output is correct |
5 |
Correct |
64 ms |
11344 KB |
Output is correct |
6 |
Correct |
85 ms |
12824 KB |
Output is correct |
7 |
Correct |
84 ms |
12880 KB |
Output is correct |
8 |
Correct |
57 ms |
9332 KB |
Output is correct |
9 |
Correct |
96 ms |
12788 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
50 ms |
7980 KB |
Output is correct |
12 |
Correct |
48 ms |
7764 KB |
Output is correct |
13 |
Correct |
48 ms |
7860 KB |
Output is correct |
14 |
Correct |
50 ms |
7680 KB |
Output is correct |
15 |
Correct |
55 ms |
8788 KB |
Output is correct |
16 |
Correct |
32 ms |
4432 KB |
Output is correct |
17 |
Correct |
68 ms |
10836 KB |
Output is correct |
18 |
Correct |
52 ms |
8980 KB |
Output is correct |
19 |
Correct |
69 ms |
10792 KB |
Output is correct |
20 |
Correct |
53 ms |
8624 KB |
Output is correct |
21 |
Correct |
370 ms |
243928 KB |
Output is correct |
22 |
Correct |
499 ms |
309408 KB |
Output is correct |
23 |
Correct |
514 ms |
309412 KB |
Output is correct |
24 |
Correct |
438 ms |
307872 KB |
Output is correct |
25 |
Correct |
528 ms |
309212 KB |
Output is correct |
26 |
Correct |
493 ms |
310176 KB |
Output is correct |
27 |
Correct |
352 ms |
244388 KB |
Output is correct |
28 |
Correct |
511 ms |
309584 KB |
Output is correct |
29 |
Correct |
315 ms |
274344 KB |
Output is correct |
30 |
Correct |
297 ms |
273828 KB |
Output is correct |
31 |
Correct |
316 ms |
274364 KB |
Output is correct |
32 |
Correct |
311 ms |
274336 KB |
Output is correct |
33 |
Correct |
756 ms |
100600 KB |
Output is correct |
34 |
Correct |
736 ms |
100664 KB |
Output is correct |
35 |
Correct |
600 ms |
95908 KB |
Output is correct |
36 |
Correct |
629 ms |
95932 KB |
Output is correct |
37 |
Correct |
551 ms |
122760 KB |
Output is correct |
38 |
Correct |
434 ms |
174496 KB |
Output is correct |
39 |
Correct |
571 ms |
221860 KB |
Output is correct |
40 |
Correct |
419 ms |
93856 KB |
Output is correct |
41 |
Correct |
396 ms |
163488 KB |
Output is correct |
42 |
Correct |
519 ms |
218276 KB |
Output is correct |
43 |
Correct |
374 ms |
120996 KB |
Output is correct |
44 |
Correct |
460 ms |
124904 KB |
Output is correct |
45 |
Correct |
421 ms |
196012 KB |
Output is correct |
46 |
Correct |
393 ms |
117116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
209 ms |
36896 KB |
Output is correct |
4 |
Correct |
200 ms |
36256 KB |
Output is correct |
5 |
Correct |
436 ms |
37016 KB |
Output is correct |
6 |
Correct |
189 ms |
36452 KB |
Output is correct |
7 |
Correct |
191 ms |
36608 KB |
Output is correct |
8 |
Correct |
136 ms |
19120 KB |
Output is correct |
9 |
Correct |
207 ms |
36292 KB |
Output is correct |
10 |
Correct |
48 ms |
19624 KB |
Output is correct |
11 |
Correct |
331 ms |
271012 KB |
Output is correct |
12 |
Correct |
384 ms |
195484 KB |
Output is correct |
13 |
Correct |
378 ms |
117408 KB |
Output is correct |
14 |
Correct |
320 ms |
88424 KB |
Output is correct |
15 |
Correct |
309 ms |
87440 KB |
Output is correct |
16 |
Correct |
322 ms |
88740 KB |
Output is correct |
17 |
Correct |
312 ms |
87616 KB |
Output is correct |
18 |
Correct |
341 ms |
42140 KB |
Output is correct |
19 |
Correct |
324 ms |
39208 KB |
Output is correct |
20 |
Correct |
281 ms |
209724 KB |
Output is correct |
21 |
Correct |
309 ms |
210596 KB |
Output is correct |
22 |
Correct |
275 ms |
123672 KB |
Output is correct |
23 |
Correct |
309 ms |
123808 KB |
Output is correct |
24 |
Correct |
263 ms |
124816 KB |
Output is correct |
25 |
Correct |
240 ms |
124508 KB |
Output is correct |
26 |
Correct |
248 ms |
124268 KB |
Output is correct |
27 |
Correct |
340 ms |
274080 KB |
Output is correct |
28 |
Correct |
339 ms |
274340 KB |
Output is correct |
29 |
Correct |
343 ms |
274328 KB |
Output is correct |
30 |
Correct |
84 ms |
22808 KB |
Output is correct |
31 |
Correct |
30 ms |
4104 KB |
Output is correct |
32 |
Correct |
123 ms |
17804 KB |
Output is correct |
33 |
Correct |
36 ms |
5220 KB |
Output is correct |
34 |
Correct |
57 ms |
8580 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3420 KB |
Output is correct |
3 |
Correct |
55 ms |
7508 KB |
Output is correct |
4 |
Correct |
93 ms |
12884 KB |
Output is correct |
5 |
Correct |
64 ms |
11344 KB |
Output is correct |
6 |
Correct |
85 ms |
12824 KB |
Output is correct |
7 |
Correct |
84 ms |
12880 KB |
Output is correct |
8 |
Correct |
57 ms |
9332 KB |
Output is correct |
9 |
Correct |
96 ms |
12788 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
50 ms |
7980 KB |
Output is correct |
12 |
Correct |
48 ms |
7764 KB |
Output is correct |
13 |
Correct |
48 ms |
7860 KB |
Output is correct |
14 |
Correct |
50 ms |
7680 KB |
Output is correct |
15 |
Correct |
55 ms |
8788 KB |
Output is correct |
16 |
Correct |
32 ms |
4432 KB |
Output is correct |
17 |
Correct |
68 ms |
10836 KB |
Output is correct |
18 |
Correct |
52 ms |
8980 KB |
Output is correct |
19 |
Correct |
69 ms |
10792 KB |
Output is correct |
20 |
Correct |
53 ms |
8624 KB |
Output is correct |
21 |
Correct |
370 ms |
243928 KB |
Output is correct |
22 |
Correct |
499 ms |
309408 KB |
Output is correct |
23 |
Correct |
514 ms |
309412 KB |
Output is correct |
24 |
Correct |
438 ms |
307872 KB |
Output is correct |
25 |
Correct |
528 ms |
309212 KB |
Output is correct |
26 |
Correct |
493 ms |
310176 KB |
Output is correct |
27 |
Correct |
352 ms |
244388 KB |
Output is correct |
28 |
Correct |
511 ms |
309584 KB |
Output is correct |
29 |
Correct |
315 ms |
274344 KB |
Output is correct |
30 |
Correct |
297 ms |
273828 KB |
Output is correct |
31 |
Correct |
316 ms |
274364 KB |
Output is correct |
32 |
Correct |
311 ms |
274336 KB |
Output is correct |
33 |
Correct |
756 ms |
100600 KB |
Output is correct |
34 |
Correct |
736 ms |
100664 KB |
Output is correct |
35 |
Correct |
600 ms |
95908 KB |
Output is correct |
36 |
Correct |
629 ms |
95932 KB |
Output is correct |
37 |
Correct |
551 ms |
122760 KB |
Output is correct |
38 |
Correct |
434 ms |
174496 KB |
Output is correct |
39 |
Correct |
571 ms |
221860 KB |
Output is correct |
40 |
Correct |
419 ms |
93856 KB |
Output is correct |
41 |
Correct |
396 ms |
163488 KB |
Output is correct |
42 |
Correct |
519 ms |
218276 KB |
Output is correct |
43 |
Correct |
374 ms |
120996 KB |
Output is correct |
44 |
Correct |
460 ms |
124904 KB |
Output is correct |
45 |
Correct |
421 ms |
196012 KB |
Output is correct |
46 |
Correct |
393 ms |
117116 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
209 ms |
36896 KB |
Output is correct |
50 |
Correct |
200 ms |
36256 KB |
Output is correct |
51 |
Correct |
436 ms |
37016 KB |
Output is correct |
52 |
Correct |
189 ms |
36452 KB |
Output is correct |
53 |
Correct |
191 ms |
36608 KB |
Output is correct |
54 |
Correct |
136 ms |
19120 KB |
Output is correct |
55 |
Correct |
207 ms |
36292 KB |
Output is correct |
56 |
Correct |
48 ms |
19624 KB |
Output is correct |
57 |
Correct |
331 ms |
271012 KB |
Output is correct |
58 |
Correct |
384 ms |
195484 KB |
Output is correct |
59 |
Correct |
378 ms |
117408 KB |
Output is correct |
60 |
Correct |
320 ms |
88424 KB |
Output is correct |
61 |
Correct |
309 ms |
87440 KB |
Output is correct |
62 |
Correct |
322 ms |
88740 KB |
Output is correct |
63 |
Correct |
312 ms |
87616 KB |
Output is correct |
64 |
Correct |
341 ms |
42140 KB |
Output is correct |
65 |
Correct |
324 ms |
39208 KB |
Output is correct |
66 |
Correct |
281 ms |
209724 KB |
Output is correct |
67 |
Correct |
309 ms |
210596 KB |
Output is correct |
68 |
Correct |
275 ms |
123672 KB |
Output is correct |
69 |
Correct |
309 ms |
123808 KB |
Output is correct |
70 |
Correct |
263 ms |
124816 KB |
Output is correct |
71 |
Correct |
240 ms |
124508 KB |
Output is correct |
72 |
Correct |
248 ms |
124268 KB |
Output is correct |
73 |
Correct |
340 ms |
274080 KB |
Output is correct |
74 |
Correct |
339 ms |
274340 KB |
Output is correct |
75 |
Correct |
343 ms |
274328 KB |
Output is correct |
76 |
Correct |
84 ms |
22808 KB |
Output is correct |
77 |
Correct |
30 ms |
4104 KB |
Output is correct |
78 |
Correct |
123 ms |
17804 KB |
Output is correct |
79 |
Correct |
36 ms |
5220 KB |
Output is correct |
80 |
Correct |
57 ms |
8580 KB |
Output is correct |
81 |
Correct |
970 ms |
47020 KB |
Output is correct |
82 |
Correct |
966 ms |
47104 KB |
Output is correct |
83 |
Correct |
1389 ms |
47104 KB |
Output is correct |
84 |
Correct |
1043 ms |
46636 KB |
Output is correct |
85 |
Correct |
1061 ms |
46892 KB |
Output is correct |
86 |
Correct |
224 ms |
27988 KB |
Output is correct |
87 |
Correct |
333 ms |
47104 KB |
Output is correct |
88 |
Correct |
1733 ms |
49372 KB |
Output is correct |
89 |
Correct |
1627 ms |
49616 KB |
Output is correct |
90 |
Correct |
260 ms |
29156 KB |
Output is correct |
91 |
Correct |
80 ms |
23556 KB |
Output is correct |
92 |
Correct |
496 ms |
105380 KB |
Output is correct |
93 |
Correct |
416 ms |
102620 KB |
Output is correct |
94 |
Correct |
448 ms |
102816 KB |
Output is correct |
95 |
Correct |
472 ms |
102492 KB |
Output is correct |
96 |
Correct |
519 ms |
50628 KB |
Output is correct |
97 |
Correct |
415 ms |
47008 KB |
Output is correct |
98 |
Correct |
375 ms |
218504 KB |
Output is correct |
99 |
Correct |
418 ms |
218816 KB |
Output is correct |
100 |
Correct |
332 ms |
132768 KB |
Output is correct |
101 |
Correct |
376 ms |
132772 KB |
Output is correct |
102 |
Correct |
378 ms |
133932 KB |
Output is correct |
103 |
Correct |
325 ms |
133428 KB |
Output is correct |
104 |
Correct |
371 ms |
132996 KB |
Output is correct |
105 |
Correct |
106 ms |
14772 KB |
Output is correct |
106 |
Correct |
57 ms |
7628 KB |
Output is correct |
107 |
Correct |
213 ms |
24276 KB |
Output is correct |
108 |
Correct |
70 ms |
10040 KB |
Output is correct |
109 |
Correct |
101 ms |
13512 KB |
Output is correct |