#include<bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
const int L = 17;
using namespace std;
void solve(){
int n, t, r;cin >> n >> t;
set<pair<int, int>> s[n-1];
vector<pair<int, int>> pp[n-1];
for(int i = 0;i < n-1;i++){
cin >> r;
while(r--){
int a, b;cin >> a >> b;
if(s[i].empty() || s[i].rbegin()->first<a || s[i].lower_bound({a,-1})->second > b){
auto p = s[i].insert({a,b}).first;
while(p != s[i].begin() && prev(p)->second >= b){
s[i].erase(prev(p));
}
if(p != prev(s[i].end()) && next(p)->first == p->first){
s[i].erase(next(p));
}
}
}
for(auto [a,b] : s[i]) pp[i].emplace_back(a,b);
}
vector<long long> bl[L][n-1]; vector<int> blt[L][n-1];
for(int l = 0;l < L;l++){
for(int i = 0;i < n-1;i++){
bl[l][i].resize(s[i].size());
blt[l][i].resize(s[i].size());
}
}
vector<int> nxt[n-1];
for(int i = 0;i < n-1;i++){
for(int j = 0;j < pp[i].size();j++){
bl[0][i][j] = pp[i][j].second-pp[i][j].first;
blt[0][i][j] = j;
if(i==n-2) continue;
if(pp[i+1].back().first < pp[i][j].second) nxt[i].push_back(0);
else nxt[i].push_back(lower_bound(pp[i+1].begin(), pp[i+1].end(), pair<int,int>{pp[i][j].second, -1})-pp[i+1].begin());
}
}
for(int l = 1;l < L;l++){
for(int i = 0;i+(1<<l) < n;i++){
for(int j = 0;j < pp[i].size();j++){
int na = pp[i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]].first;
int nb = pp[i+(1<<l-1)-1][blt[l-1][i][j]].second;
if(na < nb){
bl[l][i][j] = bl[l-1][i][j] + t+na-nb + bl[l-1][i+(1<<l-1)][0];
blt[l][i][j] = blt[l-1][i+(1<<l-1)][0];
} else {
bl[l][i][j] = bl[l-1][i][j] + na-nb + bl[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
blt[l][i][j] = blt[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
}
}
}
}
int q;cin >> q;
while(q--){
int l,r;cin >> l >> r;l--;r--;
long long aans = LONG_LONG_MAX;
int ol = l;
for(int j = 0;j < pp[ol].size();j++){
l = ol;
long long ans = pp[l][j].second-pp[l][j].first;
int k = j;l++;
for(int i = L-1;i >= 0;i--){
if(l+(1<<i) > r) continue;
int na = pp[l][nxt[l-1][k]].first;
int nb = pp[l-1][k].second;
if(na < nb){
ans += t+na-nb+bl[i][l][0];
k = blt[i][l][0];
} else {
ans += na-nb+bl[i][l][nxt[l-1][k]];
k = blt[i][l][nxt[l-1][k]];
}
l+=1<<i;
}
aans = min(aans, ans);
}
cout << aans << '\n';
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:35:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int j = 0;j < pp[i].size();j++){
| ~~^~~~~~~~~~~~~~
Main.cpp:45:29: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int j = 0;j < pp[i].size();j++){
| ~~^~~~~~~~~~~~~~
Main.cpp:46:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
46 | int na = pp[i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]].first;
| ~^~
Main.cpp:46:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
46 | int na = pp[i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]].first;
| ~^~
Main.cpp:47:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
47 | int nb = pp[i+(1<<l-1)-1][blt[l-1][i][j]].second;
| ~^~
Main.cpp:49:76: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
49 | bl[l][i][j] = bl[l-1][i][j] + t+na-nb + bl[l-1][i+(1<<l-1)][0];
| ~^~
Main.cpp:50:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
50 | blt[l][i][j] = blt[l-1][i+(1<<l-1)][0];
| ~^~
Main.cpp:52:74: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
52 | bl[l][i][j] = bl[l-1][i][j] + na-nb + bl[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
| ~^~
Main.cpp:52:90: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
52 | bl[l][i][j] = bl[l-1][i][j] + na-nb + bl[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
| ~^~
Main.cpp:53:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | blt[l][i][j] = blt[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
| ~^~
Main.cpp:53:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
53 | blt[l][i][j] = blt[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
| ~^~
Main.cpp:63:25: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int j = 0;j < pp[ol].size();j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
3212 KB |
Output is correct |
3 |
Correct |
51 ms |
6212 KB |
Output is correct |
4 |
Correct |
69 ms |
8272 KB |
Output is correct |
5 |
Correct |
55 ms |
7448 KB |
Output is correct |
6 |
Correct |
72 ms |
8444 KB |
Output is correct |
7 |
Correct |
65 ms |
8528 KB |
Output is correct |
8 |
Correct |
52 ms |
6228 KB |
Output is correct |
9 |
Correct |
78 ms |
8272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
3212 KB |
Output is correct |
3 |
Correct |
51 ms |
6212 KB |
Output is correct |
4 |
Correct |
69 ms |
8272 KB |
Output is correct |
5 |
Correct |
55 ms |
7448 KB |
Output is correct |
6 |
Correct |
72 ms |
8444 KB |
Output is correct |
7 |
Correct |
65 ms |
8528 KB |
Output is correct |
8 |
Correct |
52 ms |
6228 KB |
Output is correct |
9 |
Correct |
78 ms |
8272 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
78 ms |
4832 KB |
Output is correct |
12 |
Correct |
81 ms |
4688 KB |
Output is correct |
13 |
Correct |
77 ms |
4688 KB |
Output is correct |
14 |
Correct |
75 ms |
4692 KB |
Output is correct |
15 |
Correct |
78 ms |
5564 KB |
Output is correct |
16 |
Correct |
36 ms |
3420 KB |
Output is correct |
17 |
Correct |
74 ms |
7072 KB |
Output is correct |
18 |
Correct |
92 ms |
5712 KB |
Output is correct |
19 |
Correct |
73 ms |
7000 KB |
Output is correct |
20 |
Correct |
79 ms |
5548 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
3212 KB |
Output is correct |
3 |
Correct |
51 ms |
6212 KB |
Output is correct |
4 |
Correct |
69 ms |
8272 KB |
Output is correct |
5 |
Correct |
55 ms |
7448 KB |
Output is correct |
6 |
Correct |
72 ms |
8444 KB |
Output is correct |
7 |
Correct |
65 ms |
8528 KB |
Output is correct |
8 |
Correct |
52 ms |
6228 KB |
Output is correct |
9 |
Correct |
78 ms |
8272 KB |
Output is correct |
10 |
Correct |
314 ms |
166444 KB |
Output is correct |
11 |
Correct |
464 ms |
211540 KB |
Output is correct |
12 |
Correct |
475 ms |
211580 KB |
Output is correct |
13 |
Correct |
372 ms |
210236 KB |
Output is correct |
14 |
Correct |
448 ms |
211284 KB |
Output is correct |
15 |
Correct |
423 ms |
211536 KB |
Output is correct |
16 |
Correct |
346 ms |
166584 KB |
Output is correct |
17 |
Correct |
399 ms |
211280 KB |
Output is correct |
18 |
Correct |
252 ms |
187748 KB |
Output is correct |
19 |
Correct |
238 ms |
187444 KB |
Output is correct |
20 |
Correct |
254 ms |
187728 KB |
Output is correct |
21 |
Correct |
227 ms |
187732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
3212 KB |
Output is correct |
3 |
Correct |
51 ms |
6212 KB |
Output is correct |
4 |
Correct |
69 ms |
8272 KB |
Output is correct |
5 |
Correct |
55 ms |
7448 KB |
Output is correct |
6 |
Correct |
72 ms |
8444 KB |
Output is correct |
7 |
Correct |
65 ms |
8528 KB |
Output is correct |
8 |
Correct |
52 ms |
6228 KB |
Output is correct |
9 |
Correct |
78 ms |
8272 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
78 ms |
4832 KB |
Output is correct |
12 |
Correct |
81 ms |
4688 KB |
Output is correct |
13 |
Correct |
77 ms |
4688 KB |
Output is correct |
14 |
Correct |
75 ms |
4692 KB |
Output is correct |
15 |
Correct |
78 ms |
5564 KB |
Output is correct |
16 |
Correct |
36 ms |
3420 KB |
Output is correct |
17 |
Correct |
74 ms |
7072 KB |
Output is correct |
18 |
Correct |
92 ms |
5712 KB |
Output is correct |
19 |
Correct |
73 ms |
7000 KB |
Output is correct |
20 |
Correct |
79 ms |
5548 KB |
Output is correct |
21 |
Correct |
314 ms |
166444 KB |
Output is correct |
22 |
Correct |
464 ms |
211540 KB |
Output is correct |
23 |
Correct |
475 ms |
211580 KB |
Output is correct |
24 |
Correct |
372 ms |
210236 KB |
Output is correct |
25 |
Correct |
448 ms |
211284 KB |
Output is correct |
26 |
Correct |
423 ms |
211536 KB |
Output is correct |
27 |
Correct |
346 ms |
166584 KB |
Output is correct |
28 |
Correct |
399 ms |
211280 KB |
Output is correct |
29 |
Correct |
252 ms |
187748 KB |
Output is correct |
30 |
Correct |
238 ms |
187444 KB |
Output is correct |
31 |
Correct |
254 ms |
187728 KB |
Output is correct |
32 |
Correct |
227 ms |
187732 KB |
Output is correct |
33 |
Correct |
329 ms |
56152 KB |
Output is correct |
34 |
Correct |
361 ms |
55960 KB |
Output is correct |
35 |
Correct |
280 ms |
55124 KB |
Output is correct |
36 |
Correct |
266 ms |
55024 KB |
Output is correct |
37 |
Correct |
236 ms |
74324 KB |
Output is correct |
38 |
Correct |
294 ms |
112976 KB |
Output is correct |
39 |
Correct |
360 ms |
144212 KB |
Output is correct |
40 |
Correct |
254 ms |
55676 KB |
Output is correct |
41 |
Correct |
281 ms |
107000 KB |
Output is correct |
42 |
Correct |
374 ms |
143188 KB |
Output is correct |
43 |
Correct |
253 ms |
74320 KB |
Output is correct |
44 |
Correct |
291 ms |
76116 KB |
Output is correct |
45 |
Correct |
207 ms |
127228 KB |
Output is correct |
46 |
Correct |
168 ms |
70228 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 |
469 ms |
25644 KB |
Output is correct |
4 |
Correct |
478 ms |
25508 KB |
Output is correct |
5 |
Correct |
491 ms |
25668 KB |
Output is correct |
6 |
Correct |
462 ms |
25428 KB |
Output is correct |
7 |
Correct |
466 ms |
25428 KB |
Output is correct |
8 |
Correct |
248 ms |
13708 KB |
Output is correct |
9 |
Correct |
431 ms |
25952 KB |
Output is correct |
10 |
Execution timed out |
3031 ms |
12176 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
3212 KB |
Output is correct |
3 |
Correct |
51 ms |
6212 KB |
Output is correct |
4 |
Correct |
69 ms |
8272 KB |
Output is correct |
5 |
Correct |
55 ms |
7448 KB |
Output is correct |
6 |
Correct |
72 ms |
8444 KB |
Output is correct |
7 |
Correct |
65 ms |
8528 KB |
Output is correct |
8 |
Correct |
52 ms |
6228 KB |
Output is correct |
9 |
Correct |
78 ms |
8272 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
78 ms |
4832 KB |
Output is correct |
12 |
Correct |
81 ms |
4688 KB |
Output is correct |
13 |
Correct |
77 ms |
4688 KB |
Output is correct |
14 |
Correct |
75 ms |
4692 KB |
Output is correct |
15 |
Correct |
78 ms |
5564 KB |
Output is correct |
16 |
Correct |
36 ms |
3420 KB |
Output is correct |
17 |
Correct |
74 ms |
7072 KB |
Output is correct |
18 |
Correct |
92 ms |
5712 KB |
Output is correct |
19 |
Correct |
73 ms |
7000 KB |
Output is correct |
20 |
Correct |
79 ms |
5548 KB |
Output is correct |
21 |
Correct |
314 ms |
166444 KB |
Output is correct |
22 |
Correct |
464 ms |
211540 KB |
Output is correct |
23 |
Correct |
475 ms |
211580 KB |
Output is correct |
24 |
Correct |
372 ms |
210236 KB |
Output is correct |
25 |
Correct |
448 ms |
211284 KB |
Output is correct |
26 |
Correct |
423 ms |
211536 KB |
Output is correct |
27 |
Correct |
346 ms |
166584 KB |
Output is correct |
28 |
Correct |
399 ms |
211280 KB |
Output is correct |
29 |
Correct |
252 ms |
187748 KB |
Output is correct |
30 |
Correct |
238 ms |
187444 KB |
Output is correct |
31 |
Correct |
254 ms |
187728 KB |
Output is correct |
32 |
Correct |
227 ms |
187732 KB |
Output is correct |
33 |
Correct |
329 ms |
56152 KB |
Output is correct |
34 |
Correct |
361 ms |
55960 KB |
Output is correct |
35 |
Correct |
280 ms |
55124 KB |
Output is correct |
36 |
Correct |
266 ms |
55024 KB |
Output is correct |
37 |
Correct |
236 ms |
74324 KB |
Output is correct |
38 |
Correct |
294 ms |
112976 KB |
Output is correct |
39 |
Correct |
360 ms |
144212 KB |
Output is correct |
40 |
Correct |
254 ms |
55676 KB |
Output is correct |
41 |
Correct |
281 ms |
107000 KB |
Output is correct |
42 |
Correct |
374 ms |
143188 KB |
Output is correct |
43 |
Correct |
253 ms |
74320 KB |
Output is correct |
44 |
Correct |
291 ms |
76116 KB |
Output is correct |
45 |
Correct |
207 ms |
127228 KB |
Output is correct |
46 |
Correct |
168 ms |
70228 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
469 ms |
25644 KB |
Output is correct |
50 |
Correct |
478 ms |
25508 KB |
Output is correct |
51 |
Correct |
491 ms |
25668 KB |
Output is correct |
52 |
Correct |
462 ms |
25428 KB |
Output is correct |
53 |
Correct |
466 ms |
25428 KB |
Output is correct |
54 |
Correct |
248 ms |
13708 KB |
Output is correct |
55 |
Correct |
431 ms |
25952 KB |
Output is correct |
56 |
Execution timed out |
3031 ms |
12176 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |