#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
const ll inf = numeric_limits<ll> :: max();
const int M = 1e5;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, D; cin>>n>>D;
vector<int> x(n);
vector<pii> t[n];
for (int i = 1; i < n; i++){
cin>>x[i];
for (int j = 0; j < x[i]; j++){
int a, b; cin>>a>>b;
t[i].pb({a, b});
}
}
for (int i = 1; i < n; i++){
sort(t[i].begin(), t[i].end());
vector<pii> nw;
for (int j = 0; j < x[i]; j++){
while (!nw.empty() && nw.back().ss >= t[i][j].ss){
nw.pop_back();
}
if (nw.empty() || nw.back().ff != t[i][j].ff){
nw.pb(t[i][j]);
}
}
t[i] = nw;
x[i] = (int) t[i].size();
}
vector<int> p(n);
for (int i = 1; i < n; i++){
p[i] = p[i - 1] + x[i];
}
int S = p.back();
vector<int> g(S);
vector<bool> e(S);
for (int i = 1; i + 1 < n; i++){
int k = x[i + 1] - 1, mn = 0;
for (int j = x[i] - 1; j >= 0; j--){
while (k >= 0 && t[i + 1][k].ff >= t[i][j].ss){
mn = k--;
}
g[p[i - 1] + j] = p[i] + mn;
e[p[i - 1] + j] = (t[i][j].ss > t[i + 1][mn].ff);
}
}
int lg = (int) log2(S);
pii sp[S][20];
for (int i = 0; i < S; i++) sp[i][0] = {g[i], e[i]};
for (int j = 1; j <= lg; j++){
for (int i = 0; i + (1 << j) <= S; i++){
sp[i][j] = {sp[sp[i][j - 1].ff][j - 1].ff, sp[i][j - 1].ss + sp[sp[i][j - 1].ff][j - 1].ss};
}
}
auto get = [&](int l, int r){
ll out = inf;
for (int i = 0; i < x[l]; i++){
int tt = r - l - 1, f = p[l - 1] + i, cc = 0;
for (int j = lg; j >= 0; j--){
int k = (1 << j);
if (tt >= k){
cc += sp[f][j].ss;
f = sp[f][j].ff;
tt -= k;
}
}
out = min(out, 1LL * D * cc + (t[r - 1][f - p[r - 2]].ss - t[l][i].ff));
}
return out;
};
const int A = 300;
vector<int> all;
pair<int, ll> st[M], nw[M];
ll out[M / A + 5][n + 1];
int s1 = 0, s2 = 0, cc = 0;
ll mn;
for (int i = 1; i < n; i++){
if (x[i] <= A) continue;
all.pb(i);
s1 = s2 = 0;
for (int j = 0; j < x[i]; j++){
st[s1++] = {p[i - 1] + j, t[i][j].ss - t[i][j].ff};
}
for (int j = i + 1; j <= n; j++){
mn = inf;
for (int k = 0; k < s1; k++){
mn = min(mn, st[k].ss);
}
out[cc][j] = mn;
if (j == n) break;
for (int k = 0; k < s1; k++){
auto &[x, y] = st[k];
y += D * e[x] + (t[j][g[x] - p[j - 1]].ss - t[j - 1][x - p[j - 2]].ss);
x = g[x];
}
s2 = 0;
int k = 0;
while (k < s1){
int h = k; mn = inf;
while (h < s1 && st[k].ff == st[h].ff){
mn = min(mn, st[h].ss);
h++;
}
nw[s2++] = {st[k].ff, mn};
k = h;
}
s1 = s2;
for (int i = 0; i < s1; i++){
st[i] = nw[i];
}
}
cc++;
}
vector<int> :: iterator it;
int q; cin>>q;
for (int i = 1; i <= q; i++){
int l, r; cin>>l>>r;
if (x[l] <= A){
cout<<get(l, r)<<"\n";
}
else {
it = lower_bound(all.begin(), all.end(), l);
int j = (int) (it - all.begin());
cout<<out[j][r]<<"\n";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3408 KB |
Output is correct |
2 |
Correct |
39 ms |
6480 KB |
Output is correct |
3 |
Correct |
61 ms |
10312 KB |
Output is correct |
4 |
Correct |
73 ms |
13140 KB |
Output is correct |
5 |
Correct |
61 ms |
12360 KB |
Output is correct |
6 |
Correct |
76 ms |
13140 KB |
Output is correct |
7 |
Correct |
74 ms |
13128 KB |
Output is correct |
8 |
Correct |
61 ms |
10312 KB |
Output is correct |
9 |
Correct |
77 ms |
13136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3408 KB |
Output is correct |
2 |
Correct |
39 ms |
6480 KB |
Output is correct |
3 |
Correct |
61 ms |
10312 KB |
Output is correct |
4 |
Correct |
73 ms |
13140 KB |
Output is correct |
5 |
Correct |
61 ms |
12360 KB |
Output is correct |
6 |
Correct |
76 ms |
13140 KB |
Output is correct |
7 |
Correct |
74 ms |
13128 KB |
Output is correct |
8 |
Correct |
61 ms |
10312 KB |
Output is correct |
9 |
Correct |
77 ms |
13136 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
74 ms |
8520 KB |
Output is correct |
12 |
Correct |
74 ms |
8296 KB |
Output is correct |
13 |
Correct |
81 ms |
8272 KB |
Output is correct |
14 |
Correct |
70 ms |
8264 KB |
Output is correct |
15 |
Correct |
70 ms |
9288 KB |
Output is correct |
16 |
Correct |
40 ms |
6484 KB |
Output is correct |
17 |
Correct |
69 ms |
11336 KB |
Output is correct |
18 |
Correct |
68 ms |
9288 KB |
Output is correct |
19 |
Correct |
75 ms |
11344 KB |
Output is correct |
20 |
Correct |
73 ms |
9288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3408 KB |
Output is correct |
2 |
Correct |
39 ms |
6480 KB |
Output is correct |
3 |
Correct |
61 ms |
10312 KB |
Output is correct |
4 |
Correct |
73 ms |
13140 KB |
Output is correct |
5 |
Correct |
61 ms |
12360 KB |
Output is correct |
6 |
Correct |
76 ms |
13140 KB |
Output is correct |
7 |
Correct |
74 ms |
13128 KB |
Output is correct |
8 |
Correct |
61 ms |
10312 KB |
Output is correct |
9 |
Correct |
77 ms |
13136 KB |
Output is correct |
10 |
Correct |
395 ms |
233032 KB |
Output is correct |
11 |
Correct |
565 ms |
294728 KB |
Output is correct |
12 |
Correct |
553 ms |
294960 KB |
Output is correct |
13 |
Correct |
526 ms |
293672 KB |
Output is correct |
14 |
Correct |
557 ms |
294856 KB |
Output is correct |
15 |
Correct |
596 ms |
294728 KB |
Output is correct |
16 |
Correct |
358 ms |
233032 KB |
Output is correct |
17 |
Correct |
447 ms |
294708 KB |
Output is correct |
18 |
Correct |
290 ms |
262984 KB |
Output is correct |
19 |
Correct |
280 ms |
262728 KB |
Output is correct |
20 |
Correct |
293 ms |
262984 KB |
Output is correct |
21 |
Correct |
324 ms |
262984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3408 KB |
Output is correct |
2 |
Correct |
39 ms |
6480 KB |
Output is correct |
3 |
Correct |
61 ms |
10312 KB |
Output is correct |
4 |
Correct |
73 ms |
13140 KB |
Output is correct |
5 |
Correct |
61 ms |
12360 KB |
Output is correct |
6 |
Correct |
76 ms |
13140 KB |
Output is correct |
7 |
Correct |
74 ms |
13128 KB |
Output is correct |
8 |
Correct |
61 ms |
10312 KB |
Output is correct |
9 |
Correct |
77 ms |
13136 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
74 ms |
8520 KB |
Output is correct |
12 |
Correct |
74 ms |
8296 KB |
Output is correct |
13 |
Correct |
81 ms |
8272 KB |
Output is correct |
14 |
Correct |
70 ms |
8264 KB |
Output is correct |
15 |
Correct |
70 ms |
9288 KB |
Output is correct |
16 |
Correct |
40 ms |
6484 KB |
Output is correct |
17 |
Correct |
69 ms |
11336 KB |
Output is correct |
18 |
Correct |
68 ms |
9288 KB |
Output is correct |
19 |
Correct |
75 ms |
11344 KB |
Output is correct |
20 |
Correct |
73 ms |
9288 KB |
Output is correct |
21 |
Correct |
395 ms |
233032 KB |
Output is correct |
22 |
Correct |
565 ms |
294728 KB |
Output is correct |
23 |
Correct |
553 ms |
294960 KB |
Output is correct |
24 |
Correct |
526 ms |
293672 KB |
Output is correct |
25 |
Correct |
557 ms |
294856 KB |
Output is correct |
26 |
Correct |
596 ms |
294728 KB |
Output is correct |
27 |
Correct |
358 ms |
233032 KB |
Output is correct |
28 |
Correct |
447 ms |
294708 KB |
Output is correct |
29 |
Correct |
290 ms |
262984 KB |
Output is correct |
30 |
Correct |
280 ms |
262728 KB |
Output is correct |
31 |
Correct |
293 ms |
262984 KB |
Output is correct |
32 |
Correct |
324 ms |
262984 KB |
Output is correct |
33 |
Correct |
349 ms |
78664 KB |
Output is correct |
34 |
Correct |
322 ms |
78516 KB |
Output is correct |
35 |
Correct |
283 ms |
76616 KB |
Output is correct |
36 |
Correct |
262 ms |
76752 KB |
Output is correct |
37 |
Correct |
266 ms |
104268 KB |
Output is correct |
38 |
Correct |
298 ms |
161108 KB |
Output is correct |
39 |
Correct |
411 ms |
204544 KB |
Output is correct |
40 |
Correct |
246 ms |
79388 KB |
Output is correct |
41 |
Correct |
248 ms |
151844 KB |
Output is correct |
42 |
Correct |
355 ms |
202288 KB |
Output is correct |
43 |
Correct |
228 ms |
107336 KB |
Output is correct |
44 |
Correct |
240 ms |
109836 KB |
Output is correct |
45 |
Correct |
226 ms |
182292 KB |
Output is correct |
46 |
Correct |
159 ms |
101032 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3580 KB |
Output is correct |
2 |
Correct |
3 ms |
3408 KB |
Output is correct |
3 |
Correct |
553 ms |
21124 KB |
Output is correct |
4 |
Correct |
514 ms |
20808 KB |
Output is correct |
5 |
Correct |
557 ms |
20868 KB |
Output is correct |
6 |
Correct |
532 ms |
20768 KB |
Output is correct |
7 |
Correct |
536 ms |
20736 KB |
Output is correct |
8 |
Correct |
238 ms |
13728 KB |
Output is correct |
9 |
Correct |
56 ms |
21064 KB |
Output is correct |
10 |
Correct |
41 ms |
12032 KB |
Output is correct |
11 |
Correct |
298 ms |
263012 KB |
Output is correct |
12 |
Correct |
241 ms |
182008 KB |
Output is correct |
13 |
Correct |
168 ms |
101192 KB |
Output is correct |
14 |
Correct |
115 ms |
71184 KB |
Output is correct |
15 |
Correct |
109 ms |
70912 KB |
Output is correct |
16 |
Correct |
116 ms |
71972 KB |
Output is correct |
17 |
Correct |
121 ms |
70984 KB |
Output is correct |
18 |
Correct |
93 ms |
23712 KB |
Output is correct |
19 |
Correct |
198 ms |
21068 KB |
Output is correct |
20 |
Correct |
462 ms |
197068 KB |
Output is correct |
21 |
Correct |
413 ms |
196884 KB |
Output is correct |
22 |
Correct |
515 ms |
108616 KB |
Output is correct |
23 |
Correct |
414 ms |
108620 KB |
Output is correct |
24 |
Correct |
136 ms |
108252 KB |
Output is correct |
25 |
Correct |
128 ms |
108500 KB |
Output is correct |
26 |
Correct |
144 ms |
108360 KB |
Output is correct |
27 |
Correct |
280 ms |
262900 KB |
Output is correct |
28 |
Correct |
304 ms |
262984 KB |
Output is correct |
29 |
Correct |
297 ms |
262880 KB |
Output is correct |
30 |
Correct |
62 ms |
21844 KB |
Output is correct |
31 |
Correct |
80 ms |
5448 KB |
Output is correct |
32 |
Correct |
65 ms |
14920 KB |
Output is correct |
33 |
Correct |
58 ms |
6328 KB |
Output is correct |
34 |
Correct |
65 ms |
8264 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
3408 KB |
Output is correct |
2 |
Correct |
39 ms |
6480 KB |
Output is correct |
3 |
Correct |
61 ms |
10312 KB |
Output is correct |
4 |
Correct |
73 ms |
13140 KB |
Output is correct |
5 |
Correct |
61 ms |
12360 KB |
Output is correct |
6 |
Correct |
76 ms |
13140 KB |
Output is correct |
7 |
Correct |
74 ms |
13128 KB |
Output is correct |
8 |
Correct |
61 ms |
10312 KB |
Output is correct |
9 |
Correct |
77 ms |
13136 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
74 ms |
8520 KB |
Output is correct |
12 |
Correct |
74 ms |
8296 KB |
Output is correct |
13 |
Correct |
81 ms |
8272 KB |
Output is correct |
14 |
Correct |
70 ms |
8264 KB |
Output is correct |
15 |
Correct |
70 ms |
9288 KB |
Output is correct |
16 |
Correct |
40 ms |
6484 KB |
Output is correct |
17 |
Correct |
69 ms |
11336 KB |
Output is correct |
18 |
Correct |
68 ms |
9288 KB |
Output is correct |
19 |
Correct |
75 ms |
11344 KB |
Output is correct |
20 |
Correct |
73 ms |
9288 KB |
Output is correct |
21 |
Correct |
395 ms |
233032 KB |
Output is correct |
22 |
Correct |
565 ms |
294728 KB |
Output is correct |
23 |
Correct |
553 ms |
294960 KB |
Output is correct |
24 |
Correct |
526 ms |
293672 KB |
Output is correct |
25 |
Correct |
557 ms |
294856 KB |
Output is correct |
26 |
Correct |
596 ms |
294728 KB |
Output is correct |
27 |
Correct |
358 ms |
233032 KB |
Output is correct |
28 |
Correct |
447 ms |
294708 KB |
Output is correct |
29 |
Correct |
290 ms |
262984 KB |
Output is correct |
30 |
Correct |
280 ms |
262728 KB |
Output is correct |
31 |
Correct |
293 ms |
262984 KB |
Output is correct |
32 |
Correct |
324 ms |
262984 KB |
Output is correct |
33 |
Correct |
349 ms |
78664 KB |
Output is correct |
34 |
Correct |
322 ms |
78516 KB |
Output is correct |
35 |
Correct |
283 ms |
76616 KB |
Output is correct |
36 |
Correct |
262 ms |
76752 KB |
Output is correct |
37 |
Correct |
266 ms |
104268 KB |
Output is correct |
38 |
Correct |
298 ms |
161108 KB |
Output is correct |
39 |
Correct |
411 ms |
204544 KB |
Output is correct |
40 |
Correct |
246 ms |
79388 KB |
Output is correct |
41 |
Correct |
248 ms |
151844 KB |
Output is correct |
42 |
Correct |
355 ms |
202288 KB |
Output is correct |
43 |
Correct |
228 ms |
107336 KB |
Output is correct |
44 |
Correct |
240 ms |
109836 KB |
Output is correct |
45 |
Correct |
226 ms |
182292 KB |
Output is correct |
46 |
Correct |
159 ms |
101032 KB |
Output is correct |
47 |
Correct |
3 ms |
3580 KB |
Output is correct |
48 |
Correct |
3 ms |
3408 KB |
Output is correct |
49 |
Correct |
553 ms |
21124 KB |
Output is correct |
50 |
Correct |
514 ms |
20808 KB |
Output is correct |
51 |
Correct |
557 ms |
20868 KB |
Output is correct |
52 |
Correct |
532 ms |
20768 KB |
Output is correct |
53 |
Correct |
536 ms |
20736 KB |
Output is correct |
54 |
Correct |
238 ms |
13728 KB |
Output is correct |
55 |
Correct |
56 ms |
21064 KB |
Output is correct |
56 |
Correct |
41 ms |
12032 KB |
Output is correct |
57 |
Correct |
298 ms |
263012 KB |
Output is correct |
58 |
Correct |
241 ms |
182008 KB |
Output is correct |
59 |
Correct |
168 ms |
101192 KB |
Output is correct |
60 |
Correct |
115 ms |
71184 KB |
Output is correct |
61 |
Correct |
109 ms |
70912 KB |
Output is correct |
62 |
Correct |
116 ms |
71972 KB |
Output is correct |
63 |
Correct |
121 ms |
70984 KB |
Output is correct |
64 |
Correct |
93 ms |
23712 KB |
Output is correct |
65 |
Correct |
198 ms |
21068 KB |
Output is correct |
66 |
Correct |
462 ms |
197068 KB |
Output is correct |
67 |
Correct |
413 ms |
196884 KB |
Output is correct |
68 |
Correct |
515 ms |
108616 KB |
Output is correct |
69 |
Correct |
414 ms |
108620 KB |
Output is correct |
70 |
Correct |
136 ms |
108252 KB |
Output is correct |
71 |
Correct |
128 ms |
108500 KB |
Output is correct |
72 |
Correct |
144 ms |
108360 KB |
Output is correct |
73 |
Correct |
280 ms |
262900 KB |
Output is correct |
74 |
Correct |
304 ms |
262984 KB |
Output is correct |
75 |
Correct |
297 ms |
262880 KB |
Output is correct |
76 |
Correct |
62 ms |
21844 KB |
Output is correct |
77 |
Correct |
80 ms |
5448 KB |
Output is correct |
78 |
Correct |
65 ms |
14920 KB |
Output is correct |
79 |
Correct |
58 ms |
6328 KB |
Output is correct |
80 |
Correct |
65 ms |
8264 KB |
Output is correct |
81 |
Correct |
1147 ms |
25176 KB |
Output is correct |
82 |
Correct |
1127 ms |
25100 KB |
Output is correct |
83 |
Correct |
1303 ms |
25032 KB |
Output is correct |
84 |
Correct |
1217 ms |
24892 KB |
Output is correct |
85 |
Correct |
1163 ms |
24904 KB |
Output is correct |
86 |
Correct |
479 ms |
17480 KB |
Output is correct |
87 |
Correct |
113 ms |
25928 KB |
Output is correct |
88 |
Correct |
931 ms |
26292 KB |
Output is correct |
89 |
Correct |
883 ms |
26372 KB |
Output is correct |
90 |
Correct |
358 ms |
18508 KB |
Output is correct |
91 |
Correct |
72 ms |
13508 KB |
Output is correct |
92 |
Correct |
199 ms |
83716 KB |
Output is correct |
93 |
Correct |
200 ms |
81924 KB |
Output is correct |
94 |
Correct |
201 ms |
81992 KB |
Output is correct |
95 |
Correct |
212 ms |
81480 KB |
Output is correct |
96 |
Correct |
264 ms |
28744 KB |
Output is correct |
97 |
Correct |
644 ms |
25536 KB |
Output is correct |
98 |
Correct |
1289 ms |
201800 KB |
Output is correct |
99 |
Correct |
1043 ms |
201888 KB |
Output is correct |
100 |
Correct |
1544 ms |
113224 KB |
Output is correct |
101 |
Correct |
1227 ms |
113224 KB |
Output is correct |
102 |
Correct |
195 ms |
113052 KB |
Output is correct |
103 |
Correct |
170 ms |
113300 KB |
Output is correct |
104 |
Correct |
204 ms |
113224 KB |
Output is correct |
105 |
Correct |
102 ms |
13364 KB |
Output is correct |
106 |
Correct |
186 ms |
7616 KB |
Output is correct |
107 |
Correct |
179 ms |
18940 KB |
Output is correct |
108 |
Correct |
139 ms |
9220 KB |
Output is correct |
109 |
Correct |
181 ms |
11292 KB |
Output is correct |