#include<bits/stdc++.h>
#define fi first
#define se second
#define pitem item*
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<ll> vl;
const int N=1e6+10;
const int SS=1<<19;
const int INFi=2e9;
const ll INFl=1e16;
const ll mod2=998244353;
const ll mod=1e9+7;
const ll mod3=2027865967;
const ll p=70032301;
const ull p2=913;
const int L=20;
int n,m;
vector<pair<int,int>> t[N];
vi t2[N];
pair<int,ll> jp[N][L];
pair<int,int> num[N];
map<pair<int,int>, ll> mm;
ll zn(int a,int b){
ll res=0,curr=0;
int g=0;
b--;
if(num[a].fi==b){
return t[b][num[a].se].se-t[b][num[a].se].fi;
}
for(int i=L-1;i>=0;i--){
if(num[jp[a][i].fi].fi>=b) res=curr+jp[a][i].se,g=jp[a][i].fi;
else{
curr+=jp[a][i].se;
a=jp[a][i].fi;
}
}
res+=t[b][num[g].se].se-t[b][num[g].se].fi;
return res;
}
void solve(){
cin>>n>>m;
vector<pair<int,int>> cr;
int li=0;
for(int i=1;i<n;i++){
int a;
cin>>a;
cr.clear();
while(a--){
int x,d;
cin>>x>>d;
cr.push_back({x,d});
}
sort(cr.begin(),cr.end());
reverse(cr.begin(),cr.end());
while(cr.size()){
while(t[i].size() and t[i].back().se>cr.back().se) t[i].pop_back();
t[i].push_back(cr.back());
cr.pop_back();
}
for(int i2=0;i2<t[i].size();i2++){
t2[i].push_back(++li);
num[li]={i,i2};
}
}
num[li+1].fi=n;
for(int i=0;i<L;i++) jp[li+1][i].fi=li+1;
for(int i=1;i<n;i++){
int curr=0;
for(int i2=0;i2<t[i].size();i2++){
while(curr<t[i+1].size() and t[i+1][curr].fi<t[i][i2].se) curr++;
ll xd=t[i][i2].se-t[i][i2].fi;
if(curr==t[i+1].size()){
if(i<n-1) xd+=m-t[i][i2].se+t[i+1][0].fi,jp[t2[i][i2]][0].fi=t2[i+1][0];
else jp[t2[i][i2]][0].fi=li+1;
}else{
xd+=t[i+1][curr].fi-t[i][i2].se;
jp[t2[i][i2]][0].fi=t2[i+1][curr];
}
jp[t2[i][i2]][0].se=xd;
}
}
for(int i2=1;i2<L;i2++){
for(int i=1;i<=li;i++){
int g=jp[i][i2].fi;
jp[i][i2].se=jp[i][i2-1].se+jp[jp[i][i2-1].fi][i2-1].se;
jp[i][i2].fi=jp[jp[i][i2-1].fi][i2-1].fi;
}
}
int q;
cin>>q;
for(int i=1;i<=q;i++){
int a,b;
cin>>a>>b;
if(mm.count(make_pair(a,b))>0){
cout<<mm[make_pair(a,b)]<<"\n";
continue;
}
ll res=INFl;
for(auto u:t2[a]) res=min(res,zn(u,b));
mm[make_pair(a,b)]=res;
cout<<res<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
int tt=1;
while(tt--) solve();
}
Compilation message
Main.cpp: In function 'void solve()':
Main.cpp:63:24: 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 i2=0;i2<t[i].size();i2++){
| ~~^~~~~~~~~~~~
Main.cpp:72:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i2=0;i2<t[i].size();i2++){
| ~~^~~~~~~~~~~~
Main.cpp:73: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]
73 | while(curr<t[i+1].size() and t[i+1][curr].fi<t[i][i2].se) curr++;
| ~~~~^~~~~~~~~~~~~~
Main.cpp:75:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | if(curr==t[i+1].size()){
| ~~~~^~~~~~~~~~~~~~~
Main.cpp:87:17: warning: unused variable 'g' [-Wunused-variable]
87 | int g=jp[i][i2].fi;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50012 KB |
Output is correct |
2 |
Correct |
43 ms |
53072 KB |
Output is correct |
3 |
Correct |
205 ms |
68436 KB |
Output is correct |
4 |
Correct |
252 ms |
76624 KB |
Output is correct |
5 |
Correct |
220 ms |
71124 KB |
Output is correct |
6 |
Correct |
305 ms |
76224 KB |
Output is correct |
7 |
Correct |
288 ms |
76164 KB |
Output is correct |
8 |
Correct |
194 ms |
68176 KB |
Output is correct |
9 |
Correct |
355 ms |
76276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50012 KB |
Output is correct |
2 |
Correct |
43 ms |
53072 KB |
Output is correct |
3 |
Correct |
205 ms |
68436 KB |
Output is correct |
4 |
Correct |
252 ms |
76624 KB |
Output is correct |
5 |
Correct |
220 ms |
71124 KB |
Output is correct |
6 |
Correct |
305 ms |
76224 KB |
Output is correct |
7 |
Correct |
288 ms |
76164 KB |
Output is correct |
8 |
Correct |
194 ms |
68176 KB |
Output is correct |
9 |
Correct |
355 ms |
76276 KB |
Output is correct |
10 |
Correct |
8 ms |
50008 KB |
Output is correct |
11 |
Correct |
225 ms |
62904 KB |
Output is correct |
12 |
Correct |
206 ms |
62800 KB |
Output is correct |
13 |
Correct |
211 ms |
62728 KB |
Output is correct |
14 |
Correct |
223 ms |
62800 KB |
Output is correct |
15 |
Correct |
285 ms |
69464 KB |
Output is correct |
16 |
Correct |
46 ms |
54096 KB |
Output is correct |
17 |
Correct |
289 ms |
75204 KB |
Output is correct |
18 |
Correct |
288 ms |
70072 KB |
Output is correct |
19 |
Correct |
314 ms |
74580 KB |
Output is correct |
20 |
Correct |
303 ms |
66784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50012 KB |
Output is correct |
2 |
Correct |
43 ms |
53072 KB |
Output is correct |
3 |
Correct |
205 ms |
68436 KB |
Output is correct |
4 |
Correct |
252 ms |
76624 KB |
Output is correct |
5 |
Correct |
220 ms |
71124 KB |
Output is correct |
6 |
Correct |
305 ms |
76224 KB |
Output is correct |
7 |
Correct |
288 ms |
76164 KB |
Output is correct |
8 |
Correct |
194 ms |
68176 KB |
Output is correct |
9 |
Correct |
355 ms |
76276 KB |
Output is correct |
10 |
Correct |
477 ms |
100536 KB |
Output is correct |
11 |
Correct |
802 ms |
116844 KB |
Output is correct |
12 |
Correct |
721 ms |
116816 KB |
Output is correct |
13 |
Correct |
532 ms |
111432 KB |
Output is correct |
14 |
Correct |
692 ms |
117984 KB |
Output is correct |
15 |
Correct |
675 ms |
118176 KB |
Output is correct |
16 |
Correct |
493 ms |
101852 KB |
Output is correct |
17 |
Correct |
680 ms |
118096 KB |
Output is correct |
18 |
Correct |
219 ms |
96292 KB |
Output is correct |
19 |
Correct |
174 ms |
95132 KB |
Output is correct |
20 |
Correct |
255 ms |
96452 KB |
Output is correct |
21 |
Correct |
214 ms |
96596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50012 KB |
Output is correct |
2 |
Correct |
43 ms |
53072 KB |
Output is correct |
3 |
Correct |
205 ms |
68436 KB |
Output is correct |
4 |
Correct |
252 ms |
76624 KB |
Output is correct |
5 |
Correct |
220 ms |
71124 KB |
Output is correct |
6 |
Correct |
305 ms |
76224 KB |
Output is correct |
7 |
Correct |
288 ms |
76164 KB |
Output is correct |
8 |
Correct |
194 ms |
68176 KB |
Output is correct |
9 |
Correct |
355 ms |
76276 KB |
Output is correct |
10 |
Correct |
8 ms |
50008 KB |
Output is correct |
11 |
Correct |
225 ms |
62904 KB |
Output is correct |
12 |
Correct |
206 ms |
62800 KB |
Output is correct |
13 |
Correct |
211 ms |
62728 KB |
Output is correct |
14 |
Correct |
223 ms |
62800 KB |
Output is correct |
15 |
Correct |
285 ms |
69464 KB |
Output is correct |
16 |
Correct |
46 ms |
54096 KB |
Output is correct |
17 |
Correct |
289 ms |
75204 KB |
Output is correct |
18 |
Correct |
288 ms |
70072 KB |
Output is correct |
19 |
Correct |
314 ms |
74580 KB |
Output is correct |
20 |
Correct |
303 ms |
66784 KB |
Output is correct |
21 |
Correct |
477 ms |
100536 KB |
Output is correct |
22 |
Correct |
802 ms |
116844 KB |
Output is correct |
23 |
Correct |
721 ms |
116816 KB |
Output is correct |
24 |
Correct |
532 ms |
111432 KB |
Output is correct |
25 |
Correct |
692 ms |
117984 KB |
Output is correct |
26 |
Correct |
675 ms |
118176 KB |
Output is correct |
27 |
Correct |
493 ms |
101852 KB |
Output is correct |
28 |
Correct |
680 ms |
118096 KB |
Output is correct |
29 |
Correct |
219 ms |
96292 KB |
Output is correct |
30 |
Correct |
174 ms |
95132 KB |
Output is correct |
31 |
Correct |
255 ms |
96452 KB |
Output is correct |
32 |
Correct |
214 ms |
96596 KB |
Output is correct |
33 |
Correct |
1279 ms |
113408 KB |
Output is correct |
34 |
Correct |
1190 ms |
113488 KB |
Output is correct |
35 |
Correct |
925 ms |
110728 KB |
Output is correct |
36 |
Correct |
950 ms |
110672 KB |
Output is correct |
37 |
Correct |
718 ms |
111944 KB |
Output is correct |
38 |
Correct |
443 ms |
100468 KB |
Output is correct |
39 |
Correct |
552 ms |
111316 KB |
Output is correct |
40 |
Correct |
539 ms |
99920 KB |
Output is correct |
41 |
Correct |
514 ms |
99920 KB |
Output is correct |
42 |
Correct |
768 ms |
113684 KB |
Output is correct |
43 |
Correct |
616 ms |
100948 KB |
Output is correct |
44 |
Correct |
619 ms |
105556 KB |
Output is correct |
45 |
Correct |
197 ms |
94288 KB |
Output is correct |
46 |
Correct |
173 ms |
92668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
50012 KB |
Output is correct |
2 |
Correct |
7 ms |
50012 KB |
Output is correct |
3 |
Correct |
1834 ms |
86452 KB |
Output is correct |
4 |
Correct |
1849 ms |
88868 KB |
Output is correct |
5 |
Correct |
1900 ms |
88916 KB |
Output is correct |
6 |
Correct |
1858 ms |
88940 KB |
Output is correct |
7 |
Correct |
1871 ms |
88732 KB |
Output is correct |
8 |
Correct |
853 ms |
73660 KB |
Output is correct |
9 |
Correct |
1652 ms |
89172 KB |
Output is correct |
10 |
Correct |
41 ms |
70864 KB |
Output is correct |
11 |
Correct |
212 ms |
96340 KB |
Output is correct |
12 |
Correct |
200 ms |
94304 KB |
Output is correct |
13 |
Correct |
205 ms |
92696 KB |
Output is correct |
14 |
Correct |
131 ms |
90648 KB |
Output is correct |
15 |
Correct |
152 ms |
89924 KB |
Output is correct |
16 |
Correct |
191 ms |
92796 KB |
Output is correct |
17 |
Correct |
172 ms |
92240 KB |
Output is correct |
18 |
Correct |
404 ms |
89428 KB |
Output is correct |
19 |
Correct |
1020 ms |
83380 KB |
Output is correct |
20 |
Correct |
162 ms |
89684 KB |
Output is correct |
21 |
Correct |
197 ms |
91728 KB |
Output is correct |
22 |
Correct |
420 ms |
88480 KB |
Output is correct |
23 |
Correct |
444 ms |
90568 KB |
Output is correct |
24 |
Execution timed out |
3024 ms |
86136 KB |
Time limit exceeded |
25 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
50012 KB |
Output is correct |
2 |
Correct |
43 ms |
53072 KB |
Output is correct |
3 |
Correct |
205 ms |
68436 KB |
Output is correct |
4 |
Correct |
252 ms |
76624 KB |
Output is correct |
5 |
Correct |
220 ms |
71124 KB |
Output is correct |
6 |
Correct |
305 ms |
76224 KB |
Output is correct |
7 |
Correct |
288 ms |
76164 KB |
Output is correct |
8 |
Correct |
194 ms |
68176 KB |
Output is correct |
9 |
Correct |
355 ms |
76276 KB |
Output is correct |
10 |
Correct |
8 ms |
50008 KB |
Output is correct |
11 |
Correct |
225 ms |
62904 KB |
Output is correct |
12 |
Correct |
206 ms |
62800 KB |
Output is correct |
13 |
Correct |
211 ms |
62728 KB |
Output is correct |
14 |
Correct |
223 ms |
62800 KB |
Output is correct |
15 |
Correct |
285 ms |
69464 KB |
Output is correct |
16 |
Correct |
46 ms |
54096 KB |
Output is correct |
17 |
Correct |
289 ms |
75204 KB |
Output is correct |
18 |
Correct |
288 ms |
70072 KB |
Output is correct |
19 |
Correct |
314 ms |
74580 KB |
Output is correct |
20 |
Correct |
303 ms |
66784 KB |
Output is correct |
21 |
Correct |
477 ms |
100536 KB |
Output is correct |
22 |
Correct |
802 ms |
116844 KB |
Output is correct |
23 |
Correct |
721 ms |
116816 KB |
Output is correct |
24 |
Correct |
532 ms |
111432 KB |
Output is correct |
25 |
Correct |
692 ms |
117984 KB |
Output is correct |
26 |
Correct |
675 ms |
118176 KB |
Output is correct |
27 |
Correct |
493 ms |
101852 KB |
Output is correct |
28 |
Correct |
680 ms |
118096 KB |
Output is correct |
29 |
Correct |
219 ms |
96292 KB |
Output is correct |
30 |
Correct |
174 ms |
95132 KB |
Output is correct |
31 |
Correct |
255 ms |
96452 KB |
Output is correct |
32 |
Correct |
214 ms |
96596 KB |
Output is correct |
33 |
Correct |
1279 ms |
113408 KB |
Output is correct |
34 |
Correct |
1190 ms |
113488 KB |
Output is correct |
35 |
Correct |
925 ms |
110728 KB |
Output is correct |
36 |
Correct |
950 ms |
110672 KB |
Output is correct |
37 |
Correct |
718 ms |
111944 KB |
Output is correct |
38 |
Correct |
443 ms |
100468 KB |
Output is correct |
39 |
Correct |
552 ms |
111316 KB |
Output is correct |
40 |
Correct |
539 ms |
99920 KB |
Output is correct |
41 |
Correct |
514 ms |
99920 KB |
Output is correct |
42 |
Correct |
768 ms |
113684 KB |
Output is correct |
43 |
Correct |
616 ms |
100948 KB |
Output is correct |
44 |
Correct |
619 ms |
105556 KB |
Output is correct |
45 |
Correct |
197 ms |
94288 KB |
Output is correct |
46 |
Correct |
173 ms |
92668 KB |
Output is correct |
47 |
Correct |
7 ms |
50012 KB |
Output is correct |
48 |
Correct |
7 ms |
50012 KB |
Output is correct |
49 |
Correct |
1834 ms |
86452 KB |
Output is correct |
50 |
Correct |
1849 ms |
88868 KB |
Output is correct |
51 |
Correct |
1900 ms |
88916 KB |
Output is correct |
52 |
Correct |
1858 ms |
88940 KB |
Output is correct |
53 |
Correct |
1871 ms |
88732 KB |
Output is correct |
54 |
Correct |
853 ms |
73660 KB |
Output is correct |
55 |
Correct |
1652 ms |
89172 KB |
Output is correct |
56 |
Correct |
41 ms |
70864 KB |
Output is correct |
57 |
Correct |
212 ms |
96340 KB |
Output is correct |
58 |
Correct |
200 ms |
94304 KB |
Output is correct |
59 |
Correct |
205 ms |
92696 KB |
Output is correct |
60 |
Correct |
131 ms |
90648 KB |
Output is correct |
61 |
Correct |
152 ms |
89924 KB |
Output is correct |
62 |
Correct |
191 ms |
92796 KB |
Output is correct |
63 |
Correct |
172 ms |
92240 KB |
Output is correct |
64 |
Correct |
404 ms |
89428 KB |
Output is correct |
65 |
Correct |
1020 ms |
83380 KB |
Output is correct |
66 |
Correct |
162 ms |
89684 KB |
Output is correct |
67 |
Correct |
197 ms |
91728 KB |
Output is correct |
68 |
Correct |
420 ms |
88480 KB |
Output is correct |
69 |
Correct |
444 ms |
90568 KB |
Output is correct |
70 |
Execution timed out |
3024 ms |
86136 KB |
Time limit exceeded |
71 |
Halted |
0 ms |
0 KB |
- |