#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];
vi best[K][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]];
}
}
for(int k=0; k<K; k++){
for(int i=1; i<n; i++){
vector<pair<int, pair<ll, int> > > V;//dokad, czas, id
for(int j=0; j<loty[i].size(); j++){
V.pb(mp(nxt[k][id[i][j]], mp(czas[k][id[i][j]]+loty[i][j].nd-loty[i][j].st, j)));
}
sort(all(V));
for(int j=0; j<V.size(); j++){
if(j==0 || V[j].st!=V[j-1].st){
best[k][i].pb(V[j].nd.nd);
}
}
assert(best[k][i].size());
}
}
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;
int k=0;
int dl=r-l-1;
if(dl>0){
while((1<<k)<=dl)k++;
k--;
assert(best[k][l].size());
//for(int i=0; i<loty[l].size(); i++){
for(int i:best[k][l]){
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, dl).nd;
//deb(part);
ans=min(ans, part);
}
}
else{
for(int i=0; i<loty[l].size(); i++){
ans=min(ans, ll(loty[l][i].nd-loty[l][i].st));
}
}
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:77: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]
77 | for(int j=1; j<co.size(); j++){
| ~^~~~~~~~~~
Main.cpp:95: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]
95 | for(int i=1; i<co.size(); i++){
| ~^~~~~~~~~~
Main.cpp:103:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
103 | for(int j=0; j<loty[i].size(); j++){
| ~^~~~~~~~~~~~~~~
Main.cpp:107:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<long long int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
107 | for(int j=0; j<V.size(); j++){
| ~^~~~~~~~~
Main.cpp:147:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
147 | for(int i=0; i<loty[l].size(); i++){
| ~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
148560 KB |
Output is correct |
2 |
Correct |
96 ms |
151632 KB |
Output is correct |
3 |
Correct |
205 ms |
168532 KB |
Output is correct |
4 |
Correct |
300 ms |
174672 KB |
Output is correct |
5 |
Correct |
236 ms |
169284 KB |
Output is correct |
6 |
Correct |
293 ms |
174420 KB |
Output is correct |
7 |
Correct |
293 ms |
174460 KB |
Output is correct |
8 |
Correct |
211 ms |
167784 KB |
Output is correct |
9 |
Correct |
299 ms |
174420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
148560 KB |
Output is correct |
2 |
Correct |
96 ms |
151632 KB |
Output is correct |
3 |
Correct |
205 ms |
168532 KB |
Output is correct |
4 |
Correct |
300 ms |
174672 KB |
Output is correct |
5 |
Correct |
236 ms |
169284 KB |
Output is correct |
6 |
Correct |
293 ms |
174420 KB |
Output is correct |
7 |
Correct |
293 ms |
174460 KB |
Output is correct |
8 |
Correct |
211 ms |
167784 KB |
Output is correct |
9 |
Correct |
299 ms |
174420 KB |
Output is correct |
10 |
Correct |
66 ms |
148564 KB |
Output is correct |
11 |
Correct |
210 ms |
160080 KB |
Output is correct |
12 |
Correct |
229 ms |
160104 KB |
Output is correct |
13 |
Correct |
218 ms |
159832 KB |
Output is correct |
14 |
Correct |
218 ms |
159920 KB |
Output is correct |
15 |
Correct |
238 ms |
166744 KB |
Output is correct |
16 |
Correct |
106 ms |
152660 KB |
Output is correct |
17 |
Correct |
278 ms |
172900 KB |
Output is correct |
18 |
Correct |
232 ms |
167508 KB |
Output is correct |
19 |
Correct |
272 ms |
172040 KB |
Output is correct |
20 |
Correct |
252 ms |
166244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
148560 KB |
Output is correct |
2 |
Correct |
96 ms |
151632 KB |
Output is correct |
3 |
Correct |
205 ms |
168532 KB |
Output is correct |
4 |
Correct |
300 ms |
174672 KB |
Output is correct |
5 |
Correct |
236 ms |
169284 KB |
Output is correct |
6 |
Correct |
293 ms |
174420 KB |
Output is correct |
7 |
Correct |
293 ms |
174460 KB |
Output is correct |
8 |
Correct |
211 ms |
167784 KB |
Output is correct |
9 |
Correct |
299 ms |
174420 KB |
Output is correct |
10 |
Correct |
411 ms |
237808 KB |
Output is correct |
11 |
Correct |
604 ms |
265216 KB |
Output is correct |
12 |
Correct |
720 ms |
265292 KB |
Output is correct |
13 |
Correct |
649 ms |
259788 KB |
Output is correct |
14 |
Correct |
745 ms |
266356 KB |
Output is correct |
15 |
Correct |
639 ms |
266656 KB |
Output is correct |
16 |
Correct |
434 ms |
239112 KB |
Output is correct |
17 |
Correct |
685 ms |
266548 KB |
Output is correct |
18 |
Correct |
306 ms |
238592 KB |
Output is correct |
19 |
Correct |
289 ms |
237568 KB |
Output is correct |
20 |
Correct |
303 ms |
238576 KB |
Output is correct |
21 |
Correct |
331 ms |
238760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
148560 KB |
Output is correct |
2 |
Correct |
96 ms |
151632 KB |
Output is correct |
3 |
Correct |
205 ms |
168532 KB |
Output is correct |
4 |
Correct |
300 ms |
174672 KB |
Output is correct |
5 |
Correct |
236 ms |
169284 KB |
Output is correct |
6 |
Correct |
293 ms |
174420 KB |
Output is correct |
7 |
Correct |
293 ms |
174460 KB |
Output is correct |
8 |
Correct |
211 ms |
167784 KB |
Output is correct |
9 |
Correct |
299 ms |
174420 KB |
Output is correct |
10 |
Correct |
66 ms |
148564 KB |
Output is correct |
11 |
Correct |
210 ms |
160080 KB |
Output is correct |
12 |
Correct |
229 ms |
160104 KB |
Output is correct |
13 |
Correct |
218 ms |
159832 KB |
Output is correct |
14 |
Correct |
218 ms |
159920 KB |
Output is correct |
15 |
Correct |
238 ms |
166744 KB |
Output is correct |
16 |
Correct |
106 ms |
152660 KB |
Output is correct |
17 |
Correct |
278 ms |
172900 KB |
Output is correct |
18 |
Correct |
232 ms |
167508 KB |
Output is correct |
19 |
Correct |
272 ms |
172040 KB |
Output is correct |
20 |
Correct |
252 ms |
166244 KB |
Output is correct |
21 |
Correct |
411 ms |
237808 KB |
Output is correct |
22 |
Correct |
604 ms |
265216 KB |
Output is correct |
23 |
Correct |
720 ms |
265292 KB |
Output is correct |
24 |
Correct |
649 ms |
259788 KB |
Output is correct |
25 |
Correct |
745 ms |
266356 KB |
Output is correct |
26 |
Correct |
639 ms |
266656 KB |
Output is correct |
27 |
Correct |
434 ms |
239112 KB |
Output is correct |
28 |
Correct |
685 ms |
266548 KB |
Output is correct |
29 |
Correct |
306 ms |
238592 KB |
Output is correct |
30 |
Correct |
289 ms |
237568 KB |
Output is correct |
31 |
Correct |
303 ms |
238576 KB |
Output is correct |
32 |
Correct |
331 ms |
238760 KB |
Output is correct |
33 |
Correct |
557 ms |
218116 KB |
Output is correct |
34 |
Correct |
614 ms |
217856 KB |
Output is correct |
35 |
Correct |
597 ms |
210948 KB |
Output is correct |
36 |
Correct |
500 ms |
210948 KB |
Output is correct |
37 |
Correct |
555 ms |
218136 KB |
Output is correct |
38 |
Correct |
396 ms |
221696 KB |
Output is correct |
39 |
Correct |
500 ms |
239876 KB |
Output is correct |
40 |
Correct |
403 ms |
202496 KB |
Output is correct |
41 |
Correct |
463 ms |
220676 KB |
Output is correct |
42 |
Correct |
587 ms |
241932 KB |
Output is correct |
43 |
Correct |
420 ms |
210388 KB |
Output is correct |
44 |
Correct |
458 ms |
215452 KB |
Output is correct |
45 |
Correct |
291 ms |
218852 KB |
Output is correct |
46 |
Correct |
229 ms |
198912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
148564 KB |
Output is correct |
2 |
Correct |
60 ms |
148560 KB |
Output is correct |
3 |
Correct |
423 ms |
179204 KB |
Output is correct |
4 |
Correct |
424 ms |
181520 KB |
Output is correct |
5 |
Correct |
428 ms |
181764 KB |
Output is correct |
6 |
Correct |
416 ms |
181808 KB |
Output is correct |
7 |
Correct |
411 ms |
181556 KB |
Output is correct |
8 |
Correct |
174 ms |
166460 KB |
Output is correct |
9 |
Correct |
173 ms |
177404 KB |
Output is correct |
10 |
Correct |
166 ms |
164408 KB |
Output is correct |
11 |
Correct |
292 ms |
238500 KB |
Output is correct |
12 |
Correct |
320 ms |
218820 KB |
Output is correct |
13 |
Correct |
243 ms |
198920 KB |
Output is correct |
14 |
Correct |
273 ms |
190368 KB |
Output is correct |
15 |
Correct |
246 ms |
189708 KB |
Output is correct |
16 |
Correct |
222 ms |
192512 KB |
Output is correct |
17 |
Correct |
274 ms |
192016 KB |
Output is correct |
18 |
Correct |
235 ms |
178788 KB |
Output is correct |
19 |
Correct |
230 ms |
177156 KB |
Output is correct |
20 |
Correct |
213 ms |
217428 KB |
Output is correct |
21 |
Correct |
238 ms |
219360 KB |
Output is correct |
22 |
Correct |
207 ms |
196952 KB |
Output is correct |
23 |
Correct |
242 ms |
198912 KB |
Output is correct |
24 |
Correct |
305 ms |
199148 KB |
Output is correct |
25 |
Correct |
241 ms |
196260 KB |
Output is correct |
26 |
Correct |
275 ms |
200708 KB |
Output is correct |
27 |
Correct |
285 ms |
237568 KB |
Output is correct |
28 |
Correct |
303 ms |
238592 KB |
Output is correct |
29 |
Correct |
290 ms |
238760 KB |
Output is correct |
30 |
Correct |
153 ms |
164272 KB |
Output is correct |
31 |
Correct |
96 ms |
152060 KB |
Output is correct |
32 |
Correct |
143 ms |
164492 KB |
Output is correct |
33 |
Correct |
115 ms |
153228 KB |
Output is correct |
34 |
Correct |
134 ms |
158288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
148560 KB |
Output is correct |
2 |
Correct |
96 ms |
151632 KB |
Output is correct |
3 |
Correct |
205 ms |
168532 KB |
Output is correct |
4 |
Correct |
300 ms |
174672 KB |
Output is correct |
5 |
Correct |
236 ms |
169284 KB |
Output is correct |
6 |
Correct |
293 ms |
174420 KB |
Output is correct |
7 |
Correct |
293 ms |
174460 KB |
Output is correct |
8 |
Correct |
211 ms |
167784 KB |
Output is correct |
9 |
Correct |
299 ms |
174420 KB |
Output is correct |
10 |
Correct |
66 ms |
148564 KB |
Output is correct |
11 |
Correct |
210 ms |
160080 KB |
Output is correct |
12 |
Correct |
229 ms |
160104 KB |
Output is correct |
13 |
Correct |
218 ms |
159832 KB |
Output is correct |
14 |
Correct |
218 ms |
159920 KB |
Output is correct |
15 |
Correct |
238 ms |
166744 KB |
Output is correct |
16 |
Correct |
106 ms |
152660 KB |
Output is correct |
17 |
Correct |
278 ms |
172900 KB |
Output is correct |
18 |
Correct |
232 ms |
167508 KB |
Output is correct |
19 |
Correct |
272 ms |
172040 KB |
Output is correct |
20 |
Correct |
252 ms |
166244 KB |
Output is correct |
21 |
Correct |
411 ms |
237808 KB |
Output is correct |
22 |
Correct |
604 ms |
265216 KB |
Output is correct |
23 |
Correct |
720 ms |
265292 KB |
Output is correct |
24 |
Correct |
649 ms |
259788 KB |
Output is correct |
25 |
Correct |
745 ms |
266356 KB |
Output is correct |
26 |
Correct |
639 ms |
266656 KB |
Output is correct |
27 |
Correct |
434 ms |
239112 KB |
Output is correct |
28 |
Correct |
685 ms |
266548 KB |
Output is correct |
29 |
Correct |
306 ms |
238592 KB |
Output is correct |
30 |
Correct |
289 ms |
237568 KB |
Output is correct |
31 |
Correct |
303 ms |
238576 KB |
Output is correct |
32 |
Correct |
331 ms |
238760 KB |
Output is correct |
33 |
Correct |
557 ms |
218116 KB |
Output is correct |
34 |
Correct |
614 ms |
217856 KB |
Output is correct |
35 |
Correct |
597 ms |
210948 KB |
Output is correct |
36 |
Correct |
500 ms |
210948 KB |
Output is correct |
37 |
Correct |
555 ms |
218136 KB |
Output is correct |
38 |
Correct |
396 ms |
221696 KB |
Output is correct |
39 |
Correct |
500 ms |
239876 KB |
Output is correct |
40 |
Correct |
403 ms |
202496 KB |
Output is correct |
41 |
Correct |
463 ms |
220676 KB |
Output is correct |
42 |
Correct |
587 ms |
241932 KB |
Output is correct |
43 |
Correct |
420 ms |
210388 KB |
Output is correct |
44 |
Correct |
458 ms |
215452 KB |
Output is correct |
45 |
Correct |
291 ms |
218852 KB |
Output is correct |
46 |
Correct |
229 ms |
198912 KB |
Output is correct |
47 |
Correct |
61 ms |
148564 KB |
Output is correct |
48 |
Correct |
60 ms |
148560 KB |
Output is correct |
49 |
Correct |
423 ms |
179204 KB |
Output is correct |
50 |
Correct |
424 ms |
181520 KB |
Output is correct |
51 |
Correct |
428 ms |
181764 KB |
Output is correct |
52 |
Correct |
416 ms |
181808 KB |
Output is correct |
53 |
Correct |
411 ms |
181556 KB |
Output is correct |
54 |
Correct |
174 ms |
166460 KB |
Output is correct |
55 |
Correct |
173 ms |
177404 KB |
Output is correct |
56 |
Correct |
166 ms |
164408 KB |
Output is correct |
57 |
Correct |
292 ms |
238500 KB |
Output is correct |
58 |
Correct |
320 ms |
218820 KB |
Output is correct |
59 |
Correct |
243 ms |
198920 KB |
Output is correct |
60 |
Correct |
273 ms |
190368 KB |
Output is correct |
61 |
Correct |
246 ms |
189708 KB |
Output is correct |
62 |
Correct |
222 ms |
192512 KB |
Output is correct |
63 |
Correct |
274 ms |
192016 KB |
Output is correct |
64 |
Correct |
235 ms |
178788 KB |
Output is correct |
65 |
Correct |
230 ms |
177156 KB |
Output is correct |
66 |
Correct |
213 ms |
217428 KB |
Output is correct |
67 |
Correct |
238 ms |
219360 KB |
Output is correct |
68 |
Correct |
207 ms |
196952 KB |
Output is correct |
69 |
Correct |
242 ms |
198912 KB |
Output is correct |
70 |
Correct |
305 ms |
199148 KB |
Output is correct |
71 |
Correct |
241 ms |
196260 KB |
Output is correct |
72 |
Correct |
275 ms |
200708 KB |
Output is correct |
73 |
Correct |
285 ms |
237568 KB |
Output is correct |
74 |
Correct |
303 ms |
238592 KB |
Output is correct |
75 |
Correct |
290 ms |
238760 KB |
Output is correct |
76 |
Correct |
153 ms |
164272 KB |
Output is correct |
77 |
Correct |
96 ms |
152060 KB |
Output is correct |
78 |
Correct |
143 ms |
164492 KB |
Output is correct |
79 |
Correct |
115 ms |
153228 KB |
Output is correct |
80 |
Correct |
134 ms |
158288 KB |
Output is correct |
81 |
Correct |
762 ms |
194308 KB |
Output is correct |
82 |
Correct |
806 ms |
194184 KB |
Output is correct |
83 |
Correct |
789 ms |
194108 KB |
Output is correct |
84 |
Correct |
731 ms |
194052 KB |
Output is correct |
85 |
Correct |
856 ms |
194304 KB |
Output is correct |
86 |
Correct |
309 ms |
178184 KB |
Output is correct |
87 |
Correct |
324 ms |
190704 KB |
Output is correct |
88 |
Correct |
1098 ms |
206488 KB |
Output is correct |
89 |
Correct |
918 ms |
202152 KB |
Output is correct |
90 |
Correct |
352 ms |
183752 KB |
Output is correct |
91 |
Correct |
183 ms |
167452 KB |
Output is correct |
92 |
Correct |
395 ms |
205024 KB |
Output is correct |
93 |
Correct |
413 ms |
203520 KB |
Output is correct |
94 |
Correct |
398 ms |
214788 KB |
Output is correct |
95 |
Correct |
483 ms |
211400 KB |
Output is correct |
96 |
Correct |
375 ms |
191880 KB |
Output is correct |
97 |
Correct |
337 ms |
186624 KB |
Output is correct |
98 |
Correct |
321 ms |
225768 KB |
Output is correct |
99 |
Correct |
434 ms |
231940 KB |
Output is correct |
100 |
Correct |
296 ms |
204800 KB |
Output is correct |
101 |
Correct |
390 ms |
211136 KB |
Output is correct |
102 |
Correct |
476 ms |
209020 KB |
Output is correct |
103 |
Correct |
334 ms |
205000 KB |
Output is correct |
104 |
Correct |
476 ms |
216068 KB |
Output is correct |
105 |
Correct |
309 ms |
175976 KB |
Output is correct |
106 |
Correct |
134 ms |
155492 KB |
Output is correct |
107 |
Correct |
356 ms |
182452 KB |
Output is correct |
108 |
Correct |
212 ms |
159356 KB |
Output is correct |
109 |
Correct |
245 ms |
166736 KB |
Output is correct |