// Judges with GCC >= 12 only needs Ofast
// #pragma GCC optimize("O3,no-stack-protector,fast-math,unroll-loops,tree-vectorize")
// MLE optimization
// #pragma GCC optimize("conserve-stack")
// Old judges
// #pragma GCC target("sse4.2,popcnt,lzcnt,abm,mmx,fma,bmi,bmi2")
// New judges. Test with assert(__builtin_cpu_supports("avx2"));
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma,tune=native")
// Atcoder
// #pragma GCC target("avx2,popcnt,lzcnt,abm,bmi,bmi2,fma")
/*
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- insert(x),erase(x)
- find_by_order(k): return iterator to the k-th smallest element
- order_of_key(x): the number of elements that are strictly smaller
*/
#include<bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
uniform_real_distribution<> pp(0.0,1.0);
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pil pair<int,ll>
#define piii pair<int,pii>
#define mpp make_pair
#define fi first
#define se second
const ll inf=1e18;
const int mod=998244353;
const int maxn=100005;
const int B=50;
const int maxs=655;
const int maxm=200005;
const int maxq=300005;
const int maxl=25;
const int maxa=1000000;
const int root=3;
const int base=101;
int N,T,Q,M[maxn];
vector<pii> P[maxn];
vector<int> jmp[maxn];
vector<pil> tree[4*maxn];
void build(int l,int r,int id){
if(l==r){
for(int i=0;i<M[l];i++) tree[id].push_back({i,P[l][i].se-P[l][i].fi});
return;
}
int mid=(l+r)>>1;
build(l,mid,id<<1);build(mid+1,r,id<<1|1);
for(int i=0;i<M[l];i++){
auto [p,val]=tree[id<<1][i];
int cc=P[mid][p].se,pos=jmp[mid][p];
int nxt=P[mid+1][pos].fi;
tree[id].push_back({tree[id<<1|1][pos].fi,val+(nxt-cc+T)%T+tree[id<<1|1][pos].se});
}
}
pil query(int l,int r,int id,int tl,int tr,pil cur){
if(tr<l || r<tl) return cur;
if(tl<=l && r<=tr){
auto [p,val]=cur;
int cc=P[l-1][p].se,pos=jmp[l-1][p];
int nxt=P[l][pos].fi;
return {tree[id][pos].fi,val+(nxt-cc+T)%T+tree[id][pos].se};
}
int mid=(l+r)>>1;
return query(mid+1,r,id<<1|1,tl,tr,query(l,mid,id<<1,tl,tr,cur));
}
int f[maxn],cnt;
ll D[2005][maxn];
void build_ans(int i){
int t=f[i];
vector<ll> cur(M[i]);
D[t][i]=inf;
for(int j=0;j<M[i];j++){
cur[j]=P[i][j].se-P[i][j].fi;
D[t][i]=min(D[t][i],cur[j]);
}
for(int r=i+1;r<N;r++){
D[t][r]=inf;
vector<ll> nw(M[r],inf);
for(int j=0;j<M[r-1];j++){
if(cur[j]==inf) continue;
int cc=P[r-1][j].se,pos=jmp[r-1][j];
int nxt=P[r][pos].fi;
nw[pos]=min(nw[pos],cur[j]+(nxt-cc+T)%T+P[r][pos].se-P[r][pos].fi);
}
swap(cur,nw);
for(int j=0;j<M[r];j++) D[t][r]=min(D[t][r],cur[j]);
}
}
void solve(){
cin >> N >> T;
for(int i=1;i<N;i++){
cin >> M[i];
P[i].resize(M[i]);
for(int j=0;j<M[i];j++) cin >> P[i][j].fi >> P[i][j].se;
sort(P[i].begin(),P[i].end());
vector<pii> cc;
for(auto [x,y]:P[i]){
while(!cc.empty() && cc.back().se>=y) cc.pop_back();
if(cc.empty() || cc.back().fi!=x) cc.push_back({x,y});
}
P[i]=cc;
M[i]=(int)cc.size();
if(M[i]>B) f[i]=++cnt;
}
for(int i=1;i<N-1;i++){
jmp[i].resize(M[i]);
for(int j=0;j<M[i];j++){
int cc=P[i][j].se;
int pos=lower_bound(P[i+1].begin(),P[i+1].end(),mpp(cc,0))-P[i+1].begin();
jmp[i][j]=pos%M[i+1];
}
}
for(int i=1;i<N;i++){
if(M[i]>B) build_ans(i);
}
build(1,N-1,1);
cin >> Q;
for(int i=1;i<=Q;i++){
int l,r;cin >> l >> r;r--;
if(M[l]<=B){
ll ans=inf;
for(int j=0;j<M[l];j++){
if(l+1<=r) ans=min(ans,query(1,N-1,1,l+1,r,{j,P[l][j].se-P[l][j].fi}).se);
else ans=min(ans,1LL*(P[l][j].se-P[l][j].fi));
}
cout << ans << '\n';
}
else cout << D[f[l]][r] << '\n';
}
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;//cin >> test;
while(test--) solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
41 ms |
21076 KB |
Output is correct |
3 |
Correct |
79 ms |
21844 KB |
Output is correct |
4 |
Correct |
102 ms |
23636 KB |
Output is correct |
5 |
Correct |
101 ms |
22196 KB |
Output is correct |
6 |
Correct |
106 ms |
23636 KB |
Output is correct |
7 |
Correct |
115 ms |
23632 KB |
Output is correct |
8 |
Correct |
80 ms |
22004 KB |
Output is correct |
9 |
Correct |
108 ms |
23636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
41 ms |
21076 KB |
Output is correct |
3 |
Correct |
79 ms |
21844 KB |
Output is correct |
4 |
Correct |
102 ms |
23636 KB |
Output is correct |
5 |
Correct |
101 ms |
22196 KB |
Output is correct |
6 |
Correct |
106 ms |
23636 KB |
Output is correct |
7 |
Correct |
115 ms |
23632 KB |
Output is correct |
8 |
Correct |
80 ms |
22004 KB |
Output is correct |
9 |
Correct |
108 ms |
23636 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
233 ms |
22560 KB |
Output is correct |
12 |
Correct |
183 ms |
22616 KB |
Output is correct |
13 |
Correct |
175 ms |
22696 KB |
Output is correct |
14 |
Correct |
198 ms |
22508 KB |
Output is correct |
15 |
Correct |
143 ms |
22864 KB |
Output is correct |
16 |
Correct |
38 ms |
20824 KB |
Output is correct |
17 |
Correct |
105 ms |
23376 KB |
Output is correct |
18 |
Correct |
140 ms |
22868 KB |
Output is correct |
19 |
Correct |
116 ms |
23224 KB |
Output is correct |
20 |
Correct |
160 ms |
22936 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
41 ms |
21076 KB |
Output is correct |
3 |
Correct |
79 ms |
21844 KB |
Output is correct |
4 |
Correct |
102 ms |
23636 KB |
Output is correct |
5 |
Correct |
101 ms |
22196 KB |
Output is correct |
6 |
Correct |
106 ms |
23636 KB |
Output is correct |
7 |
Correct |
115 ms |
23632 KB |
Output is correct |
8 |
Correct |
80 ms |
22004 KB |
Output is correct |
9 |
Correct |
108 ms |
23636 KB |
Output is correct |
10 |
Correct |
245 ms |
33848 KB |
Output is correct |
11 |
Correct |
354 ms |
39504 KB |
Output is correct |
12 |
Correct |
336 ms |
39408 KB |
Output is correct |
13 |
Correct |
255 ms |
37236 KB |
Output is correct |
14 |
Correct |
384 ms |
39248 KB |
Output is correct |
15 |
Correct |
311 ms |
39504 KB |
Output is correct |
16 |
Correct |
229 ms |
33712 KB |
Output is correct |
17 |
Correct |
325 ms |
39508 KB |
Output is correct |
18 |
Correct |
100 ms |
32336 KB |
Output is correct |
19 |
Correct |
127 ms |
32084 KB |
Output is correct |
20 |
Correct |
137 ms |
32336 KB |
Output is correct |
21 |
Correct |
137 ms |
32384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
41 ms |
21076 KB |
Output is correct |
3 |
Correct |
79 ms |
21844 KB |
Output is correct |
4 |
Correct |
102 ms |
23636 KB |
Output is correct |
5 |
Correct |
101 ms |
22196 KB |
Output is correct |
6 |
Correct |
106 ms |
23636 KB |
Output is correct |
7 |
Correct |
115 ms |
23632 KB |
Output is correct |
8 |
Correct |
80 ms |
22004 KB |
Output is correct |
9 |
Correct |
108 ms |
23636 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
233 ms |
22560 KB |
Output is correct |
12 |
Correct |
183 ms |
22616 KB |
Output is correct |
13 |
Correct |
175 ms |
22696 KB |
Output is correct |
14 |
Correct |
198 ms |
22508 KB |
Output is correct |
15 |
Correct |
143 ms |
22864 KB |
Output is correct |
16 |
Correct |
38 ms |
20824 KB |
Output is correct |
17 |
Correct |
105 ms |
23376 KB |
Output is correct |
18 |
Correct |
140 ms |
22868 KB |
Output is correct |
19 |
Correct |
116 ms |
23224 KB |
Output is correct |
20 |
Correct |
160 ms |
22936 KB |
Output is correct |
21 |
Correct |
245 ms |
33848 KB |
Output is correct |
22 |
Correct |
354 ms |
39504 KB |
Output is correct |
23 |
Correct |
336 ms |
39408 KB |
Output is correct |
24 |
Correct |
255 ms |
37236 KB |
Output is correct |
25 |
Correct |
384 ms |
39248 KB |
Output is correct |
26 |
Correct |
311 ms |
39504 KB |
Output is correct |
27 |
Correct |
229 ms |
33712 KB |
Output is correct |
28 |
Correct |
325 ms |
39508 KB |
Output is correct |
29 |
Correct |
100 ms |
32336 KB |
Output is correct |
30 |
Correct |
127 ms |
32084 KB |
Output is correct |
31 |
Correct |
137 ms |
32336 KB |
Output is correct |
32 |
Correct |
137 ms |
32384 KB |
Output is correct |
33 |
Correct |
414 ms |
33252 KB |
Output is correct |
34 |
Correct |
450 ms |
33108 KB |
Output is correct |
35 |
Correct |
439 ms |
31656 KB |
Output is correct |
36 |
Correct |
442 ms |
31568 KB |
Output is correct |
37 |
Correct |
347 ms |
33512 KB |
Output is correct |
38 |
Correct |
241 ms |
31516 KB |
Output is correct |
39 |
Correct |
297 ms |
35824 KB |
Output is correct |
40 |
Correct |
324 ms |
30288 KB |
Output is correct |
41 |
Correct |
217 ms |
31568 KB |
Output is correct |
42 |
Correct |
301 ms |
35664 KB |
Output is correct |
43 |
Correct |
297 ms |
30804 KB |
Output is correct |
44 |
Correct |
348 ms |
32228 KB |
Output is correct |
45 |
Correct |
163 ms |
29428 KB |
Output is correct |
46 |
Correct |
188 ms |
27372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16732 KB |
Output is correct |
2 |
Correct |
4 ms |
16732 KB |
Output is correct |
3 |
Correct |
108 ms |
200696 KB |
Output is correct |
4 |
Correct |
104 ms |
212868 KB |
Output is correct |
5 |
Correct |
108 ms |
198696 KB |
Output is correct |
6 |
Correct |
126 ms |
202652 KB |
Output is correct |
7 |
Correct |
105 ms |
200788 KB |
Output is correct |
8 |
Correct |
69 ms |
198228 KB |
Output is correct |
9 |
Correct |
66 ms |
144208 KB |
Output is correct |
10 |
Correct |
33 ms |
23032 KB |
Output is correct |
11 |
Correct |
113 ms |
32448 KB |
Output is correct |
12 |
Correct |
116 ms |
29500 KB |
Output is correct |
13 |
Correct |
137 ms |
27372 KB |
Output is correct |
14 |
Correct |
80 ms |
30800 KB |
Output is correct |
15 |
Correct |
100 ms |
30464 KB |
Output is correct |
16 |
Correct |
228 ms |
187216 KB |
Output is correct |
17 |
Correct |
140 ms |
50960 KB |
Output is correct |
18 |
Correct |
218 ms |
190408 KB |
Output is correct |
19 |
Correct |
111 ms |
182784 KB |
Output is correct |
20 |
Correct |
152 ms |
153340 KB |
Output is correct |
21 |
Correct |
189 ms |
153580 KB |
Output is correct |
22 |
Correct |
152 ms |
153376 KB |
Output is correct |
23 |
Correct |
241 ms |
153424 KB |
Output is correct |
24 |
Correct |
75 ms |
33896 KB |
Output is correct |
25 |
Correct |
54 ms |
33544 KB |
Output is correct |
26 |
Correct |
85 ms |
36992 KB |
Output is correct |
27 |
Correct |
100 ms |
31852 KB |
Output is correct |
28 |
Correct |
168 ms |
32344 KB |
Output is correct |
29 |
Correct |
125 ms |
32240 KB |
Output is correct |
30 |
Correct |
83 ms |
26196 KB |
Output is correct |
31 |
Correct |
62 ms |
28892 KB |
Output is correct |
32 |
Correct |
146 ms |
22096 KB |
Output is correct |
33 |
Correct |
89 ms |
24916 KB |
Output is correct |
34 |
Correct |
205 ms |
21260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
16728 KB |
Output is correct |
2 |
Correct |
41 ms |
21076 KB |
Output is correct |
3 |
Correct |
79 ms |
21844 KB |
Output is correct |
4 |
Correct |
102 ms |
23636 KB |
Output is correct |
5 |
Correct |
101 ms |
22196 KB |
Output is correct |
6 |
Correct |
106 ms |
23636 KB |
Output is correct |
7 |
Correct |
115 ms |
23632 KB |
Output is correct |
8 |
Correct |
80 ms |
22004 KB |
Output is correct |
9 |
Correct |
108 ms |
23636 KB |
Output is correct |
10 |
Correct |
3 ms |
16732 KB |
Output is correct |
11 |
Correct |
233 ms |
22560 KB |
Output is correct |
12 |
Correct |
183 ms |
22616 KB |
Output is correct |
13 |
Correct |
175 ms |
22696 KB |
Output is correct |
14 |
Correct |
198 ms |
22508 KB |
Output is correct |
15 |
Correct |
143 ms |
22864 KB |
Output is correct |
16 |
Correct |
38 ms |
20824 KB |
Output is correct |
17 |
Correct |
105 ms |
23376 KB |
Output is correct |
18 |
Correct |
140 ms |
22868 KB |
Output is correct |
19 |
Correct |
116 ms |
23224 KB |
Output is correct |
20 |
Correct |
160 ms |
22936 KB |
Output is correct |
21 |
Correct |
245 ms |
33848 KB |
Output is correct |
22 |
Correct |
354 ms |
39504 KB |
Output is correct |
23 |
Correct |
336 ms |
39408 KB |
Output is correct |
24 |
Correct |
255 ms |
37236 KB |
Output is correct |
25 |
Correct |
384 ms |
39248 KB |
Output is correct |
26 |
Correct |
311 ms |
39504 KB |
Output is correct |
27 |
Correct |
229 ms |
33712 KB |
Output is correct |
28 |
Correct |
325 ms |
39508 KB |
Output is correct |
29 |
Correct |
100 ms |
32336 KB |
Output is correct |
30 |
Correct |
127 ms |
32084 KB |
Output is correct |
31 |
Correct |
137 ms |
32336 KB |
Output is correct |
32 |
Correct |
137 ms |
32384 KB |
Output is correct |
33 |
Correct |
414 ms |
33252 KB |
Output is correct |
34 |
Correct |
450 ms |
33108 KB |
Output is correct |
35 |
Correct |
439 ms |
31656 KB |
Output is correct |
36 |
Correct |
442 ms |
31568 KB |
Output is correct |
37 |
Correct |
347 ms |
33512 KB |
Output is correct |
38 |
Correct |
241 ms |
31516 KB |
Output is correct |
39 |
Correct |
297 ms |
35824 KB |
Output is correct |
40 |
Correct |
324 ms |
30288 KB |
Output is correct |
41 |
Correct |
217 ms |
31568 KB |
Output is correct |
42 |
Correct |
301 ms |
35664 KB |
Output is correct |
43 |
Correct |
297 ms |
30804 KB |
Output is correct |
44 |
Correct |
348 ms |
32228 KB |
Output is correct |
45 |
Correct |
163 ms |
29428 KB |
Output is correct |
46 |
Correct |
188 ms |
27372 KB |
Output is correct |
47 |
Correct |
4 ms |
16732 KB |
Output is correct |
48 |
Correct |
4 ms |
16732 KB |
Output is correct |
49 |
Correct |
108 ms |
200696 KB |
Output is correct |
50 |
Correct |
104 ms |
212868 KB |
Output is correct |
51 |
Correct |
108 ms |
198696 KB |
Output is correct |
52 |
Correct |
126 ms |
202652 KB |
Output is correct |
53 |
Correct |
105 ms |
200788 KB |
Output is correct |
54 |
Correct |
69 ms |
198228 KB |
Output is correct |
55 |
Correct |
66 ms |
144208 KB |
Output is correct |
56 |
Correct |
33 ms |
23032 KB |
Output is correct |
57 |
Correct |
113 ms |
32448 KB |
Output is correct |
58 |
Correct |
116 ms |
29500 KB |
Output is correct |
59 |
Correct |
137 ms |
27372 KB |
Output is correct |
60 |
Correct |
80 ms |
30800 KB |
Output is correct |
61 |
Correct |
100 ms |
30464 KB |
Output is correct |
62 |
Correct |
228 ms |
187216 KB |
Output is correct |
63 |
Correct |
140 ms |
50960 KB |
Output is correct |
64 |
Correct |
218 ms |
190408 KB |
Output is correct |
65 |
Correct |
111 ms |
182784 KB |
Output is correct |
66 |
Correct |
152 ms |
153340 KB |
Output is correct |
67 |
Correct |
189 ms |
153580 KB |
Output is correct |
68 |
Correct |
152 ms |
153376 KB |
Output is correct |
69 |
Correct |
241 ms |
153424 KB |
Output is correct |
70 |
Correct |
75 ms |
33896 KB |
Output is correct |
71 |
Correct |
54 ms |
33544 KB |
Output is correct |
72 |
Correct |
85 ms |
36992 KB |
Output is correct |
73 |
Correct |
100 ms |
31852 KB |
Output is correct |
74 |
Correct |
168 ms |
32344 KB |
Output is correct |
75 |
Correct |
125 ms |
32240 KB |
Output is correct |
76 |
Correct |
83 ms |
26196 KB |
Output is correct |
77 |
Correct |
62 ms |
28892 KB |
Output is correct |
78 |
Correct |
146 ms |
22096 KB |
Output is correct |
79 |
Correct |
89 ms |
24916 KB |
Output is correct |
80 |
Correct |
205 ms |
21260 KB |
Output is correct |
81 |
Correct |
194 ms |
212304 KB |
Output is correct |
82 |
Correct |
196 ms |
212308 KB |
Output is correct |
83 |
Correct |
231 ms |
211980 KB |
Output is correct |
84 |
Correct |
202 ms |
189780 KB |
Output is correct |
85 |
Correct |
224 ms |
191748 KB |
Output is correct |
86 |
Correct |
153 ms |
189804 KB |
Output is correct |
87 |
Correct |
178 ms |
190804 KB |
Output is correct |
88 |
Correct |
273 ms |
212044 KB |
Output is correct |
89 |
Correct |
256 ms |
214300 KB |
Output is correct |
90 |
Correct |
140 ms |
209236 KB |
Output is correct |
91 |
Correct |
93 ms |
24876 KB |
Output is correct |
92 |
Correct |
231 ms |
36368 KB |
Output is correct |
93 |
Correct |
327 ms |
36748 KB |
Output is correct |
94 |
Correct |
431 ms |
233260 KB |
Output is correct |
95 |
Correct |
292 ms |
32336 KB |
Output is correct |
96 |
Correct |
601 ms |
214528 KB |
Output is correct |
97 |
Correct |
234 ms |
212304 KB |
Output is correct |
98 |
Correct |
231 ms |
194132 KB |
Output is correct |
99 |
Correct |
340 ms |
194364 KB |
Output is correct |
100 |
Correct |
214 ms |
67924 KB |
Output is correct |
101 |
Correct |
286 ms |
222224 KB |
Output is correct |
102 |
Correct |
184 ms |
39468 KB |
Output is correct |
103 |
Correct |
142 ms |
39640 KB |
Output is correct |
104 |
Correct |
195 ms |
33364 KB |
Output is correct |
105 |
Correct |
180 ms |
30496 KB |
Output is correct |
106 |
Correct |
182 ms |
24244 KB |
Output is correct |
107 |
Correct |
439 ms |
27092 KB |
Output is correct |
108 |
Correct |
288 ms |
22944 KB |
Output is correct |
109 |
Correct |
665 ms |
25776 KB |
Output is correct |