#include <bits/stdc++.h>
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);
}
}
const int lg = log2(S);
vector<vector<pii>> sp(S, vector<pii>(lg + 1));
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 = 500;
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";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
41 ms |
6396 KB |
Output is correct |
3 |
Correct |
57 ms |
8940 KB |
Output is correct |
4 |
Correct |
85 ms |
11080 KB |
Output is correct |
5 |
Correct |
67 ms |
10120 KB |
Output is correct |
6 |
Correct |
82 ms |
11080 KB |
Output is correct |
7 |
Correct |
83 ms |
11080 KB |
Output is correct |
8 |
Correct |
63 ms |
9032 KB |
Output is correct |
9 |
Correct |
86 ms |
11080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
41 ms |
6396 KB |
Output is correct |
3 |
Correct |
57 ms |
8940 KB |
Output is correct |
4 |
Correct |
85 ms |
11080 KB |
Output is correct |
5 |
Correct |
67 ms |
10120 KB |
Output is correct |
6 |
Correct |
82 ms |
11080 KB |
Output is correct |
7 |
Correct |
83 ms |
11080 KB |
Output is correct |
8 |
Correct |
63 ms |
9032 KB |
Output is correct |
9 |
Correct |
86 ms |
11080 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
88 ms |
8008 KB |
Output is correct |
12 |
Correct |
90 ms |
7752 KB |
Output is correct |
13 |
Correct |
92 ms |
7852 KB |
Output is correct |
14 |
Correct |
84 ms |
7860 KB |
Output is correct |
15 |
Correct |
83 ms |
8520 KB |
Output is correct |
16 |
Correct |
48 ms |
6428 KB |
Output is correct |
17 |
Correct |
84 ms |
9804 KB |
Output is correct |
18 |
Correct |
78 ms |
8532 KB |
Output is correct |
19 |
Correct |
74 ms |
9800 KB |
Output is correct |
20 |
Correct |
73 ms |
8520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
41 ms |
6396 KB |
Output is correct |
3 |
Correct |
57 ms |
8940 KB |
Output is correct |
4 |
Correct |
85 ms |
11080 KB |
Output is correct |
5 |
Correct |
67 ms |
10120 KB |
Output is correct |
6 |
Correct |
82 ms |
11080 KB |
Output is correct |
7 |
Correct |
83 ms |
11080 KB |
Output is correct |
8 |
Correct |
63 ms |
9032 KB |
Output is correct |
9 |
Correct |
86 ms |
11080 KB |
Output is correct |
10 |
Correct |
333 ms |
151476 KB |
Output is correct |
11 |
Correct |
426 ms |
191420 KB |
Output is correct |
12 |
Correct |
439 ms |
191560 KB |
Output is correct |
13 |
Correct |
349 ms |
190428 KB |
Output is correct |
14 |
Correct |
403 ms |
191560 KB |
Output is correct |
15 |
Correct |
394 ms |
191560 KB |
Output is correct |
16 |
Correct |
276 ms |
151400 KB |
Output is correct |
17 |
Correct |
370 ms |
191560 KB |
Output is correct |
18 |
Correct |
274 ms |
170056 KB |
Output is correct |
19 |
Correct |
271 ms |
169800 KB |
Output is correct |
20 |
Correct |
238 ms |
170056 KB |
Output is correct |
21 |
Correct |
232 ms |
170088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
41 ms |
6396 KB |
Output is correct |
3 |
Correct |
57 ms |
8940 KB |
Output is correct |
4 |
Correct |
85 ms |
11080 KB |
Output is correct |
5 |
Correct |
67 ms |
10120 KB |
Output is correct |
6 |
Correct |
82 ms |
11080 KB |
Output is correct |
7 |
Correct |
83 ms |
11080 KB |
Output is correct |
8 |
Correct |
63 ms |
9032 KB |
Output is correct |
9 |
Correct |
86 ms |
11080 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
88 ms |
8008 KB |
Output is correct |
12 |
Correct |
90 ms |
7752 KB |
Output is correct |
13 |
Correct |
92 ms |
7852 KB |
Output is correct |
14 |
Correct |
84 ms |
7860 KB |
Output is correct |
15 |
Correct |
83 ms |
8520 KB |
Output is correct |
16 |
Correct |
48 ms |
6428 KB |
Output is correct |
17 |
Correct |
84 ms |
9804 KB |
Output is correct |
18 |
Correct |
78 ms |
8532 KB |
Output is correct |
19 |
Correct |
74 ms |
9800 KB |
Output is correct |
20 |
Correct |
73 ms |
8520 KB |
Output is correct |
21 |
Correct |
333 ms |
151476 KB |
Output is correct |
22 |
Correct |
426 ms |
191420 KB |
Output is correct |
23 |
Correct |
439 ms |
191560 KB |
Output is correct |
24 |
Correct |
349 ms |
190428 KB |
Output is correct |
25 |
Correct |
403 ms |
191560 KB |
Output is correct |
26 |
Correct |
394 ms |
191560 KB |
Output is correct |
27 |
Correct |
276 ms |
151400 KB |
Output is correct |
28 |
Correct |
370 ms |
191560 KB |
Output is correct |
29 |
Correct |
274 ms |
170056 KB |
Output is correct |
30 |
Correct |
271 ms |
169800 KB |
Output is correct |
31 |
Correct |
238 ms |
170056 KB |
Output is correct |
32 |
Correct |
232 ms |
170088 KB |
Output is correct |
33 |
Correct |
280 ms |
58720 KB |
Output is correct |
34 |
Correct |
305 ms |
58428 KB |
Output is correct |
35 |
Correct |
287 ms |
56648 KB |
Output is correct |
36 |
Correct |
312 ms |
56500 KB |
Output is correct |
37 |
Correct |
294 ms |
73552 KB |
Output is correct |
38 |
Correct |
314 ms |
107340 KB |
Output is correct |
39 |
Correct |
380 ms |
135868 KB |
Output is correct |
40 |
Correct |
265 ms |
56592 KB |
Output is correct |
41 |
Correct |
257 ms |
100936 KB |
Output is correct |
42 |
Correct |
325 ms |
133704 KB |
Output is correct |
43 |
Correct |
232 ms |
73800 KB |
Output is correct |
44 |
Correct |
281 ms |
75852 KB |
Output is correct |
45 |
Correct |
207 ms |
120228 KB |
Output is correct |
46 |
Correct |
147 ms |
70472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
4 ms |
3408 KB |
Output is correct |
3 |
Correct |
783 ms |
21068 KB |
Output is correct |
4 |
Correct |
738 ms |
21124 KB |
Output is correct |
5 |
Correct |
806 ms |
21288 KB |
Output is correct |
6 |
Correct |
823 ms |
21176 KB |
Output is correct |
7 |
Correct |
773 ms |
21028 KB |
Output is correct |
8 |
Correct |
332 ms |
13684 KB |
Output is correct |
9 |
Correct |
56 ms |
21512 KB |
Output is correct |
10 |
Correct |
46 ms |
12260 KB |
Output is correct |
11 |
Correct |
268 ms |
170056 KB |
Output is correct |
12 |
Correct |
202 ms |
120416 KB |
Output is correct |
13 |
Correct |
178 ms |
70500 KB |
Output is correct |
14 |
Correct |
106 ms |
52040 KB |
Output is correct |
15 |
Correct |
111 ms |
51976 KB |
Output is correct |
16 |
Correct |
111 ms |
52552 KB |
Output is correct |
17 |
Correct |
118 ms |
52004 KB |
Output is correct |
18 |
Correct |
114 ms |
22984 KB |
Output is correct |
19 |
Correct |
516 ms |
21496 KB |
Output is correct |
20 |
Correct |
573 ms |
129608 KB |
Output is correct |
21 |
Correct |
503 ms |
129448 KB |
Output is correct |
22 |
Correct |
701 ms |
75080 KB |
Output is correct |
23 |
Correct |
523 ms |
74968 KB |
Output is correct |
24 |
Correct |
110 ms |
75036 KB |
Output is correct |
25 |
Correct |
99 ms |
74876 KB |
Output is correct |
26 |
Correct |
124 ms |
75084 KB |
Output is correct |
27 |
Correct |
219 ms |
169860 KB |
Output is correct |
28 |
Correct |
234 ms |
169928 KB |
Output is correct |
29 |
Correct |
242 ms |
170036 KB |
Output is correct |
30 |
Correct |
64 ms |
16228 KB |
Output is correct |
31 |
Correct |
117 ms |
5384 KB |
Output is correct |
32 |
Correct |
76 ms |
12360 KB |
Output is correct |
33 |
Correct |
56 ms |
5828 KB |
Output is correct |
34 |
Correct |
85 ms |
7752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
41 ms |
6396 KB |
Output is correct |
3 |
Correct |
57 ms |
8940 KB |
Output is correct |
4 |
Correct |
85 ms |
11080 KB |
Output is correct |
5 |
Correct |
67 ms |
10120 KB |
Output is correct |
6 |
Correct |
82 ms |
11080 KB |
Output is correct |
7 |
Correct |
83 ms |
11080 KB |
Output is correct |
8 |
Correct |
63 ms |
9032 KB |
Output is correct |
9 |
Correct |
86 ms |
11080 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
88 ms |
8008 KB |
Output is correct |
12 |
Correct |
90 ms |
7752 KB |
Output is correct |
13 |
Correct |
92 ms |
7852 KB |
Output is correct |
14 |
Correct |
84 ms |
7860 KB |
Output is correct |
15 |
Correct |
83 ms |
8520 KB |
Output is correct |
16 |
Correct |
48 ms |
6428 KB |
Output is correct |
17 |
Correct |
84 ms |
9804 KB |
Output is correct |
18 |
Correct |
78 ms |
8532 KB |
Output is correct |
19 |
Correct |
74 ms |
9800 KB |
Output is correct |
20 |
Correct |
73 ms |
8520 KB |
Output is correct |
21 |
Correct |
333 ms |
151476 KB |
Output is correct |
22 |
Correct |
426 ms |
191420 KB |
Output is correct |
23 |
Correct |
439 ms |
191560 KB |
Output is correct |
24 |
Correct |
349 ms |
190428 KB |
Output is correct |
25 |
Correct |
403 ms |
191560 KB |
Output is correct |
26 |
Correct |
394 ms |
191560 KB |
Output is correct |
27 |
Correct |
276 ms |
151400 KB |
Output is correct |
28 |
Correct |
370 ms |
191560 KB |
Output is correct |
29 |
Correct |
274 ms |
170056 KB |
Output is correct |
30 |
Correct |
271 ms |
169800 KB |
Output is correct |
31 |
Correct |
238 ms |
170056 KB |
Output is correct |
32 |
Correct |
232 ms |
170088 KB |
Output is correct |
33 |
Correct |
280 ms |
58720 KB |
Output is correct |
34 |
Correct |
305 ms |
58428 KB |
Output is correct |
35 |
Correct |
287 ms |
56648 KB |
Output is correct |
36 |
Correct |
312 ms |
56500 KB |
Output is correct |
37 |
Correct |
294 ms |
73552 KB |
Output is correct |
38 |
Correct |
314 ms |
107340 KB |
Output is correct |
39 |
Correct |
380 ms |
135868 KB |
Output is correct |
40 |
Correct |
265 ms |
56592 KB |
Output is correct |
41 |
Correct |
257 ms |
100936 KB |
Output is correct |
42 |
Correct |
325 ms |
133704 KB |
Output is correct |
43 |
Correct |
232 ms |
73800 KB |
Output is correct |
44 |
Correct |
281 ms |
75852 KB |
Output is correct |
45 |
Correct |
207 ms |
120228 KB |
Output is correct |
46 |
Correct |
147 ms |
70472 KB |
Output is correct |
47 |
Correct |
3 ms |
3408 KB |
Output is correct |
48 |
Correct |
4 ms |
3408 KB |
Output is correct |
49 |
Correct |
783 ms |
21068 KB |
Output is correct |
50 |
Correct |
738 ms |
21124 KB |
Output is correct |
51 |
Correct |
806 ms |
21288 KB |
Output is correct |
52 |
Correct |
823 ms |
21176 KB |
Output is correct |
53 |
Correct |
773 ms |
21028 KB |
Output is correct |
54 |
Correct |
332 ms |
13684 KB |
Output is correct |
55 |
Correct |
56 ms |
21512 KB |
Output is correct |
56 |
Correct |
46 ms |
12260 KB |
Output is correct |
57 |
Correct |
268 ms |
170056 KB |
Output is correct |
58 |
Correct |
202 ms |
120416 KB |
Output is correct |
59 |
Correct |
178 ms |
70500 KB |
Output is correct |
60 |
Correct |
106 ms |
52040 KB |
Output is correct |
61 |
Correct |
111 ms |
51976 KB |
Output is correct |
62 |
Correct |
111 ms |
52552 KB |
Output is correct |
63 |
Correct |
118 ms |
52004 KB |
Output is correct |
64 |
Correct |
114 ms |
22984 KB |
Output is correct |
65 |
Correct |
516 ms |
21496 KB |
Output is correct |
66 |
Correct |
573 ms |
129608 KB |
Output is correct |
67 |
Correct |
503 ms |
129448 KB |
Output is correct |
68 |
Correct |
701 ms |
75080 KB |
Output is correct |
69 |
Correct |
523 ms |
74968 KB |
Output is correct |
70 |
Correct |
110 ms |
75036 KB |
Output is correct |
71 |
Correct |
99 ms |
74876 KB |
Output is correct |
72 |
Correct |
124 ms |
75084 KB |
Output is correct |
73 |
Correct |
219 ms |
169860 KB |
Output is correct |
74 |
Correct |
234 ms |
169928 KB |
Output is correct |
75 |
Correct |
242 ms |
170036 KB |
Output is correct |
76 |
Correct |
64 ms |
16228 KB |
Output is correct |
77 |
Correct |
117 ms |
5384 KB |
Output is correct |
78 |
Correct |
76 ms |
12360 KB |
Output is correct |
79 |
Correct |
56 ms |
5828 KB |
Output is correct |
80 |
Correct |
85 ms |
7752 KB |
Output is correct |
81 |
Correct |
1725 ms |
25216 KB |
Output is correct |
82 |
Correct |
1885 ms |
25400 KB |
Output is correct |
83 |
Correct |
2021 ms |
25236 KB |
Output is correct |
84 |
Correct |
1985 ms |
25228 KB |
Output is correct |
85 |
Correct |
1899 ms |
25160 KB |
Output is correct |
86 |
Correct |
682 ms |
17140 KB |
Output is correct |
87 |
Correct |
1350 ms |
25928 KB |
Output is correct |
88 |
Correct |
1417 ms |
26384 KB |
Output is correct |
89 |
Correct |
1473 ms |
26308 KB |
Output is correct |
90 |
Correct |
535 ms |
18248 KB |
Output is correct |
91 |
Correct |
70 ms |
13756 KB |
Output is correct |
92 |
Correct |
237 ms |
61700 KB |
Output is correct |
93 |
Correct |
233 ms |
60388 KB |
Output is correct |
94 |
Correct |
232 ms |
60744 KB |
Output is correct |
95 |
Correct |
256 ms |
60348 KB |
Output is correct |
96 |
Correct |
348 ms |
27720 KB |
Output is correct |
97 |
Correct |
1683 ms |
25916 KB |
Output is correct |
98 |
Correct |
1795 ms |
134472 KB |
Output is correct |
99 |
Correct |
1341 ms |
134472 KB |
Output is correct |
100 |
Correct |
2341 ms |
80076 KB |
Output is correct |
101 |
Correct |
1682 ms |
79840 KB |
Output is correct |
102 |
Correct |
183 ms |
79772 KB |
Output is correct |
103 |
Correct |
161 ms |
80008 KB |
Output is correct |
104 |
Correct |
184 ms |
79944 KB |
Output is correct |
105 |
Correct |
123 ms |
11392 KB |
Output is correct |
106 |
Correct |
229 ms |
7568 KB |
Output is correct |
107 |
Correct |
193 ms |
16200 KB |
Output is correct |
108 |
Correct |
156 ms |
8580 KB |
Output is correct |
109 |
Correct |
232 ms |
10660 KB |
Output is correct |