#include<bits/stdc++.h>
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:33: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]
33 | for(int j = 0;j < pp[i].size();j++){
| ~~^~~~~~~~~~~~~~
Main.cpp:43: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]
43 | for(int j = 0;j < pp[i].size();j++){
| ~~^~~~~~~~~~~~~~
Main.cpp:44:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
44 | int na = pp[i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]].first;
| ~^~
Main.cpp:44:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
44 | int na = pp[i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]].first;
| ~^~
Main.cpp:45:36: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
45 | int nb = pp[i+(1<<l-1)-1][blt[l-1][i][j]].second;
| ~^~
Main.cpp:47:76: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
47 | bl[l][i][j] = bl[l-1][i][j] + t+na-nb + bl[l-1][i+(1<<l-1)][0];
| ~^~
Main.cpp:48:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
48 | blt[l][i][j] = blt[l-1][i+(1<<l-1)][0];
| ~^~
Main.cpp:50:74: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
50 | 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:50:90: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
50 | 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:51:52: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
51 | blt[l][i][j] = blt[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
| ~^~
Main.cpp:51:68: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
51 | blt[l][i][j] = blt[l-1][i+(1<<l-1)][nxt[i+(1<<l-1)-1][blt[l-1][i][j]]];
| ~^~
Main.cpp:61: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]
61 | for(int j = 0;j < pp[ol].size();j++){
| ~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
4436 KB |
Output is correct |
3 |
Correct |
64 ms |
7912 KB |
Output is correct |
4 |
Correct |
73 ms |
10704 KB |
Output is correct |
5 |
Correct |
57 ms |
9556 KB |
Output is correct |
6 |
Correct |
75 ms |
10708 KB |
Output is correct |
7 |
Correct |
74 ms |
10836 KB |
Output is correct |
8 |
Correct |
64 ms |
7988 KB |
Output is correct |
9 |
Correct |
82 ms |
10576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
4436 KB |
Output is correct |
3 |
Correct |
64 ms |
7912 KB |
Output is correct |
4 |
Correct |
73 ms |
10704 KB |
Output is correct |
5 |
Correct |
57 ms |
9556 KB |
Output is correct |
6 |
Correct |
75 ms |
10708 KB |
Output is correct |
7 |
Correct |
74 ms |
10836 KB |
Output is correct |
8 |
Correct |
64 ms |
7988 KB |
Output is correct |
9 |
Correct |
82 ms |
10576 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
87 ms |
7088 KB |
Output is correct |
12 |
Correct |
94 ms |
7108 KB |
Output is correct |
13 |
Correct |
88 ms |
7168 KB |
Output is correct |
14 |
Correct |
87 ms |
6992 KB |
Output is correct |
15 |
Correct |
87 ms |
8272 KB |
Output is correct |
16 |
Correct |
35 ms |
4456 KB |
Output is correct |
17 |
Correct |
94 ms |
9556 KB |
Output is correct |
18 |
Correct |
93 ms |
8044 KB |
Output is correct |
19 |
Correct |
84 ms |
9556 KB |
Output is correct |
20 |
Correct |
87 ms |
7796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
4436 KB |
Output is correct |
3 |
Correct |
64 ms |
7912 KB |
Output is correct |
4 |
Correct |
73 ms |
10704 KB |
Output is correct |
5 |
Correct |
57 ms |
9556 KB |
Output is correct |
6 |
Correct |
75 ms |
10708 KB |
Output is correct |
7 |
Correct |
74 ms |
10836 KB |
Output is correct |
8 |
Correct |
64 ms |
7988 KB |
Output is correct |
9 |
Correct |
82 ms |
10576 KB |
Output is correct |
10 |
Correct |
328 ms |
169748 KB |
Output is correct |
11 |
Correct |
475 ms |
215944 KB |
Output is correct |
12 |
Correct |
462 ms |
216076 KB |
Output is correct |
13 |
Correct |
366 ms |
214096 KB |
Output is correct |
14 |
Correct |
467 ms |
215888 KB |
Output is correct |
15 |
Correct |
421 ms |
216132 KB |
Output is correct |
16 |
Correct |
290 ms |
169812 KB |
Output is correct |
17 |
Correct |
440 ms |
215892 KB |
Output is correct |
18 |
Correct |
251 ms |
190292 KB |
Output is correct |
19 |
Correct |
234 ms |
189828 KB |
Output is correct |
20 |
Correct |
245 ms |
190164 KB |
Output is correct |
21 |
Correct |
240 ms |
190292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
4436 KB |
Output is correct |
3 |
Correct |
64 ms |
7912 KB |
Output is correct |
4 |
Correct |
73 ms |
10704 KB |
Output is correct |
5 |
Correct |
57 ms |
9556 KB |
Output is correct |
6 |
Correct |
75 ms |
10708 KB |
Output is correct |
7 |
Correct |
74 ms |
10836 KB |
Output is correct |
8 |
Correct |
64 ms |
7988 KB |
Output is correct |
9 |
Correct |
82 ms |
10576 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
87 ms |
7088 KB |
Output is correct |
12 |
Correct |
94 ms |
7108 KB |
Output is correct |
13 |
Correct |
88 ms |
7168 KB |
Output is correct |
14 |
Correct |
87 ms |
6992 KB |
Output is correct |
15 |
Correct |
87 ms |
8272 KB |
Output is correct |
16 |
Correct |
35 ms |
4456 KB |
Output is correct |
17 |
Correct |
94 ms |
9556 KB |
Output is correct |
18 |
Correct |
93 ms |
8044 KB |
Output is correct |
19 |
Correct |
84 ms |
9556 KB |
Output is correct |
20 |
Correct |
87 ms |
7796 KB |
Output is correct |
21 |
Correct |
328 ms |
169748 KB |
Output is correct |
22 |
Correct |
475 ms |
215944 KB |
Output is correct |
23 |
Correct |
462 ms |
216076 KB |
Output is correct |
24 |
Correct |
366 ms |
214096 KB |
Output is correct |
25 |
Correct |
467 ms |
215888 KB |
Output is correct |
26 |
Correct |
421 ms |
216132 KB |
Output is correct |
27 |
Correct |
290 ms |
169812 KB |
Output is correct |
28 |
Correct |
440 ms |
215892 KB |
Output is correct |
29 |
Correct |
251 ms |
190292 KB |
Output is correct |
30 |
Correct |
234 ms |
189828 KB |
Output is correct |
31 |
Correct |
245 ms |
190164 KB |
Output is correct |
32 |
Correct |
240 ms |
190292 KB |
Output is correct |
33 |
Correct |
382 ms |
61264 KB |
Output is correct |
34 |
Correct |
404 ms |
61168 KB |
Output is correct |
35 |
Correct |
377 ms |
60088 KB |
Output is correct |
36 |
Correct |
337 ms |
60096 KB |
Output is correct |
37 |
Correct |
295 ms |
79572 KB |
Output is correct |
38 |
Correct |
312 ms |
117056 KB |
Output is correct |
39 |
Correct |
374 ms |
149652 KB |
Output is correct |
40 |
Correct |
324 ms |
60244 KB |
Output is correct |
41 |
Correct |
296 ms |
111476 KB |
Output is correct |
42 |
Correct |
384 ms |
148648 KB |
Output is correct |
43 |
Correct |
270 ms |
78932 KB |
Output is correct |
44 |
Correct |
285 ms |
81488 KB |
Output is correct |
45 |
Correct |
213 ms |
130124 KB |
Output is correct |
46 |
Correct |
165 ms |
72944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
550 ms |
28068 KB |
Output is correct |
4 |
Correct |
552 ms |
27988 KB |
Output is correct |
5 |
Correct |
539 ms |
27852 KB |
Output is correct |
6 |
Correct |
545 ms |
27984 KB |
Output is correct |
7 |
Correct |
550 ms |
28284 KB |
Output is correct |
8 |
Correct |
297 ms |
15952 KB |
Output is correct |
9 |
Correct |
506 ms |
28548 KB |
Output is correct |
10 |
Execution timed out |
3037 ms |
13620 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
32 ms |
4436 KB |
Output is correct |
3 |
Correct |
64 ms |
7912 KB |
Output is correct |
4 |
Correct |
73 ms |
10704 KB |
Output is correct |
5 |
Correct |
57 ms |
9556 KB |
Output is correct |
6 |
Correct |
75 ms |
10708 KB |
Output is correct |
7 |
Correct |
74 ms |
10836 KB |
Output is correct |
8 |
Correct |
64 ms |
7988 KB |
Output is correct |
9 |
Correct |
82 ms |
10576 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
87 ms |
7088 KB |
Output is correct |
12 |
Correct |
94 ms |
7108 KB |
Output is correct |
13 |
Correct |
88 ms |
7168 KB |
Output is correct |
14 |
Correct |
87 ms |
6992 KB |
Output is correct |
15 |
Correct |
87 ms |
8272 KB |
Output is correct |
16 |
Correct |
35 ms |
4456 KB |
Output is correct |
17 |
Correct |
94 ms |
9556 KB |
Output is correct |
18 |
Correct |
93 ms |
8044 KB |
Output is correct |
19 |
Correct |
84 ms |
9556 KB |
Output is correct |
20 |
Correct |
87 ms |
7796 KB |
Output is correct |
21 |
Correct |
328 ms |
169748 KB |
Output is correct |
22 |
Correct |
475 ms |
215944 KB |
Output is correct |
23 |
Correct |
462 ms |
216076 KB |
Output is correct |
24 |
Correct |
366 ms |
214096 KB |
Output is correct |
25 |
Correct |
467 ms |
215888 KB |
Output is correct |
26 |
Correct |
421 ms |
216132 KB |
Output is correct |
27 |
Correct |
290 ms |
169812 KB |
Output is correct |
28 |
Correct |
440 ms |
215892 KB |
Output is correct |
29 |
Correct |
251 ms |
190292 KB |
Output is correct |
30 |
Correct |
234 ms |
189828 KB |
Output is correct |
31 |
Correct |
245 ms |
190164 KB |
Output is correct |
32 |
Correct |
240 ms |
190292 KB |
Output is correct |
33 |
Correct |
382 ms |
61264 KB |
Output is correct |
34 |
Correct |
404 ms |
61168 KB |
Output is correct |
35 |
Correct |
377 ms |
60088 KB |
Output is correct |
36 |
Correct |
337 ms |
60096 KB |
Output is correct |
37 |
Correct |
295 ms |
79572 KB |
Output is correct |
38 |
Correct |
312 ms |
117056 KB |
Output is correct |
39 |
Correct |
374 ms |
149652 KB |
Output is correct |
40 |
Correct |
324 ms |
60244 KB |
Output is correct |
41 |
Correct |
296 ms |
111476 KB |
Output is correct |
42 |
Correct |
384 ms |
148648 KB |
Output is correct |
43 |
Correct |
270 ms |
78932 KB |
Output is correct |
44 |
Correct |
285 ms |
81488 KB |
Output is correct |
45 |
Correct |
213 ms |
130124 KB |
Output is correct |
46 |
Correct |
165 ms |
72944 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
550 ms |
28068 KB |
Output is correct |
50 |
Correct |
552 ms |
27988 KB |
Output is correct |
51 |
Correct |
539 ms |
27852 KB |
Output is correct |
52 |
Correct |
545 ms |
27984 KB |
Output is correct |
53 |
Correct |
550 ms |
28284 KB |
Output is correct |
54 |
Correct |
297 ms |
15952 KB |
Output is correct |
55 |
Correct |
506 ms |
28548 KB |
Output is correct |
56 |
Execution timed out |
3037 ms |
13620 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |