#include<bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
template <typename T> bool chkmin(T &x,T y){return x>y?x=y,1:0;}
template <typename T> bool chkmax(T &x,T y){return x<y?x=y,1:0;}
ll readint(){
ll x=0,f=1; char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int blk=210;
int n,t,q;
int m[100005];
vector<pii> a[100005];
vector<int> nxt[100005];
vector<int> idx[100005][18];
vector<ll> jmp[100005][18];
ll ans[300005],bst[100005];
vector<pii> qs[100005];
vector<ll> dist[100005];
int lg2[100005];
vector<bool> ok[100005];
ll lift(int x,int y,int d){
ll ret=0;
while(d){
ret+=jmp[x][lg2[d]][y];
y=idx[x][lg2[d]][y];
x+=(1<<lg2[d]);
d-=(1<<lg2[d]);
}
return ret;
}
int main(){
n=readint(); t=readint();
for(int i=1;i<n;i++){
m[i]=readint();
a[i].resize(m[i]);
for(int j=0;j<m[i];j++){
a[i][j].fi=readint();
a[i][j].se=readint();
}
sort(a[i].begin(),a[i].end());
}
for(int i=1;i<n;i++){
nxt[i]=vector<int>(m[i],-1);
for(int j=0;j<18;j++){
jmp[i][j]=vector<ll>(m[i],0);
idx[i][j]=vector<int>(m[i],0);
}
ok[i]=vector<bool>(m[i],true);
if(i+1==n) break;
vector<int> ord(m[i]);
iota(ord.begin(),ord.end(),0);
sort(ord.begin(),ord.end(),[&](int x,int y){
if(a[i][x].se!=a[i][y].se) return a[i][x].se<a[i][y].se;
else return a[i][x].fi<a[i][y].fi;
});
int sz=m[i+1];
vector<int> mn(sz);
mn[sz-1]=sz-1;
for(int j=sz-2;j>=0;j--){
mn[j]=(a[i+1][j].se<=a[i+1][mn[j+1]].se?j:mn[j+1]);
}
int ptr=0;
for(int j=0;j<m[i];j++){
while(ptr<m[i+1]&&a[i+1][ptr].fi<a[i][ord[j]].se) ptr++;
nxt[i][ord[j]]=(ptr==m[i+1]?mn[0]:mn[ptr]);
jmp[i][0][ord[j]]=(a[i][ord[j]].se<=a[i+1][nxt[i][ord[j]]].fi?a[i+1][nxt[i][ord[j]]].fi-a[i][ord[j]].se:t-a[i][ord[j]].se+a[i+1][nxt[i][ord[j]]].fi)+(a[i+1][nxt[i][ord[j]]].se-a[i+1][nxt[i][ord[j]]].fi);
idx[i][0][ord[j]]=nxt[i][ord[j]];
}
for(int j=0;j<m[i];j++) ord[j]=j;
sort(ord.begin(),ord.end(),[&](int x,int y){
if(nxt[i][x]!=nxt[i][y]) return nxt[i][x]<nxt[i][y];
else return jmp[i][0][x]+a[i][x].se-a[i][x].fi<jmp[i][0][y]+a[i][y].se-a[i][y].fi;
});
for(int j=0;j<m[i];j++){
int ptr=j;
while(ptr+1<m[i]&&nxt[i][ord[j]]==nxt[i][ord[ptr+1]]) ptr++;
for(int r=j+1;r<=ptr;r++) ok[i][ord[r]]=false;
j=ptr;
}
}
for(int i=2;i<=n;i++) lg2[i]=lg2[i>>1]+1;
for(int k=1;k<18;k++){
for(int i=1;i+1+(1<<k)<=n;i++){
for(int j=0;j<m[i];j++){
jmp[i][k][j]=jmp[i][k-1][j]+jmp[i+(1<<(k-1))][k-1][idx[i][k-1][j]];
idx[i][k][j]=idx[i+(1<<(k-1))][k-1][idx[i][k-1][j]];
}
}
}
q=readint();
for(int i=1;i<=q;i++){
int l=readint(),r=readint();
--r;
qs[l].pb(mp(r,i));
}
for(int i=1;i<n;i++){
dist[i].resize(m[i]);
}
for(int i=1;i<=n;i++){
if(m[i]<=blk){
for(auto p:qs[i]){
ans[p.se]=(ll)1e18;
if(p.fi==i){
for(int j=0;j<m[i];j++) chkmin(ans[p.se],(ll)a[i][j].se-a[i][j].fi);
}
}
for(int j=0;j<m[i];j++){
if(!ok[i][j]) continue;
for(auto p:qs[i]) ans[p.se]=min(ans[p.se],a[i][j].se-a[i][j].fi+lift(i,j,p.fi-i));
}
continue;
}
int mx=0;
for(auto p:qs[i]) chkmax(mx,p.fi);
for(int j=i;j<=mx;j++){
for(int k=0;k<m[j];k++){
dist[j][k]=(ll)1e18;
}
bst[j]=(ll)1e18;
}
priority_queue<pair<ll,pii>> pq;
for(int j=0;j<m[i];j++){
dist[i][j]=a[i][j].se-a[i][j].fi;
pq.push(mp(-dist[i][j],mp(i,j)));
}
while(!pq.empty()){
pair<ll,pii> p=pq.top();
pq.pop();
if(dist[p.se.fi][p.se.se]!=-p.fi) continue;
int x=p.se.fi,y=p.se.se;
chkmin(bst[x],dist[x][y]);
if(x>=mx) continue;
if(dist[x+1][nxt[x][y]]>dist[x][y]+jmp[x][0][y]){
dist[x+1][nxt[x][y]]=dist[x][y]+jmp[x][0][y];
pq.push(mp(-dist[x+1][nxt[x][y]],mp(x+1,nxt[x][y])));
}
}
for(auto p:qs[i]) ans[p.se]=bst[p.fi];
}
for(int i=1;i<=q;i++) printf("%lld\n",ans[i]);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
101816 KB |
Output is correct |
2 |
Correct |
64 ms |
108272 KB |
Output is correct |
3 |
Correct |
77 ms |
111500 KB |
Output is correct |
4 |
Correct |
76 ms |
114780 KB |
Output is correct |
5 |
Correct |
81 ms |
112468 KB |
Output is correct |
6 |
Correct |
80 ms |
114836 KB |
Output is correct |
7 |
Correct |
89 ms |
114968 KB |
Output is correct |
8 |
Correct |
63 ms |
111584 KB |
Output is correct |
9 |
Correct |
78 ms |
114712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
101816 KB |
Output is correct |
2 |
Correct |
64 ms |
108272 KB |
Output is correct |
3 |
Correct |
77 ms |
111500 KB |
Output is correct |
4 |
Correct |
76 ms |
114780 KB |
Output is correct |
5 |
Correct |
81 ms |
112468 KB |
Output is correct |
6 |
Correct |
80 ms |
114836 KB |
Output is correct |
7 |
Correct |
89 ms |
114968 KB |
Output is correct |
8 |
Correct |
63 ms |
111584 KB |
Output is correct |
9 |
Correct |
78 ms |
114712 KB |
Output is correct |
10 |
Correct |
23 ms |
101724 KB |
Output is correct |
11 |
Correct |
83 ms |
111844 KB |
Output is correct |
12 |
Correct |
86 ms |
111632 KB |
Output is correct |
13 |
Correct |
86 ms |
111740 KB |
Output is correct |
14 |
Correct |
83 ms |
111952 KB |
Output is correct |
15 |
Correct |
78 ms |
112280 KB |
Output is correct |
16 |
Correct |
59 ms |
109352 KB |
Output is correct |
17 |
Correct |
79 ms |
113576 KB |
Output is correct |
18 |
Correct |
71 ms |
112164 KB |
Output is correct |
19 |
Correct |
71 ms |
113492 KB |
Output is correct |
20 |
Correct |
84 ms |
112472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
101816 KB |
Output is correct |
2 |
Correct |
64 ms |
108272 KB |
Output is correct |
3 |
Correct |
77 ms |
111500 KB |
Output is correct |
4 |
Correct |
76 ms |
114780 KB |
Output is correct |
5 |
Correct |
81 ms |
112468 KB |
Output is correct |
6 |
Correct |
80 ms |
114836 KB |
Output is correct |
7 |
Correct |
89 ms |
114968 KB |
Output is correct |
8 |
Correct |
63 ms |
111584 KB |
Output is correct |
9 |
Correct |
78 ms |
114712 KB |
Output is correct |
10 |
Correct |
449 ms |
210916 KB |
Output is correct |
11 |
Correct |
533 ms |
241688 KB |
Output is correct |
12 |
Correct |
589 ms |
241768 KB |
Output is correct |
13 |
Correct |
531 ms |
238964 KB |
Output is correct |
14 |
Correct |
550 ms |
241968 KB |
Output is correct |
15 |
Correct |
576 ms |
241684 KB |
Output is correct |
16 |
Correct |
415 ms |
211020 KB |
Output is correct |
17 |
Correct |
616 ms |
242016 KB |
Output is correct |
18 |
Correct |
332 ms |
220768 KB |
Output is correct |
19 |
Correct |
353 ms |
220304 KB |
Output is correct |
20 |
Correct |
320 ms |
220688 KB |
Output is correct |
21 |
Correct |
336 ms |
220712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
101816 KB |
Output is correct |
2 |
Correct |
64 ms |
108272 KB |
Output is correct |
3 |
Correct |
77 ms |
111500 KB |
Output is correct |
4 |
Correct |
76 ms |
114780 KB |
Output is correct |
5 |
Correct |
81 ms |
112468 KB |
Output is correct |
6 |
Correct |
80 ms |
114836 KB |
Output is correct |
7 |
Correct |
89 ms |
114968 KB |
Output is correct |
8 |
Correct |
63 ms |
111584 KB |
Output is correct |
9 |
Correct |
78 ms |
114712 KB |
Output is correct |
10 |
Correct |
23 ms |
101724 KB |
Output is correct |
11 |
Correct |
83 ms |
111844 KB |
Output is correct |
12 |
Correct |
86 ms |
111632 KB |
Output is correct |
13 |
Correct |
86 ms |
111740 KB |
Output is correct |
14 |
Correct |
83 ms |
111952 KB |
Output is correct |
15 |
Correct |
78 ms |
112280 KB |
Output is correct |
16 |
Correct |
59 ms |
109352 KB |
Output is correct |
17 |
Correct |
79 ms |
113576 KB |
Output is correct |
18 |
Correct |
71 ms |
112164 KB |
Output is correct |
19 |
Correct |
71 ms |
113492 KB |
Output is correct |
20 |
Correct |
84 ms |
112472 KB |
Output is correct |
21 |
Correct |
449 ms |
210916 KB |
Output is correct |
22 |
Correct |
533 ms |
241688 KB |
Output is correct |
23 |
Correct |
589 ms |
241768 KB |
Output is correct |
24 |
Correct |
531 ms |
238964 KB |
Output is correct |
25 |
Correct |
550 ms |
241968 KB |
Output is correct |
26 |
Correct |
576 ms |
241684 KB |
Output is correct |
27 |
Correct |
415 ms |
211020 KB |
Output is correct |
28 |
Correct |
616 ms |
242016 KB |
Output is correct |
29 |
Correct |
332 ms |
220768 KB |
Output is correct |
30 |
Correct |
353 ms |
220304 KB |
Output is correct |
31 |
Correct |
320 ms |
220688 KB |
Output is correct |
32 |
Correct |
336 ms |
220712 KB |
Output is correct |
33 |
Correct |
325 ms |
145824 KB |
Output is correct |
34 |
Correct |
364 ms |
145844 KB |
Output is correct |
35 |
Correct |
303 ms |
145644 KB |
Output is correct |
36 |
Correct |
294 ms |
145644 KB |
Output is correct |
37 |
Correct |
349 ms |
157420 KB |
Output is correct |
38 |
Correct |
327 ms |
178040 KB |
Output is correct |
39 |
Correct |
394 ms |
198920 KB |
Output is correct |
40 |
Correct |
223 ms |
144976 KB |
Output is correct |
41 |
Correct |
331 ms |
175440 KB |
Output is correct |
42 |
Correct |
413 ms |
199248 KB |
Output is correct |
43 |
Correct |
288 ms |
158480 KB |
Output is correct |
44 |
Correct |
312 ms |
161036 KB |
Output is correct |
45 |
Correct |
265 ms |
183120 KB |
Output is correct |
46 |
Correct |
169 ms |
148564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
101724 KB |
Output is correct |
2 |
Correct |
22 ms |
101828 KB |
Output is correct |
3 |
Correct |
881 ms |
127220 KB |
Output is correct |
4 |
Correct |
880 ms |
127316 KB |
Output is correct |
5 |
Correct |
1006 ms |
127088 KB |
Output is correct |
6 |
Correct |
1030 ms |
127076 KB |
Output is correct |
7 |
Correct |
1033 ms |
127068 KB |
Output is correct |
8 |
Correct |
97 ms |
127316 KB |
Output is correct |
9 |
Correct |
76 ms |
127316 KB |
Output is correct |
10 |
Correct |
63 ms |
127560 KB |
Output is correct |
11 |
Correct |
353 ms |
220844 KB |
Output is correct |
12 |
Correct |
267 ms |
182892 KB |
Output is correct |
13 |
Correct |
171 ms |
148504 KB |
Output is correct |
14 |
Correct |
129 ms |
140788 KB |
Output is correct |
15 |
Correct |
132 ms |
140808 KB |
Output is correct |
16 |
Correct |
125 ms |
141028 KB |
Output is correct |
17 |
Correct |
146 ms |
140792 KB |
Output is correct |
18 |
Correct |
93 ms |
128140 KB |
Output is correct |
19 |
Correct |
136 ms |
127512 KB |
Output is correct |
20 |
Correct |
491 ms |
193656 KB |
Output is correct |
21 |
Correct |
420 ms |
194104 KB |
Output is correct |
22 |
Correct |
399 ms |
158768 KB |
Output is correct |
23 |
Correct |
420 ms |
158868 KB |
Output is correct |
24 |
Correct |
172 ms |
159268 KB |
Output is correct |
25 |
Correct |
153 ms |
158848 KB |
Output is correct |
26 |
Correct |
183 ms |
159664 KB |
Output is correct |
27 |
Correct |
321 ms |
220192 KB |
Output is correct |
28 |
Correct |
343 ms |
220816 KB |
Output is correct |
29 |
Correct |
320 ms |
220936 KB |
Output is correct |
30 |
Correct |
109 ms |
131424 KB |
Output is correct |
31 |
Correct |
86 ms |
126820 KB |
Output is correct |
32 |
Correct |
100 ms |
128592 KB |
Output is correct |
33 |
Correct |
92 ms |
127504 KB |
Output is correct |
34 |
Correct |
97 ms |
127328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
101816 KB |
Output is correct |
2 |
Correct |
64 ms |
108272 KB |
Output is correct |
3 |
Correct |
77 ms |
111500 KB |
Output is correct |
4 |
Correct |
76 ms |
114780 KB |
Output is correct |
5 |
Correct |
81 ms |
112468 KB |
Output is correct |
6 |
Correct |
80 ms |
114836 KB |
Output is correct |
7 |
Correct |
89 ms |
114968 KB |
Output is correct |
8 |
Correct |
63 ms |
111584 KB |
Output is correct |
9 |
Correct |
78 ms |
114712 KB |
Output is correct |
10 |
Correct |
23 ms |
101724 KB |
Output is correct |
11 |
Correct |
83 ms |
111844 KB |
Output is correct |
12 |
Correct |
86 ms |
111632 KB |
Output is correct |
13 |
Correct |
86 ms |
111740 KB |
Output is correct |
14 |
Correct |
83 ms |
111952 KB |
Output is correct |
15 |
Correct |
78 ms |
112280 KB |
Output is correct |
16 |
Correct |
59 ms |
109352 KB |
Output is correct |
17 |
Correct |
79 ms |
113576 KB |
Output is correct |
18 |
Correct |
71 ms |
112164 KB |
Output is correct |
19 |
Correct |
71 ms |
113492 KB |
Output is correct |
20 |
Correct |
84 ms |
112472 KB |
Output is correct |
21 |
Correct |
449 ms |
210916 KB |
Output is correct |
22 |
Correct |
533 ms |
241688 KB |
Output is correct |
23 |
Correct |
589 ms |
241768 KB |
Output is correct |
24 |
Correct |
531 ms |
238964 KB |
Output is correct |
25 |
Correct |
550 ms |
241968 KB |
Output is correct |
26 |
Correct |
576 ms |
241684 KB |
Output is correct |
27 |
Correct |
415 ms |
211020 KB |
Output is correct |
28 |
Correct |
616 ms |
242016 KB |
Output is correct |
29 |
Correct |
332 ms |
220768 KB |
Output is correct |
30 |
Correct |
353 ms |
220304 KB |
Output is correct |
31 |
Correct |
320 ms |
220688 KB |
Output is correct |
32 |
Correct |
336 ms |
220712 KB |
Output is correct |
33 |
Correct |
325 ms |
145824 KB |
Output is correct |
34 |
Correct |
364 ms |
145844 KB |
Output is correct |
35 |
Correct |
303 ms |
145644 KB |
Output is correct |
36 |
Correct |
294 ms |
145644 KB |
Output is correct |
37 |
Correct |
349 ms |
157420 KB |
Output is correct |
38 |
Correct |
327 ms |
178040 KB |
Output is correct |
39 |
Correct |
394 ms |
198920 KB |
Output is correct |
40 |
Correct |
223 ms |
144976 KB |
Output is correct |
41 |
Correct |
331 ms |
175440 KB |
Output is correct |
42 |
Correct |
413 ms |
199248 KB |
Output is correct |
43 |
Correct |
288 ms |
158480 KB |
Output is correct |
44 |
Correct |
312 ms |
161036 KB |
Output is correct |
45 |
Correct |
265 ms |
183120 KB |
Output is correct |
46 |
Correct |
169 ms |
148564 KB |
Output is correct |
47 |
Correct |
22 ms |
101724 KB |
Output is correct |
48 |
Correct |
22 ms |
101828 KB |
Output is correct |
49 |
Correct |
881 ms |
127220 KB |
Output is correct |
50 |
Correct |
880 ms |
127316 KB |
Output is correct |
51 |
Correct |
1006 ms |
127088 KB |
Output is correct |
52 |
Correct |
1030 ms |
127076 KB |
Output is correct |
53 |
Correct |
1033 ms |
127068 KB |
Output is correct |
54 |
Correct |
97 ms |
127316 KB |
Output is correct |
55 |
Correct |
76 ms |
127316 KB |
Output is correct |
56 |
Correct |
63 ms |
127560 KB |
Output is correct |
57 |
Correct |
353 ms |
220844 KB |
Output is correct |
58 |
Correct |
267 ms |
182892 KB |
Output is correct |
59 |
Correct |
171 ms |
148504 KB |
Output is correct |
60 |
Correct |
129 ms |
140788 KB |
Output is correct |
61 |
Correct |
132 ms |
140808 KB |
Output is correct |
62 |
Correct |
125 ms |
141028 KB |
Output is correct |
63 |
Correct |
146 ms |
140792 KB |
Output is correct |
64 |
Correct |
93 ms |
128140 KB |
Output is correct |
65 |
Correct |
136 ms |
127512 KB |
Output is correct |
66 |
Correct |
491 ms |
193656 KB |
Output is correct |
67 |
Correct |
420 ms |
194104 KB |
Output is correct |
68 |
Correct |
399 ms |
158768 KB |
Output is correct |
69 |
Correct |
420 ms |
158868 KB |
Output is correct |
70 |
Correct |
172 ms |
159268 KB |
Output is correct |
71 |
Correct |
153 ms |
158848 KB |
Output is correct |
72 |
Correct |
183 ms |
159664 KB |
Output is correct |
73 |
Correct |
321 ms |
220192 KB |
Output is correct |
74 |
Correct |
343 ms |
220816 KB |
Output is correct |
75 |
Correct |
320 ms |
220936 KB |
Output is correct |
76 |
Correct |
109 ms |
131424 KB |
Output is correct |
77 |
Correct |
86 ms |
126820 KB |
Output is correct |
78 |
Correct |
100 ms |
128592 KB |
Output is correct |
79 |
Correct |
92 ms |
127504 KB |
Output is correct |
80 |
Correct |
97 ms |
127328 KB |
Output is correct |
81 |
Correct |
942 ms |
135612 KB |
Output is correct |
82 |
Correct |
1047 ms |
135608 KB |
Output is correct |
83 |
Correct |
1110 ms |
135324 KB |
Output is correct |
84 |
Correct |
952 ms |
135336 KB |
Output is correct |
85 |
Correct |
962 ms |
135544 KB |
Output is correct |
86 |
Correct |
353 ms |
135852 KB |
Output is correct |
87 |
Correct |
105 ms |
136452 KB |
Output is correct |
88 |
Correct |
831 ms |
136160 KB |
Output is correct |
89 |
Correct |
795 ms |
136272 KB |
Output is correct |
90 |
Correct |
303 ms |
136528 KB |
Output is correct |
91 |
Correct |
88 ms |
134320 KB |
Output is correct |
92 |
Correct |
212 ms |
152772 KB |
Output is correct |
93 |
Correct |
223 ms |
152520 KB |
Output is correct |
94 |
Correct |
206 ms |
152548 KB |
Output is correct |
95 |
Correct |
266 ms |
152012 KB |
Output is correct |
96 |
Correct |
186 ms |
137164 KB |
Output is correct |
97 |
Correct |
315 ms |
136020 KB |
Output is correct |
98 |
Correct |
1211 ms |
202480 KB |
Output is correct |
99 |
Correct |
986 ms |
203348 KB |
Output is correct |
100 |
Correct |
723 ms |
168516 KB |
Output is correct |
101 |
Correct |
684 ms |
168696 KB |
Output is correct |
102 |
Correct |
223 ms |
169232 KB |
Output is correct |
103 |
Correct |
190 ms |
167724 KB |
Output is correct |
104 |
Correct |
229 ms |
168256 KB |
Output is correct |
105 |
Correct |
130 ms |
139160 KB |
Output is correct |
106 |
Correct |
141 ms |
134548 KB |
Output is correct |
107 |
Correct |
171 ms |
137736 KB |
Output is correct |
108 |
Correct |
148 ms |
136008 KB |
Output is correct |
109 |
Correct |
154 ms |
136020 KB |
Output is correct |