#include<bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
const int maxn=100000+10,maxq=300000+10,lg=20,sq=100,msq=maxn/sq+10;
int t,n,q;
long long inf=1e16;
vector<pair<int,int>>all[maxn];
vector<int>dp[maxn],lnk[lg][maxn],allp;
vector<long long>wlnk[lg][maxn];
long long res[msq][maxn];
vector<long long>allz[maxn];
void vorod(){
cin>>n>>t;
for(int i=1;i<n;i++){
int d;
cin>>d;
all[i].resize(d);
if(d>=sq){
allp.push_back(i);
}
for(int j=0;j<d;j++){
cin>>all[i][j].first>>all[i][j].second;
}
sort(all[i].begin(),all[i].end());
dp[i].resize(d);
for(int j=0;j<lg;j++){
lnk[j][i].resize(d);
wlnk[j][i].resize(d);
}
allz[i].resize(d);
}
cin>>q;
}
void callnk(){
for(int i=1;i<lg;i++){
for(int j=1;j<n-1;j++){
if((1<<i)+j>n){
continue;
}
for(int h=0;h<(int)all[j].size();h++){
lnk[i][j][h]=lnk[i-1][j+(1<<(i-1))][lnk[i-1][j][h]];
wlnk[i][j][h]=wlnk[i-1][j][h]+wlnk[i-1][j+(1<<(i-1))][lnk[i-1][j][h]];
}
}
}
}
void calresp(int z){
for(int i=1;i<n-1;i++){
for(int j=0;j<(int)all[i].size();j++){
//cout<<i<<" "<<j<<" "<<z<<" "<<allz[i][j]<<" "<<((all[i+1][lnk[0][i][j]].first-all[i][j].first)+t)%t<<"\n";
allz[i+1][lnk[0][i][j]]=min(allz[i+1][lnk[0][i][j]],allz[i][j]+wlnk[0][i][j]);
res[z][i+1]=min(res[z][i+1],allz[i][j]+dp[i][j]-all[i][j].first);
}
}
int i=n-1;
for(int j=0;j<(int)all[n-1].size();j++){
//cout<<z<<" "<<i<<" "<<j<<" "<<allz[i][j]<<" "<<dp[i][j]<<" wtf"<<endl;
res[z][i+1]=min(res[z][i+1],allz[i][j]+dp[i][j]-all[i][j].first);
}
}
void calp(){
int ted=0;
for(int i=1;i<n;i++){
if((int)all[i].size()<sq){
continue;
}
for(int j=1;j<=n;j++){
for(int h=0;h<(int)all[j].size();h++){
allz[j][h]=inf;
}
res[ted][j]=inf;
}
for(int j=0;j<(int)all[i].size();j++){
allz[i][j]=0;
}
res[ted][i]=0;
calresp(ted);
ted++;
}
}
void pre(){
for(int i=n-1;i>=1;i--){
int mn=t;
for(int j=(int)all[i].size()-1;j>=0;j--){
mn=min(all[i][j].second,mn);
dp[i][j]=mn;
}
}
for(int i=n-2;i>=1;i--){
for(int j=0;j<(int)all[i].size();j++){
if(all[i+1].back().first<dp[i][j]){
lnk[0][i][j]=0;
wlnk[0][i][j]=t-all[i][j].first+all[i+1][0].first;
continue;
}
int p=lower_bound(all[i+1].begin(),all[i+1].end(),make_pair(dp[i][j],-1))-all[i+1].begin();
lnk[0][i][j]=p;
wlnk[0][i][j]=all[i+1][p].first-all[i][j].first;
}
}
calp();
callnk();
}
long long boro(int ind,int r,int p){
long long ret=0;
if(ind==r){
return 0;
}
int dis=r-ind-1;
for(int i=lg-1;i>=0;i--){
if((dis>>i)&1){
ret+=wlnk[i][ind][p];
p=lnk[i][ind][p];
ind+=(1<<i);
}
}
ret+=dp[ind][p]-all[ind][p].first;
return ret;
}
void solve(){
for(int i=0;i<q;i++){
int l,r;
cin>>l>>r;
if((int)all[l].size()>=sq){
int p=lower_bound(allp.begin(),allp.end(),l)-allp.begin();
cout<<res[p][r]<<"\n";
continue;
}
long long ret=inf;
for(int i=0;i<(int)all[l].size();i++){
ret=min(ret,boro(l,r,i));
}
cout<<ret<<"\n";
}
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
vorod();
pre();
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
101312 KB |
Output is correct |
2 |
Correct |
111 ms |
105568 KB |
Output is correct |
3 |
Correct |
125 ms |
108116 KB |
Output is correct |
4 |
Correct |
154 ms |
110504 KB |
Output is correct |
5 |
Correct |
129 ms |
109176 KB |
Output is correct |
6 |
Correct |
155 ms |
110680 KB |
Output is correct |
7 |
Correct |
159 ms |
110680 KB |
Output is correct |
8 |
Correct |
126 ms |
108580 KB |
Output is correct |
9 |
Correct |
152 ms |
110672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
101312 KB |
Output is correct |
2 |
Correct |
111 ms |
105568 KB |
Output is correct |
3 |
Correct |
125 ms |
108116 KB |
Output is correct |
4 |
Correct |
154 ms |
110504 KB |
Output is correct |
5 |
Correct |
129 ms |
109176 KB |
Output is correct |
6 |
Correct |
155 ms |
110680 KB |
Output is correct |
7 |
Correct |
159 ms |
110680 KB |
Output is correct |
8 |
Correct |
126 ms |
108580 KB |
Output is correct |
9 |
Correct |
152 ms |
110672 KB |
Output is correct |
10 |
Correct |
56 ms |
101204 KB |
Output is correct |
11 |
Correct |
168 ms |
107780 KB |
Output is correct |
12 |
Correct |
157 ms |
107804 KB |
Output is correct |
13 |
Correct |
157 ms |
107584 KB |
Output is correct |
14 |
Correct |
156 ms |
107600 KB |
Output is correct |
15 |
Correct |
162 ms |
108368 KB |
Output is correct |
16 |
Correct |
117 ms |
105436 KB |
Output is correct |
17 |
Correct |
152 ms |
109392 KB |
Output is correct |
18 |
Correct |
153 ms |
108368 KB |
Output is correct |
19 |
Correct |
152 ms |
109344 KB |
Output is correct |
20 |
Correct |
165 ms |
108392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
101312 KB |
Output is correct |
2 |
Correct |
111 ms |
105568 KB |
Output is correct |
3 |
Correct |
125 ms |
108116 KB |
Output is correct |
4 |
Correct |
154 ms |
110504 KB |
Output is correct |
5 |
Correct |
129 ms |
109176 KB |
Output is correct |
6 |
Correct |
155 ms |
110680 KB |
Output is correct |
7 |
Correct |
159 ms |
110680 KB |
Output is correct |
8 |
Correct |
126 ms |
108580 KB |
Output is correct |
9 |
Correct |
152 ms |
110672 KB |
Output is correct |
10 |
Correct |
976 ms |
214640 KB |
Output is correct |
11 |
Correct |
1396 ms |
246000 KB |
Output is correct |
12 |
Correct |
1313 ms |
246120 KB |
Output is correct |
13 |
Correct |
1010 ms |
244056 KB |
Output is correct |
14 |
Correct |
1032 ms |
245844 KB |
Output is correct |
15 |
Correct |
1285 ms |
246108 KB |
Output is correct |
16 |
Correct |
865 ms |
214872 KB |
Output is correct |
17 |
Correct |
1141 ms |
246008 KB |
Output is correct |
18 |
Correct |
744 ms |
226896 KB |
Output is correct |
19 |
Correct |
608 ms |
226276 KB |
Output is correct |
20 |
Correct |
717 ms |
227076 KB |
Output is correct |
21 |
Correct |
605 ms |
226768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
101312 KB |
Output is correct |
2 |
Correct |
111 ms |
105568 KB |
Output is correct |
3 |
Correct |
125 ms |
108116 KB |
Output is correct |
4 |
Correct |
154 ms |
110504 KB |
Output is correct |
5 |
Correct |
129 ms |
109176 KB |
Output is correct |
6 |
Correct |
155 ms |
110680 KB |
Output is correct |
7 |
Correct |
159 ms |
110680 KB |
Output is correct |
8 |
Correct |
126 ms |
108580 KB |
Output is correct |
9 |
Correct |
152 ms |
110672 KB |
Output is correct |
10 |
Correct |
56 ms |
101204 KB |
Output is correct |
11 |
Correct |
168 ms |
107780 KB |
Output is correct |
12 |
Correct |
157 ms |
107804 KB |
Output is correct |
13 |
Correct |
157 ms |
107584 KB |
Output is correct |
14 |
Correct |
156 ms |
107600 KB |
Output is correct |
15 |
Correct |
162 ms |
108368 KB |
Output is correct |
16 |
Correct |
117 ms |
105436 KB |
Output is correct |
17 |
Correct |
152 ms |
109392 KB |
Output is correct |
18 |
Correct |
153 ms |
108368 KB |
Output is correct |
19 |
Correct |
152 ms |
109344 KB |
Output is correct |
20 |
Correct |
165 ms |
108392 KB |
Output is correct |
21 |
Correct |
976 ms |
214640 KB |
Output is correct |
22 |
Correct |
1396 ms |
246000 KB |
Output is correct |
23 |
Correct |
1313 ms |
246120 KB |
Output is correct |
24 |
Correct |
1010 ms |
244056 KB |
Output is correct |
25 |
Correct |
1032 ms |
245844 KB |
Output is correct |
26 |
Correct |
1285 ms |
246108 KB |
Output is correct |
27 |
Correct |
865 ms |
214872 KB |
Output is correct |
28 |
Correct |
1141 ms |
246008 KB |
Output is correct |
29 |
Correct |
744 ms |
226896 KB |
Output is correct |
30 |
Correct |
608 ms |
226276 KB |
Output is correct |
31 |
Correct |
717 ms |
227076 KB |
Output is correct |
32 |
Correct |
605 ms |
226768 KB |
Output is correct |
33 |
Correct |
737 ms |
144536 KB |
Output is correct |
34 |
Correct |
713 ms |
144140 KB |
Output is correct |
35 |
Correct |
736 ms |
144216 KB |
Output is correct |
36 |
Correct |
595 ms |
144156 KB |
Output is correct |
37 |
Correct |
899 ms |
156308 KB |
Output is correct |
38 |
Correct |
767 ms |
179136 KB |
Output is correct |
39 |
Correct |
1042 ms |
200892 KB |
Output is correct |
40 |
Correct |
756 ms |
143164 KB |
Output is correct |
41 |
Correct |
801 ms |
176120 KB |
Output is correct |
42 |
Correct |
1103 ms |
200784 KB |
Output is correct |
43 |
Correct |
950 ms |
158268 KB |
Output is correct |
44 |
Correct |
783 ms |
160336 KB |
Output is correct |
45 |
Correct |
587 ms |
186332 KB |
Output is correct |
46 |
Correct |
490 ms |
150096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
101204 KB |
Output is correct |
2 |
Correct |
58 ms |
101204 KB |
Output is correct |
3 |
Correct |
181 ms |
130128 KB |
Output is correct |
4 |
Correct |
172 ms |
130132 KB |
Output is correct |
5 |
Correct |
175 ms |
129876 KB |
Output is correct |
6 |
Correct |
179 ms |
130116 KB |
Output is correct |
7 |
Correct |
169 ms |
130008 KB |
Output is correct |
8 |
Correct |
177 ms |
130132 KB |
Output is correct |
9 |
Correct |
168 ms |
129616 KB |
Output is correct |
10 |
Correct |
94 ms |
126832 KB |
Output is correct |
11 |
Correct |
730 ms |
227156 KB |
Output is correct |
12 |
Correct |
534 ms |
186448 KB |
Output is correct |
13 |
Correct |
480 ms |
150024 KB |
Output is correct |
14 |
Correct |
318 ms |
142812 KB |
Output is correct |
15 |
Correct |
358 ms |
142692 KB |
Output is correct |
16 |
Correct |
472 ms |
171456 KB |
Output is correct |
17 |
Correct |
333 ms |
146392 KB |
Output is correct |
18 |
Correct |
208 ms |
128588 KB |
Output is correct |
19 |
Correct |
213 ms |
131000 KB |
Output is correct |
20 |
Correct |
1268 ms |
279380 KB |
Output is correct |
21 |
Correct |
1228 ms |
279348 KB |
Output is correct |
22 |
Correct |
732 ms |
225616 KB |
Output is correct |
23 |
Correct |
782 ms |
224056 KB |
Output is correct |
24 |
Correct |
312 ms |
162136 KB |
Output is correct |
25 |
Correct |
286 ms |
162616 KB |
Output is correct |
26 |
Correct |
327 ms |
164684 KB |
Output is correct |
27 |
Correct |
640 ms |
226456 KB |
Output is correct |
28 |
Correct |
596 ms |
226828 KB |
Output is correct |
29 |
Correct |
633 ms |
227040 KB |
Output is correct |
30 |
Correct |
158 ms |
132180 KB |
Output is correct |
31 |
Correct |
122 ms |
127568 KB |
Output is correct |
32 |
Correct |
273 ms |
129020 KB |
Output is correct |
33 |
Correct |
178 ms |
127828 KB |
Output is correct |
34 |
Correct |
224 ms |
130900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
101312 KB |
Output is correct |
2 |
Correct |
111 ms |
105568 KB |
Output is correct |
3 |
Correct |
125 ms |
108116 KB |
Output is correct |
4 |
Correct |
154 ms |
110504 KB |
Output is correct |
5 |
Correct |
129 ms |
109176 KB |
Output is correct |
6 |
Correct |
155 ms |
110680 KB |
Output is correct |
7 |
Correct |
159 ms |
110680 KB |
Output is correct |
8 |
Correct |
126 ms |
108580 KB |
Output is correct |
9 |
Correct |
152 ms |
110672 KB |
Output is correct |
10 |
Correct |
56 ms |
101204 KB |
Output is correct |
11 |
Correct |
168 ms |
107780 KB |
Output is correct |
12 |
Correct |
157 ms |
107804 KB |
Output is correct |
13 |
Correct |
157 ms |
107584 KB |
Output is correct |
14 |
Correct |
156 ms |
107600 KB |
Output is correct |
15 |
Correct |
162 ms |
108368 KB |
Output is correct |
16 |
Correct |
117 ms |
105436 KB |
Output is correct |
17 |
Correct |
152 ms |
109392 KB |
Output is correct |
18 |
Correct |
153 ms |
108368 KB |
Output is correct |
19 |
Correct |
152 ms |
109344 KB |
Output is correct |
20 |
Correct |
165 ms |
108392 KB |
Output is correct |
21 |
Correct |
976 ms |
214640 KB |
Output is correct |
22 |
Correct |
1396 ms |
246000 KB |
Output is correct |
23 |
Correct |
1313 ms |
246120 KB |
Output is correct |
24 |
Correct |
1010 ms |
244056 KB |
Output is correct |
25 |
Correct |
1032 ms |
245844 KB |
Output is correct |
26 |
Correct |
1285 ms |
246108 KB |
Output is correct |
27 |
Correct |
865 ms |
214872 KB |
Output is correct |
28 |
Correct |
1141 ms |
246008 KB |
Output is correct |
29 |
Correct |
744 ms |
226896 KB |
Output is correct |
30 |
Correct |
608 ms |
226276 KB |
Output is correct |
31 |
Correct |
717 ms |
227076 KB |
Output is correct |
32 |
Correct |
605 ms |
226768 KB |
Output is correct |
33 |
Correct |
737 ms |
144536 KB |
Output is correct |
34 |
Correct |
713 ms |
144140 KB |
Output is correct |
35 |
Correct |
736 ms |
144216 KB |
Output is correct |
36 |
Correct |
595 ms |
144156 KB |
Output is correct |
37 |
Correct |
899 ms |
156308 KB |
Output is correct |
38 |
Correct |
767 ms |
179136 KB |
Output is correct |
39 |
Correct |
1042 ms |
200892 KB |
Output is correct |
40 |
Correct |
756 ms |
143164 KB |
Output is correct |
41 |
Correct |
801 ms |
176120 KB |
Output is correct |
42 |
Correct |
1103 ms |
200784 KB |
Output is correct |
43 |
Correct |
950 ms |
158268 KB |
Output is correct |
44 |
Correct |
783 ms |
160336 KB |
Output is correct |
45 |
Correct |
587 ms |
186332 KB |
Output is correct |
46 |
Correct |
490 ms |
150096 KB |
Output is correct |
47 |
Correct |
56 ms |
101204 KB |
Output is correct |
48 |
Correct |
58 ms |
101204 KB |
Output is correct |
49 |
Correct |
181 ms |
130128 KB |
Output is correct |
50 |
Correct |
172 ms |
130132 KB |
Output is correct |
51 |
Correct |
175 ms |
129876 KB |
Output is correct |
52 |
Correct |
179 ms |
130116 KB |
Output is correct |
53 |
Correct |
169 ms |
130008 KB |
Output is correct |
54 |
Correct |
177 ms |
130132 KB |
Output is correct |
55 |
Correct |
168 ms |
129616 KB |
Output is correct |
56 |
Correct |
94 ms |
126832 KB |
Output is correct |
57 |
Correct |
730 ms |
227156 KB |
Output is correct |
58 |
Correct |
534 ms |
186448 KB |
Output is correct |
59 |
Correct |
480 ms |
150024 KB |
Output is correct |
60 |
Correct |
318 ms |
142812 KB |
Output is correct |
61 |
Correct |
358 ms |
142692 KB |
Output is correct |
62 |
Correct |
472 ms |
171456 KB |
Output is correct |
63 |
Correct |
333 ms |
146392 KB |
Output is correct |
64 |
Correct |
208 ms |
128588 KB |
Output is correct |
65 |
Correct |
213 ms |
131000 KB |
Output is correct |
66 |
Correct |
1268 ms |
279380 KB |
Output is correct |
67 |
Correct |
1228 ms |
279348 KB |
Output is correct |
68 |
Correct |
732 ms |
225616 KB |
Output is correct |
69 |
Correct |
782 ms |
224056 KB |
Output is correct |
70 |
Correct |
312 ms |
162136 KB |
Output is correct |
71 |
Correct |
286 ms |
162616 KB |
Output is correct |
72 |
Correct |
327 ms |
164684 KB |
Output is correct |
73 |
Correct |
640 ms |
226456 KB |
Output is correct |
74 |
Correct |
596 ms |
226828 KB |
Output is correct |
75 |
Correct |
633 ms |
227040 KB |
Output is correct |
76 |
Correct |
158 ms |
132180 KB |
Output is correct |
77 |
Correct |
122 ms |
127568 KB |
Output is correct |
78 |
Correct |
273 ms |
129020 KB |
Output is correct |
79 |
Correct |
178 ms |
127828 KB |
Output is correct |
80 |
Correct |
224 ms |
130900 KB |
Output is correct |
81 |
Correct |
288 ms |
139456 KB |
Output is correct |
82 |
Correct |
287 ms |
139344 KB |
Output is correct |
83 |
Correct |
291 ms |
139404 KB |
Output is correct |
84 |
Correct |
284 ms |
139232 KB |
Output is correct |
85 |
Correct |
287 ms |
139164 KB |
Output is correct |
86 |
Correct |
312 ms |
139780 KB |
Output is correct |
87 |
Correct |
259 ms |
137704 KB |
Output is correct |
88 |
Correct |
378 ms |
143700 KB |
Output is correct |
89 |
Correct |
357 ms |
143324 KB |
Output is correct |
90 |
Correct |
425 ms |
146168 KB |
Output is correct |
91 |
Correct |
130 ms |
131156 KB |
Output is correct |
92 |
Correct |
533 ms |
153344 KB |
Output is correct |
93 |
Correct |
498 ms |
151888 KB |
Output is correct |
94 |
Correct |
484 ms |
153168 KB |
Output is correct |
95 |
Correct |
651 ms |
156304 KB |
Output is correct |
96 |
Correct |
385 ms |
136180 KB |
Output is correct |
97 |
Correct |
338 ms |
138324 KB |
Output is correct |
98 |
Correct |
1477 ms |
310400 KB |
Output is correct |
99 |
Correct |
1643 ms |
309684 KB |
Output is correct |
100 |
Correct |
861 ms |
238456 KB |
Output is correct |
101 |
Correct |
1006 ms |
236924 KB |
Output is correct |
102 |
Correct |
457 ms |
169296 KB |
Output is correct |
103 |
Correct |
383 ms |
169456 KB |
Output is correct |
104 |
Correct |
491 ms |
168272 KB |
Output is correct |
105 |
Correct |
220 ms |
133828 KB |
Output is correct |
106 |
Correct |
197 ms |
132480 KB |
Output is correct |
107 |
Correct |
600 ms |
133304 KB |
Output is correct |
108 |
Correct |
382 ms |
131920 KB |
Output is correct |
109 |
Correct |
375 ms |
134860 KB |
Output is correct |