#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[n / A + 2][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 |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
40 ms |
6480 KB |
Output is correct |
3 |
Correct |
52 ms |
6816 KB |
Output is correct |
4 |
Correct |
71 ms |
7788 KB |
Output is correct |
5 |
Correct |
66 ms |
7084 KB |
Output is correct |
6 |
Correct |
74 ms |
7752 KB |
Output is correct |
7 |
Correct |
83 ms |
7948 KB |
Output is correct |
8 |
Correct |
64 ms |
6728 KB |
Output is correct |
9 |
Correct |
75 ms |
7752 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
40 ms |
6480 KB |
Output is correct |
3 |
Correct |
52 ms |
6816 KB |
Output is correct |
4 |
Correct |
71 ms |
7788 KB |
Output is correct |
5 |
Correct |
66 ms |
7084 KB |
Output is correct |
6 |
Correct |
74 ms |
7752 KB |
Output is correct |
7 |
Correct |
83 ms |
7948 KB |
Output is correct |
8 |
Correct |
64 ms |
6728 KB |
Output is correct |
9 |
Correct |
75 ms |
7752 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
82 ms |
7244 KB |
Output is correct |
12 |
Correct |
83 ms |
7144 KB |
Output is correct |
13 |
Correct |
85 ms |
7240 KB |
Output is correct |
14 |
Correct |
85 ms |
7240 KB |
Output is correct |
15 |
Correct |
74 ms |
7536 KB |
Output is correct |
16 |
Correct |
40 ms |
7240 KB |
Output is correct |
17 |
Correct |
69 ms |
7720 KB |
Output is correct |
18 |
Correct |
73 ms |
7536 KB |
Output is correct |
19 |
Correct |
73 ms |
7752 KB |
Output is correct |
20 |
Correct |
74 ms |
7496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
40 ms |
6480 KB |
Output is correct |
3 |
Correct |
52 ms |
6816 KB |
Output is correct |
4 |
Correct |
71 ms |
7788 KB |
Output is correct |
5 |
Correct |
66 ms |
7084 KB |
Output is correct |
6 |
Correct |
74 ms |
7752 KB |
Output is correct |
7 |
Correct |
83 ms |
7948 KB |
Output is correct |
8 |
Correct |
64 ms |
6728 KB |
Output is correct |
9 |
Correct |
75 ms |
7752 KB |
Output is correct |
10 |
Correct |
332 ms |
123128 KB |
Output is correct |
11 |
Correct |
518 ms |
189224 KB |
Output is correct |
12 |
Correct |
430 ms |
189168 KB |
Output is correct |
13 |
Correct |
353 ms |
188088 KB |
Output is correct |
14 |
Correct |
389 ms |
189000 KB |
Output is correct |
15 |
Correct |
406 ms |
189256 KB |
Output is correct |
16 |
Correct |
312 ms |
122952 KB |
Output is correct |
17 |
Correct |
478 ms |
189172 KB |
Output is correct |
18 |
Correct |
238 ms |
153924 KB |
Output is correct |
19 |
Correct |
212 ms |
153672 KB |
Output is correct |
20 |
Correct |
230 ms |
153672 KB |
Output is correct |
21 |
Correct |
219 ms |
153788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
40 ms |
6480 KB |
Output is correct |
3 |
Correct |
52 ms |
6816 KB |
Output is correct |
4 |
Correct |
71 ms |
7788 KB |
Output is correct |
5 |
Correct |
66 ms |
7084 KB |
Output is correct |
6 |
Correct |
74 ms |
7752 KB |
Output is correct |
7 |
Correct |
83 ms |
7948 KB |
Output is correct |
8 |
Correct |
64 ms |
6728 KB |
Output is correct |
9 |
Correct |
75 ms |
7752 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
82 ms |
7244 KB |
Output is correct |
12 |
Correct |
83 ms |
7144 KB |
Output is correct |
13 |
Correct |
85 ms |
7240 KB |
Output is correct |
14 |
Correct |
85 ms |
7240 KB |
Output is correct |
15 |
Correct |
74 ms |
7536 KB |
Output is correct |
16 |
Correct |
40 ms |
7240 KB |
Output is correct |
17 |
Correct |
69 ms |
7720 KB |
Output is correct |
18 |
Correct |
73 ms |
7536 KB |
Output is correct |
19 |
Correct |
73 ms |
7752 KB |
Output is correct |
20 |
Correct |
74 ms |
7496 KB |
Output is correct |
21 |
Correct |
332 ms |
123128 KB |
Output is correct |
22 |
Correct |
518 ms |
189224 KB |
Output is correct |
23 |
Correct |
430 ms |
189168 KB |
Output is correct |
24 |
Correct |
353 ms |
188088 KB |
Output is correct |
25 |
Correct |
389 ms |
189000 KB |
Output is correct |
26 |
Correct |
406 ms |
189256 KB |
Output is correct |
27 |
Correct |
312 ms |
122952 KB |
Output is correct |
28 |
Correct |
478 ms |
189172 KB |
Output is correct |
29 |
Correct |
238 ms |
153924 KB |
Output is correct |
30 |
Correct |
212 ms |
153672 KB |
Output is correct |
31 |
Correct |
230 ms |
153672 KB |
Output is correct |
32 |
Correct |
219 ms |
153788 KB |
Output is correct |
33 |
Correct |
285 ms |
33352 KB |
Output is correct |
34 |
Correct |
345 ms |
33096 KB |
Output is correct |
35 |
Correct |
300 ms |
31048 KB |
Output is correct |
36 |
Correct |
260 ms |
31048 KB |
Output is correct |
37 |
Correct |
255 ms |
40016 KB |
Output is correct |
38 |
Correct |
249 ms |
66680 KB |
Output is correct |
39 |
Correct |
328 ms |
99408 KB |
Output is correct |
40 |
Correct |
257 ms |
28668 KB |
Output is correct |
41 |
Correct |
222 ms |
60488 KB |
Output is correct |
42 |
Correct |
332 ms |
97392 KB |
Output is correct |
43 |
Correct |
207 ms |
38472 KB |
Output is correct |
44 |
Correct |
276 ms |
40036 KB |
Output is correct |
45 |
Correct |
170 ms |
80968 KB |
Output is correct |
46 |
Correct |
119 ms |
36936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
3 ms |
3408 KB |
Output is correct |
3 |
Correct |
757 ms |
20756 KB |
Output is correct |
4 |
Correct |
800 ms |
20808 KB |
Output is correct |
5 |
Correct |
846 ms |
20776 KB |
Output is correct |
6 |
Correct |
810 ms |
20552 KB |
Output is correct |
7 |
Correct |
774 ms |
20600 KB |
Output is correct |
8 |
Correct |
343 ms |
13132 KB |
Output is correct |
9 |
Runtime error |
66 ms |
40012 KB |
Execution killed with signal 11 |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
40 ms |
6480 KB |
Output is correct |
3 |
Correct |
52 ms |
6816 KB |
Output is correct |
4 |
Correct |
71 ms |
7788 KB |
Output is correct |
5 |
Correct |
66 ms |
7084 KB |
Output is correct |
6 |
Correct |
74 ms |
7752 KB |
Output is correct |
7 |
Correct |
83 ms |
7948 KB |
Output is correct |
8 |
Correct |
64 ms |
6728 KB |
Output is correct |
9 |
Correct |
75 ms |
7752 KB |
Output is correct |
10 |
Correct |
4 ms |
3408 KB |
Output is correct |
11 |
Correct |
82 ms |
7244 KB |
Output is correct |
12 |
Correct |
83 ms |
7144 KB |
Output is correct |
13 |
Correct |
85 ms |
7240 KB |
Output is correct |
14 |
Correct |
85 ms |
7240 KB |
Output is correct |
15 |
Correct |
74 ms |
7536 KB |
Output is correct |
16 |
Correct |
40 ms |
7240 KB |
Output is correct |
17 |
Correct |
69 ms |
7720 KB |
Output is correct |
18 |
Correct |
73 ms |
7536 KB |
Output is correct |
19 |
Correct |
73 ms |
7752 KB |
Output is correct |
20 |
Correct |
74 ms |
7496 KB |
Output is correct |
21 |
Correct |
332 ms |
123128 KB |
Output is correct |
22 |
Correct |
518 ms |
189224 KB |
Output is correct |
23 |
Correct |
430 ms |
189168 KB |
Output is correct |
24 |
Correct |
353 ms |
188088 KB |
Output is correct |
25 |
Correct |
389 ms |
189000 KB |
Output is correct |
26 |
Correct |
406 ms |
189256 KB |
Output is correct |
27 |
Correct |
312 ms |
122952 KB |
Output is correct |
28 |
Correct |
478 ms |
189172 KB |
Output is correct |
29 |
Correct |
238 ms |
153924 KB |
Output is correct |
30 |
Correct |
212 ms |
153672 KB |
Output is correct |
31 |
Correct |
230 ms |
153672 KB |
Output is correct |
32 |
Correct |
219 ms |
153788 KB |
Output is correct |
33 |
Correct |
285 ms |
33352 KB |
Output is correct |
34 |
Correct |
345 ms |
33096 KB |
Output is correct |
35 |
Correct |
300 ms |
31048 KB |
Output is correct |
36 |
Correct |
260 ms |
31048 KB |
Output is correct |
37 |
Correct |
255 ms |
40016 KB |
Output is correct |
38 |
Correct |
249 ms |
66680 KB |
Output is correct |
39 |
Correct |
328 ms |
99408 KB |
Output is correct |
40 |
Correct |
257 ms |
28668 KB |
Output is correct |
41 |
Correct |
222 ms |
60488 KB |
Output is correct |
42 |
Correct |
332 ms |
97392 KB |
Output is correct |
43 |
Correct |
207 ms |
38472 KB |
Output is correct |
44 |
Correct |
276 ms |
40036 KB |
Output is correct |
45 |
Correct |
170 ms |
80968 KB |
Output is correct |
46 |
Correct |
119 ms |
36936 KB |
Output is correct |
47 |
Correct |
3 ms |
3408 KB |
Output is correct |
48 |
Correct |
3 ms |
3408 KB |
Output is correct |
49 |
Correct |
757 ms |
20756 KB |
Output is correct |
50 |
Correct |
800 ms |
20808 KB |
Output is correct |
51 |
Correct |
846 ms |
20776 KB |
Output is correct |
52 |
Correct |
810 ms |
20552 KB |
Output is correct |
53 |
Correct |
774 ms |
20600 KB |
Output is correct |
54 |
Correct |
343 ms |
13132 KB |
Output is correct |
55 |
Runtime error |
66 ms |
40012 KB |
Execution killed with signal 11 |
56 |
Halted |
0 ms |
0 KB |
- |