#include "bits/stdc++.h" /** keep-include */
using namespace std;
#define rep(i, b, e) for(int i = (b); i <= (e); i++)
#define per(i, b, e) for(int i = (e); i >= (b); i--)
#define FOR(i, b, e) rep(i, b, (e) - 1)
#define SZ(x) int(x.size())
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair
#define st first
#define nd second
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
auto &operator<<(auto &o, pair<auto, auto> p) {
return o << "(" << p.st << ", " << p.nd << ")"; }
auto operator<<(auto &o, auto x)->decltype(end(x), o) {
o << "{"; int i=0; for(auto e: x) o << ", " + 2*!i++ << e;
return o << "}"; }
#ifdef LOCAL
#define deb(x...) cerr << "[" #x "]: ", [](auto...$) { \
((cerr << $ << "; "),...) << endl; }(x)
#else
#define deb(...)
#endif
const int K=19, N=3e5+5;
vector<pii> loty[N];
vector<int> id[N];
int nxt[K][N];
ll czas[K][N];
pair<int, ll> jump(int v, int t){
ll res=0;
for(int k=K-1; k>=0; k--){
if(t>=(1<<k)){
res+=czas[k][v];
v=nxt[k][v];
t-=(1<<k);
}
}
return mp(v, res);
}
void solve() {
int n, t;
cin>>n>>t;
vector<pair<pii, int> > co;
co.pb(mp(mp(0, 0), 0));
for(int i=1; i<n; i++){
int k;
cin>>k;
vector<pair<int, int> > V(k);
for(auto &j:V){
cin>>j.st>>j.nd;
}
sort(all(V));
vector<pii> V2;
for(auto &j:V){
while(V2.size() && V2.back().nd>=j.nd){
V2.pop_back();
}
V2.pb(j);
}
k=V2.size();
for(int j=0; j<k; j++){
id[i].pb(co.size());
co.pb(mp(V2[j], i));
}
loty[i]=V2;
}
for(int j=1; j<co.size(); j++){
auto &[seg, i] = co[j];
if(i!=n-1){
auto it=lower_bound(all(loty[i+1]), mp(seg.nd, 0));
if(it==loty[i+1].end()){
it=loty[i+1].begin();
nxt[0][j]=id[i+1][it-loty[i+1].begin()];
czas[0][j]=it->nd-seg.nd+t;
deb(czas[0][j], it->nd, seg.nd,t);
}
else{
nxt[0][j]=id[i+1][it-loty[i+1].begin()];
czas[0][j]=it->nd-seg.nd;
}
}
deb(j, seg, i, co[nxt[0][j]], czas[0][j]);
}
for(int k=1; k<K; k++){
for(int i=1; i<co.size(); i++){
nxt[k][i]=nxt[k-1][nxt[k-1][i]];
czas[k][i]=czas[k-1][i]+czas[k-1][nxt[k-1][i]];
}
}
map<pair<int, int> , ll> old_ans;
int q;
cin>>q;
while(q--){
int l, r;
cin>>l>>r;
if(old_ans.find(mp(l, r))!=old_ans.end()){
cout<<old_ans[mp(l, r)]<<"\n";
continue;
}
ll ans=1e18;
for(int i=0; i<loty[l].size(); i++){
ll part=loty[l][i].nd-loty[l][i].st;
int lot=id[l][i];
deb(lot);
/*for(int j=l+1; j<r; j++){
part+=czas[0][lot];
lot=nxt[0][lot];
}*/
part+=jump(lot, r-l-1).nd;
//deb(part);
ans=min(ans, part);
}
old_ans[mp(l, r)]=ans;
cout<<ans<<"\n";
}
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int tt = 1;
// cin >> tt;
FOR(te, 0, tt) solve();
return 0;
}
Compilation message
Main.cpp:17:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
17 | auto &operator<<(auto &o, pair<auto, auto> p) {
| ^~~~
Main.cpp:17:32: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
17 | auto &operator<<(auto &o, pair<auto, auto> p) {
| ^~~~
Main.cpp:17:38: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
17 | auto &operator<<(auto &o, pair<auto, auto> p) {
| ^~~~
Main.cpp:19:17: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | auto operator<<(auto &o, auto x)->decltype(end(x), o) {
| ^~~~
Main.cpp:19:26: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | auto operator<<(auto &o, auto x)->decltype(end(x), o) {
| ^~~~
Main.cpp: In function 'void solve()':
Main.cpp:76:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for(int j=1; j<co.size(); j++){
| ~^~~~~~~~~~
Main.cpp:94:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for(int i=1; i<co.size(); i++){
| ~^~~~~~~~~~
Main.cpp:110:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
110 | for(int i=0; i<loty[l].size(); i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
38 ms |
18404 KB |
Output is correct |
3 |
Correct |
147 ms |
32848 KB |
Output is correct |
4 |
Correct |
217 ms |
38496 KB |
Output is correct |
5 |
Correct |
160 ms |
33620 KB |
Output is correct |
6 |
Correct |
190 ms |
38244 KB |
Output is correct |
7 |
Correct |
193 ms |
38228 KB |
Output is correct |
8 |
Correct |
137 ms |
32340 KB |
Output is correct |
9 |
Correct |
239 ms |
38116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
38 ms |
18404 KB |
Output is correct |
3 |
Correct |
147 ms |
32848 KB |
Output is correct |
4 |
Correct |
217 ms |
38496 KB |
Output is correct |
5 |
Correct |
160 ms |
33620 KB |
Output is correct |
6 |
Correct |
190 ms |
38244 KB |
Output is correct |
7 |
Correct |
193 ms |
38228 KB |
Output is correct |
8 |
Correct |
137 ms |
32340 KB |
Output is correct |
9 |
Correct |
239 ms |
38116 KB |
Output is correct |
10 |
Correct |
7 ms |
14680 KB |
Output is correct |
11 |
Correct |
161 ms |
25112 KB |
Output is correct |
12 |
Correct |
146 ms |
24912 KB |
Output is correct |
13 |
Correct |
133 ms |
23632 KB |
Output is correct |
14 |
Correct |
159 ms |
23636 KB |
Output is correct |
15 |
Correct |
179 ms |
30292 KB |
Output is correct |
16 |
Correct |
36 ms |
17748 KB |
Output is correct |
17 |
Correct |
177 ms |
35688 KB |
Output is correct |
18 |
Correct |
170 ms |
30800 KB |
Output is correct |
19 |
Correct |
209 ms |
35280 KB |
Output is correct |
20 |
Correct |
167 ms |
29520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
38 ms |
18404 KB |
Output is correct |
3 |
Correct |
147 ms |
32848 KB |
Output is correct |
4 |
Correct |
217 ms |
38496 KB |
Output is correct |
5 |
Correct |
160 ms |
33620 KB |
Output is correct |
6 |
Correct |
190 ms |
38244 KB |
Output is correct |
7 |
Correct |
193 ms |
38228 KB |
Output is correct |
8 |
Correct |
137 ms |
32340 KB |
Output is correct |
9 |
Correct |
239 ms |
38116 KB |
Output is correct |
10 |
Correct |
204 ms |
52808 KB |
Output is correct |
11 |
Correct |
329 ms |
66444 KB |
Output is correct |
12 |
Correct |
345 ms |
66308 KB |
Output is correct |
13 |
Correct |
279 ms |
64768 KB |
Output is correct |
14 |
Correct |
464 ms |
70928 KB |
Output is correct |
15 |
Correct |
404 ms |
70912 KB |
Output is correct |
16 |
Correct |
266 ms |
56580 KB |
Output is correct |
17 |
Correct |
377 ms |
70900 KB |
Output is correct |
18 |
Correct |
130 ms |
49920 KB |
Output is correct |
19 |
Correct |
110 ms |
49180 KB |
Output is correct |
20 |
Correct |
142 ms |
50236 KB |
Output is correct |
21 |
Correct |
125 ms |
50188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
38 ms |
18404 KB |
Output is correct |
3 |
Correct |
147 ms |
32848 KB |
Output is correct |
4 |
Correct |
217 ms |
38496 KB |
Output is correct |
5 |
Correct |
160 ms |
33620 KB |
Output is correct |
6 |
Correct |
190 ms |
38244 KB |
Output is correct |
7 |
Correct |
193 ms |
38228 KB |
Output is correct |
8 |
Correct |
137 ms |
32340 KB |
Output is correct |
9 |
Correct |
239 ms |
38116 KB |
Output is correct |
10 |
Correct |
7 ms |
14680 KB |
Output is correct |
11 |
Correct |
161 ms |
25112 KB |
Output is correct |
12 |
Correct |
146 ms |
24912 KB |
Output is correct |
13 |
Correct |
133 ms |
23632 KB |
Output is correct |
14 |
Correct |
159 ms |
23636 KB |
Output is correct |
15 |
Correct |
179 ms |
30292 KB |
Output is correct |
16 |
Correct |
36 ms |
17748 KB |
Output is correct |
17 |
Correct |
177 ms |
35688 KB |
Output is correct |
18 |
Correct |
170 ms |
30800 KB |
Output is correct |
19 |
Correct |
209 ms |
35280 KB |
Output is correct |
20 |
Correct |
167 ms |
29520 KB |
Output is correct |
21 |
Correct |
204 ms |
52808 KB |
Output is correct |
22 |
Correct |
329 ms |
66444 KB |
Output is correct |
23 |
Correct |
345 ms |
66308 KB |
Output is correct |
24 |
Correct |
279 ms |
64768 KB |
Output is correct |
25 |
Correct |
464 ms |
70928 KB |
Output is correct |
26 |
Correct |
404 ms |
70912 KB |
Output is correct |
27 |
Correct |
266 ms |
56580 KB |
Output is correct |
28 |
Correct |
377 ms |
70900 KB |
Output is correct |
29 |
Correct |
130 ms |
49920 KB |
Output is correct |
30 |
Correct |
110 ms |
49180 KB |
Output is correct |
31 |
Correct |
142 ms |
50236 KB |
Output is correct |
32 |
Correct |
125 ms |
50188 KB |
Output is correct |
33 |
Correct |
388 ms |
65652 KB |
Output is correct |
34 |
Correct |
461 ms |
65540 KB |
Output is correct |
35 |
Correct |
402 ms |
62976 KB |
Output is correct |
36 |
Correct |
404 ms |
62984 KB |
Output is correct |
37 |
Correct |
470 ms |
64136 KB |
Output is correct |
38 |
Correct |
249 ms |
54780 KB |
Output is correct |
39 |
Correct |
370 ms |
63988 KB |
Output is correct |
40 |
Correct |
299 ms |
53484 KB |
Output is correct |
41 |
Correct |
272 ms |
55556 KB |
Output is correct |
42 |
Correct |
394 ms |
66308 KB |
Output is correct |
43 |
Correct |
270 ms |
56096 KB |
Output is correct |
44 |
Correct |
373 ms |
60796 KB |
Output is correct |
45 |
Correct |
116 ms |
48996 KB |
Output is correct |
46 |
Correct |
112 ms |
46760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14684 KB |
Output is correct |
2 |
Correct |
8 ms |
14684 KB |
Output is correct |
3 |
Correct |
263 ms |
43272 KB |
Output is correct |
4 |
Correct |
277 ms |
43284 KB |
Output is correct |
5 |
Correct |
280 ms |
43188 KB |
Output is correct |
6 |
Correct |
249 ms |
43164 KB |
Output is correct |
7 |
Correct |
262 ms |
43104 KB |
Output is correct |
8 |
Correct |
163 ms |
31908 KB |
Output is correct |
9 |
Correct |
267 ms |
43264 KB |
Output is correct |
10 |
Correct |
51 ms |
28116 KB |
Output is correct |
11 |
Correct |
120 ms |
51204 KB |
Output is correct |
12 |
Correct |
127 ms |
49264 KB |
Output is correct |
13 |
Correct |
112 ms |
47360 KB |
Output is correct |
14 |
Correct |
120 ms |
45316 KB |
Output is correct |
15 |
Correct |
98 ms |
44552 KB |
Output is correct |
16 |
Correct |
101 ms |
47360 KB |
Output is correct |
17 |
Correct |
107 ms |
46592 KB |
Output is correct |
18 |
Correct |
155 ms |
43968 KB |
Output is correct |
19 |
Correct |
177 ms |
42564 KB |
Output is correct |
20 |
Correct |
87 ms |
44548 KB |
Output is correct |
21 |
Correct |
110 ms |
46476 KB |
Output is correct |
22 |
Correct |
130 ms |
43268 KB |
Output is correct |
23 |
Correct |
143 ms |
45112 KB |
Output is correct |
24 |
Execution timed out |
3073 ms |
42016 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
14680 KB |
Output is correct |
2 |
Correct |
38 ms |
18404 KB |
Output is correct |
3 |
Correct |
147 ms |
32848 KB |
Output is correct |
4 |
Correct |
217 ms |
38496 KB |
Output is correct |
5 |
Correct |
160 ms |
33620 KB |
Output is correct |
6 |
Correct |
190 ms |
38244 KB |
Output is correct |
7 |
Correct |
193 ms |
38228 KB |
Output is correct |
8 |
Correct |
137 ms |
32340 KB |
Output is correct |
9 |
Correct |
239 ms |
38116 KB |
Output is correct |
10 |
Correct |
7 ms |
14680 KB |
Output is correct |
11 |
Correct |
161 ms |
25112 KB |
Output is correct |
12 |
Correct |
146 ms |
24912 KB |
Output is correct |
13 |
Correct |
133 ms |
23632 KB |
Output is correct |
14 |
Correct |
159 ms |
23636 KB |
Output is correct |
15 |
Correct |
179 ms |
30292 KB |
Output is correct |
16 |
Correct |
36 ms |
17748 KB |
Output is correct |
17 |
Correct |
177 ms |
35688 KB |
Output is correct |
18 |
Correct |
170 ms |
30800 KB |
Output is correct |
19 |
Correct |
209 ms |
35280 KB |
Output is correct |
20 |
Correct |
167 ms |
29520 KB |
Output is correct |
21 |
Correct |
204 ms |
52808 KB |
Output is correct |
22 |
Correct |
329 ms |
66444 KB |
Output is correct |
23 |
Correct |
345 ms |
66308 KB |
Output is correct |
24 |
Correct |
279 ms |
64768 KB |
Output is correct |
25 |
Correct |
464 ms |
70928 KB |
Output is correct |
26 |
Correct |
404 ms |
70912 KB |
Output is correct |
27 |
Correct |
266 ms |
56580 KB |
Output is correct |
28 |
Correct |
377 ms |
70900 KB |
Output is correct |
29 |
Correct |
130 ms |
49920 KB |
Output is correct |
30 |
Correct |
110 ms |
49180 KB |
Output is correct |
31 |
Correct |
142 ms |
50236 KB |
Output is correct |
32 |
Correct |
125 ms |
50188 KB |
Output is correct |
33 |
Correct |
388 ms |
65652 KB |
Output is correct |
34 |
Correct |
461 ms |
65540 KB |
Output is correct |
35 |
Correct |
402 ms |
62976 KB |
Output is correct |
36 |
Correct |
404 ms |
62984 KB |
Output is correct |
37 |
Correct |
470 ms |
64136 KB |
Output is correct |
38 |
Correct |
249 ms |
54780 KB |
Output is correct |
39 |
Correct |
370 ms |
63988 KB |
Output is correct |
40 |
Correct |
299 ms |
53484 KB |
Output is correct |
41 |
Correct |
272 ms |
55556 KB |
Output is correct |
42 |
Correct |
394 ms |
66308 KB |
Output is correct |
43 |
Correct |
270 ms |
56096 KB |
Output is correct |
44 |
Correct |
373 ms |
60796 KB |
Output is correct |
45 |
Correct |
116 ms |
48996 KB |
Output is correct |
46 |
Correct |
112 ms |
46760 KB |
Output is correct |
47 |
Correct |
6 ms |
14684 KB |
Output is correct |
48 |
Correct |
8 ms |
14684 KB |
Output is correct |
49 |
Correct |
263 ms |
43272 KB |
Output is correct |
50 |
Correct |
277 ms |
43284 KB |
Output is correct |
51 |
Correct |
280 ms |
43188 KB |
Output is correct |
52 |
Correct |
249 ms |
43164 KB |
Output is correct |
53 |
Correct |
262 ms |
43104 KB |
Output is correct |
54 |
Correct |
163 ms |
31908 KB |
Output is correct |
55 |
Correct |
267 ms |
43264 KB |
Output is correct |
56 |
Correct |
51 ms |
28116 KB |
Output is correct |
57 |
Correct |
120 ms |
51204 KB |
Output is correct |
58 |
Correct |
127 ms |
49264 KB |
Output is correct |
59 |
Correct |
112 ms |
47360 KB |
Output is correct |
60 |
Correct |
120 ms |
45316 KB |
Output is correct |
61 |
Correct |
98 ms |
44552 KB |
Output is correct |
62 |
Correct |
101 ms |
47360 KB |
Output is correct |
63 |
Correct |
107 ms |
46592 KB |
Output is correct |
64 |
Correct |
155 ms |
43968 KB |
Output is correct |
65 |
Correct |
177 ms |
42564 KB |
Output is correct |
66 |
Correct |
87 ms |
44548 KB |
Output is correct |
67 |
Correct |
110 ms |
46476 KB |
Output is correct |
68 |
Correct |
130 ms |
43268 KB |
Output is correct |
69 |
Correct |
143 ms |
45112 KB |
Output is correct |
70 |
Execution timed out |
3073 ms |
42016 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |