#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 = 700;
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 |
44 ms |
6352 KB |
Output is correct |
3 |
Correct |
57 ms |
8264 KB |
Output is correct |
4 |
Correct |
83 ms |
9992 KB |
Output is correct |
5 |
Correct |
75 ms |
9288 KB |
Output is correct |
6 |
Correct |
80 ms |
10056 KB |
Output is correct |
7 |
Correct |
77 ms |
10056 KB |
Output is correct |
8 |
Correct |
55 ms |
8264 KB |
Output is correct |
9 |
Correct |
76 ms |
10096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
44 ms |
6352 KB |
Output is correct |
3 |
Correct |
57 ms |
8264 KB |
Output is correct |
4 |
Correct |
83 ms |
9992 KB |
Output is correct |
5 |
Correct |
75 ms |
9288 KB |
Output is correct |
6 |
Correct |
80 ms |
10056 KB |
Output is correct |
7 |
Correct |
77 ms |
10056 KB |
Output is correct |
8 |
Correct |
55 ms |
8264 KB |
Output is correct |
9 |
Correct |
76 ms |
10096 KB |
Output is correct |
10 |
Correct |
3 ms |
3572 KB |
Output is correct |
11 |
Correct |
86 ms |
7772 KB |
Output is correct |
12 |
Correct |
84 ms |
7768 KB |
Output is correct |
13 |
Correct |
80 ms |
7752 KB |
Output is correct |
14 |
Correct |
79 ms |
7752 KB |
Output is correct |
15 |
Correct |
77 ms |
8264 KB |
Output is correct |
16 |
Correct |
42 ms |
6472 KB |
Output is correct |
17 |
Correct |
72 ms |
9056 KB |
Output is correct |
18 |
Correct |
81 ms |
8264 KB |
Output is correct |
19 |
Correct |
76 ms |
9032 KB |
Output is correct |
20 |
Correct |
76 ms |
8204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
44 ms |
6352 KB |
Output is correct |
3 |
Correct |
57 ms |
8264 KB |
Output is correct |
4 |
Correct |
83 ms |
9992 KB |
Output is correct |
5 |
Correct |
75 ms |
9288 KB |
Output is correct |
6 |
Correct |
80 ms |
10056 KB |
Output is correct |
7 |
Correct |
77 ms |
10056 KB |
Output is correct |
8 |
Correct |
55 ms |
8264 KB |
Output is correct |
9 |
Correct |
76 ms |
10096 KB |
Output is correct |
10 |
Correct |
428 ms |
115528 KB |
Output is correct |
11 |
Correct |
563 ms |
145996 KB |
Output is correct |
12 |
Correct |
662 ms |
146248 KB |
Output is correct |
13 |
Correct |
514 ms |
144968 KB |
Output is correct |
14 |
Correct |
564 ms |
145992 KB |
Output is correct |
15 |
Correct |
467 ms |
146004 KB |
Output is correct |
16 |
Correct |
285 ms |
115528 KB |
Output is correct |
17 |
Correct |
418 ms |
146000 KB |
Output is correct |
18 |
Correct |
226 ms |
129096 KB |
Output is correct |
19 |
Correct |
213 ms |
128840 KB |
Output is correct |
20 |
Correct |
199 ms |
129096 KB |
Output is correct |
21 |
Correct |
218 ms |
129096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
44 ms |
6352 KB |
Output is correct |
3 |
Correct |
57 ms |
8264 KB |
Output is correct |
4 |
Correct |
83 ms |
9992 KB |
Output is correct |
5 |
Correct |
75 ms |
9288 KB |
Output is correct |
6 |
Correct |
80 ms |
10056 KB |
Output is correct |
7 |
Correct |
77 ms |
10056 KB |
Output is correct |
8 |
Correct |
55 ms |
8264 KB |
Output is correct |
9 |
Correct |
76 ms |
10096 KB |
Output is correct |
10 |
Correct |
3 ms |
3572 KB |
Output is correct |
11 |
Correct |
86 ms |
7772 KB |
Output is correct |
12 |
Correct |
84 ms |
7768 KB |
Output is correct |
13 |
Correct |
80 ms |
7752 KB |
Output is correct |
14 |
Correct |
79 ms |
7752 KB |
Output is correct |
15 |
Correct |
77 ms |
8264 KB |
Output is correct |
16 |
Correct |
42 ms |
6472 KB |
Output is correct |
17 |
Correct |
72 ms |
9056 KB |
Output is correct |
18 |
Correct |
81 ms |
8264 KB |
Output is correct |
19 |
Correct |
76 ms |
9032 KB |
Output is correct |
20 |
Correct |
76 ms |
8204 KB |
Output is correct |
21 |
Correct |
428 ms |
115528 KB |
Output is correct |
22 |
Correct |
563 ms |
145996 KB |
Output is correct |
23 |
Correct |
662 ms |
146248 KB |
Output is correct |
24 |
Correct |
514 ms |
144968 KB |
Output is correct |
25 |
Correct |
564 ms |
145992 KB |
Output is correct |
26 |
Correct |
467 ms |
146004 KB |
Output is correct |
27 |
Correct |
285 ms |
115528 KB |
Output is correct |
28 |
Correct |
418 ms |
146000 KB |
Output is correct |
29 |
Correct |
226 ms |
129096 KB |
Output is correct |
30 |
Correct |
213 ms |
128840 KB |
Output is correct |
31 |
Correct |
199 ms |
129096 KB |
Output is correct |
32 |
Correct |
218 ms |
129096 KB |
Output is correct |
33 |
Correct |
408 ms |
49480 KB |
Output is correct |
34 |
Correct |
377 ms |
49480 KB |
Output is correct |
35 |
Correct |
279 ms |
47524 KB |
Output is correct |
36 |
Correct |
315 ms |
47432 KB |
Output is correct |
37 |
Correct |
342 ms |
60152 KB |
Output is correct |
38 |
Correct |
389 ms |
83528 KB |
Output is correct |
39 |
Correct |
407 ms |
105692 KB |
Output is correct |
40 |
Correct |
307 ms |
46408 KB |
Output is correct |
41 |
Correct |
262 ms |
78228 KB |
Output is correct |
42 |
Correct |
318 ms |
103496 KB |
Output is correct |
43 |
Correct |
293 ms |
59012 KB |
Output is correct |
44 |
Correct |
280 ms |
60488 KB |
Output is correct |
45 |
Correct |
194 ms |
92752 KB |
Output is correct |
46 |
Correct |
148 ms |
56904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
3 ms |
3408 KB |
Output is correct |
3 |
Correct |
813 ms |
21068 KB |
Output is correct |
4 |
Correct |
840 ms |
21088 KB |
Output is correct |
5 |
Correct |
835 ms |
21064 KB |
Output is correct |
6 |
Correct |
878 ms |
21092 KB |
Output is correct |
7 |
Correct |
755 ms |
21064 KB |
Output is correct |
8 |
Correct |
326 ms |
13640 KB |
Output is correct |
9 |
Correct |
572 ms |
21328 KB |
Output is correct |
10 |
Correct |
41 ms |
12260 KB |
Output is correct |
11 |
Correct |
220 ms |
129088 KB |
Output is correct |
12 |
Correct |
189 ms |
92744 KB |
Output is correct |
13 |
Correct |
153 ms |
56904 KB |
Output is correct |
14 |
Correct |
99 ms |
43592 KB |
Output is correct |
15 |
Correct |
102 ms |
43264 KB |
Output is correct |
16 |
Correct |
95 ms |
43848 KB |
Output is correct |
17 |
Correct |
107 ms |
43636 KB |
Output is correct |
18 |
Correct |
116 ms |
24512 KB |
Output is correct |
19 |
Correct |
534 ms |
22720 KB |
Output is correct |
20 |
Correct |
571 ms |
102116 KB |
Output is correct |
21 |
Correct |
455 ms |
101960 KB |
Output is correct |
22 |
Correct |
721 ms |
61768 KB |
Output is correct |
23 |
Correct |
540 ms |
60232 KB |
Output is correct |
24 |
Correct |
114 ms |
60124 KB |
Output is correct |
25 |
Correct |
103 ms |
61648 KB |
Output is correct |
26 |
Correct |
119 ms |
62804 KB |
Output is correct |
27 |
Correct |
225 ms |
130376 KB |
Output is correct |
28 |
Correct |
237 ms |
129260 KB |
Output is correct |
29 |
Correct |
245 ms |
130888 KB |
Output is correct |
30 |
Correct |
72 ms |
15296 KB |
Output is correct |
31 |
Correct |
116 ms |
7428 KB |
Output is correct |
32 |
Correct |
75 ms |
13896 KB |
Output is correct |
33 |
Correct |
59 ms |
7348 KB |
Output is correct |
34 |
Correct |
83 ms |
9800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
3408 KB |
Output is correct |
2 |
Correct |
44 ms |
6352 KB |
Output is correct |
3 |
Correct |
57 ms |
8264 KB |
Output is correct |
4 |
Correct |
83 ms |
9992 KB |
Output is correct |
5 |
Correct |
75 ms |
9288 KB |
Output is correct |
6 |
Correct |
80 ms |
10056 KB |
Output is correct |
7 |
Correct |
77 ms |
10056 KB |
Output is correct |
8 |
Correct |
55 ms |
8264 KB |
Output is correct |
9 |
Correct |
76 ms |
10096 KB |
Output is correct |
10 |
Correct |
3 ms |
3572 KB |
Output is correct |
11 |
Correct |
86 ms |
7772 KB |
Output is correct |
12 |
Correct |
84 ms |
7768 KB |
Output is correct |
13 |
Correct |
80 ms |
7752 KB |
Output is correct |
14 |
Correct |
79 ms |
7752 KB |
Output is correct |
15 |
Correct |
77 ms |
8264 KB |
Output is correct |
16 |
Correct |
42 ms |
6472 KB |
Output is correct |
17 |
Correct |
72 ms |
9056 KB |
Output is correct |
18 |
Correct |
81 ms |
8264 KB |
Output is correct |
19 |
Correct |
76 ms |
9032 KB |
Output is correct |
20 |
Correct |
76 ms |
8204 KB |
Output is correct |
21 |
Correct |
428 ms |
115528 KB |
Output is correct |
22 |
Correct |
563 ms |
145996 KB |
Output is correct |
23 |
Correct |
662 ms |
146248 KB |
Output is correct |
24 |
Correct |
514 ms |
144968 KB |
Output is correct |
25 |
Correct |
564 ms |
145992 KB |
Output is correct |
26 |
Correct |
467 ms |
146004 KB |
Output is correct |
27 |
Correct |
285 ms |
115528 KB |
Output is correct |
28 |
Correct |
418 ms |
146000 KB |
Output is correct |
29 |
Correct |
226 ms |
129096 KB |
Output is correct |
30 |
Correct |
213 ms |
128840 KB |
Output is correct |
31 |
Correct |
199 ms |
129096 KB |
Output is correct |
32 |
Correct |
218 ms |
129096 KB |
Output is correct |
33 |
Correct |
408 ms |
49480 KB |
Output is correct |
34 |
Correct |
377 ms |
49480 KB |
Output is correct |
35 |
Correct |
279 ms |
47524 KB |
Output is correct |
36 |
Correct |
315 ms |
47432 KB |
Output is correct |
37 |
Correct |
342 ms |
60152 KB |
Output is correct |
38 |
Correct |
389 ms |
83528 KB |
Output is correct |
39 |
Correct |
407 ms |
105692 KB |
Output is correct |
40 |
Correct |
307 ms |
46408 KB |
Output is correct |
41 |
Correct |
262 ms |
78228 KB |
Output is correct |
42 |
Correct |
318 ms |
103496 KB |
Output is correct |
43 |
Correct |
293 ms |
59012 KB |
Output is correct |
44 |
Correct |
280 ms |
60488 KB |
Output is correct |
45 |
Correct |
194 ms |
92752 KB |
Output is correct |
46 |
Correct |
148 ms |
56904 KB |
Output is correct |
47 |
Correct |
3 ms |
3408 KB |
Output is correct |
48 |
Correct |
3 ms |
3408 KB |
Output is correct |
49 |
Correct |
813 ms |
21068 KB |
Output is correct |
50 |
Correct |
840 ms |
21088 KB |
Output is correct |
51 |
Correct |
835 ms |
21064 KB |
Output is correct |
52 |
Correct |
878 ms |
21092 KB |
Output is correct |
53 |
Correct |
755 ms |
21064 KB |
Output is correct |
54 |
Correct |
326 ms |
13640 KB |
Output is correct |
55 |
Correct |
572 ms |
21328 KB |
Output is correct |
56 |
Correct |
41 ms |
12260 KB |
Output is correct |
57 |
Correct |
220 ms |
129088 KB |
Output is correct |
58 |
Correct |
189 ms |
92744 KB |
Output is correct |
59 |
Correct |
153 ms |
56904 KB |
Output is correct |
60 |
Correct |
99 ms |
43592 KB |
Output is correct |
61 |
Correct |
102 ms |
43264 KB |
Output is correct |
62 |
Correct |
95 ms |
43848 KB |
Output is correct |
63 |
Correct |
107 ms |
43636 KB |
Output is correct |
64 |
Correct |
116 ms |
24512 KB |
Output is correct |
65 |
Correct |
534 ms |
22720 KB |
Output is correct |
66 |
Correct |
571 ms |
102116 KB |
Output is correct |
67 |
Correct |
455 ms |
101960 KB |
Output is correct |
68 |
Correct |
721 ms |
61768 KB |
Output is correct |
69 |
Correct |
540 ms |
60232 KB |
Output is correct |
70 |
Correct |
114 ms |
60124 KB |
Output is correct |
71 |
Correct |
103 ms |
61648 KB |
Output is correct |
72 |
Correct |
119 ms |
62804 KB |
Output is correct |
73 |
Correct |
225 ms |
130376 KB |
Output is correct |
74 |
Correct |
237 ms |
129260 KB |
Output is correct |
75 |
Correct |
245 ms |
130888 KB |
Output is correct |
76 |
Correct |
72 ms |
15296 KB |
Output is correct |
77 |
Correct |
116 ms |
7428 KB |
Output is correct |
78 |
Correct |
75 ms |
13896 KB |
Output is correct |
79 |
Correct |
59 ms |
7348 KB |
Output is correct |
80 |
Correct |
83 ms |
9800 KB |
Output is correct |
81 |
Correct |
1823 ms |
27408 KB |
Output is correct |
82 |
Correct |
1831 ms |
28744 KB |
Output is correct |
83 |
Correct |
1758 ms |
24992 KB |
Output is correct |
84 |
Correct |
1797 ms |
29000 KB |
Output is correct |
85 |
Correct |
1767 ms |
24792 KB |
Output is correct |
86 |
Correct |
679 ms |
20688 KB |
Output is correct |
87 |
Correct |
1227 ms |
28020 KB |
Output is correct |
88 |
Correct |
1375 ms |
29664 KB |
Output is correct |
89 |
Correct |
1321 ms |
29504 KB |
Output is correct |
90 |
Correct |
538 ms |
17852 KB |
Output is correct |
91 |
Correct |
67 ms |
16568 KB |
Output is correct |
92 |
Correct |
188 ms |
56004 KB |
Output is correct |
93 |
Correct |
207 ms |
55044 KB |
Output is correct |
94 |
Correct |
192 ms |
56236 KB |
Output is correct |
95 |
Correct |
224 ms |
53580 KB |
Output is correct |
96 |
Correct |
323 ms |
29512 KB |
Output is correct |
97 |
Correct |
1720 ms |
27976 KB |
Output is correct |
98 |
Correct |
1749 ms |
107124 KB |
Output is correct |
99 |
Correct |
1365 ms |
108872 KB |
Output is correct |
100 |
Correct |
2302 ms |
67740 KB |
Output is correct |
101 |
Correct |
1707 ms |
69884 KB |
Output is correct |
102 |
Correct |
175 ms |
64924 KB |
Output is correct |
103 |
Correct |
140 ms |
68936 KB |
Output is correct |
104 |
Correct |
176 ms |
67400 KB |
Output is correct |
105 |
Correct |
115 ms |
13192 KB |
Output is correct |
106 |
Correct |
227 ms |
10616 KB |
Output is correct |
107 |
Correct |
194 ms |
14980 KB |
Output is correct |
108 |
Correct |
149 ms |
11840 KB |
Output is correct |
109 |
Correct |
237 ms |
13936 KB |
Output is correct |