#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();
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;
};
vector<vector<ll>> out;
vector<int> all;
const int A = 600;
ll mn;
for (int i = 1; i < n; i++){
if (x[i] <= A) continue;
all.pb(i);
vector<pair<int, ll>> st, nw;
for (int j = 0; j < x[i]; j++){
st.pb({p[i - 1] + j, t[i][j].ss - t[i][j].ff});
}
vector<ll> add;
for (int j = 0; j <= i; j++) add.pb(0);
for (int j = i + 1; j <= n; j++){
mn = inf;
for (auto [x, y]: st){
mn = min(mn, y);
}
add.pb(mn);
if (j == n) break;
for (auto& [x, y]: st){
y += D * e[x] + (t[j][g[x] - p[j - 1]].ss - t[j - 1][x - p[j - 2]].ss);
x = g[x];
}
nw.clear();
int k = 0;
while (k < st.size()){
int h = k; mn = inf;
while (h < st.size() && st[k].ff == st[h].ff){
mn = min(mn, st[h].ss);
h++;
}
nw.pb({st[k].ff, mn});
k = h;
}
st = nw;
}
out.pb(add);
}
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";
}
}
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:112:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
112 | while (k < st.size()){
| ~~^~~~~~~~~~~
Main.cpp:114:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | while (h < st.size() && st[k].ff == st[h].ff){
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
40 ms |
3412 KB |
Output is correct |
3 |
Correct |
56 ms |
3656 KB |
Output is correct |
4 |
Correct |
74 ms |
4688 KB |
Output is correct |
5 |
Correct |
64 ms |
3664 KB |
Output is correct |
6 |
Correct |
76 ms |
4680 KB |
Output is correct |
7 |
Correct |
76 ms |
4684 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
40 ms |
3412 KB |
Output is correct |
3 |
Correct |
56 ms |
3656 KB |
Output is correct |
4 |
Correct |
74 ms |
4688 KB |
Output is correct |
5 |
Correct |
64 ms |
3664 KB |
Output is correct |
6 |
Correct |
76 ms |
4680 KB |
Output is correct |
7 |
Correct |
76 ms |
4684 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4680 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
80 ms |
4168 KB |
Output is correct |
12 |
Correct |
80 ms |
4168 KB |
Output is correct |
13 |
Correct |
80 ms |
4168 KB |
Output is correct |
14 |
Correct |
80 ms |
4136 KB |
Output is correct |
15 |
Correct |
72 ms |
4424 KB |
Output is correct |
16 |
Correct |
38 ms |
3400 KB |
Output is correct |
17 |
Correct |
67 ms |
4424 KB |
Output is correct |
18 |
Correct |
82 ms |
4424 KB |
Output is correct |
19 |
Correct |
76 ms |
4428 KB |
Output is correct |
20 |
Correct |
71 ms |
4424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
40 ms |
3412 KB |
Output is correct |
3 |
Correct |
56 ms |
3656 KB |
Output is correct |
4 |
Correct |
74 ms |
4688 KB |
Output is correct |
5 |
Correct |
64 ms |
3664 KB |
Output is correct |
6 |
Correct |
76 ms |
4680 KB |
Output is correct |
7 |
Correct |
76 ms |
4684 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4680 KB |
Output is correct |
10 |
Correct |
223 ms |
21576 KB |
Output is correct |
11 |
Correct |
320 ms |
27984 KB |
Output is correct |
12 |
Correct |
324 ms |
27976 KB |
Output is correct |
13 |
Correct |
187 ms |
26696 KB |
Output is correct |
14 |
Correct |
251 ms |
27920 KB |
Output is correct |
15 |
Correct |
242 ms |
27988 KB |
Output is correct |
16 |
Correct |
153 ms |
21584 KB |
Output is correct |
17 |
Correct |
332 ms |
27900 KB |
Output is correct |
18 |
Correct |
109 ms |
22544 KB |
Output is correct |
19 |
Correct |
84 ms |
22356 KB |
Output is correct |
20 |
Correct |
97 ms |
22560 KB |
Output is correct |
21 |
Correct |
97 ms |
22344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
40 ms |
3412 KB |
Output is correct |
3 |
Correct |
56 ms |
3656 KB |
Output is correct |
4 |
Correct |
74 ms |
4688 KB |
Output is correct |
5 |
Correct |
64 ms |
3664 KB |
Output is correct |
6 |
Correct |
76 ms |
4680 KB |
Output is correct |
7 |
Correct |
76 ms |
4684 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4680 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
80 ms |
4168 KB |
Output is correct |
12 |
Correct |
80 ms |
4168 KB |
Output is correct |
13 |
Correct |
80 ms |
4168 KB |
Output is correct |
14 |
Correct |
80 ms |
4136 KB |
Output is correct |
15 |
Correct |
72 ms |
4424 KB |
Output is correct |
16 |
Correct |
38 ms |
3400 KB |
Output is correct |
17 |
Correct |
67 ms |
4424 KB |
Output is correct |
18 |
Correct |
82 ms |
4424 KB |
Output is correct |
19 |
Correct |
76 ms |
4428 KB |
Output is correct |
20 |
Correct |
71 ms |
4424 KB |
Output is correct |
21 |
Correct |
223 ms |
21576 KB |
Output is correct |
22 |
Correct |
320 ms |
27984 KB |
Output is correct |
23 |
Correct |
324 ms |
27976 KB |
Output is correct |
24 |
Correct |
187 ms |
26696 KB |
Output is correct |
25 |
Correct |
251 ms |
27920 KB |
Output is correct |
26 |
Correct |
242 ms |
27988 KB |
Output is correct |
27 |
Correct |
153 ms |
21584 KB |
Output is correct |
28 |
Correct |
332 ms |
27900 KB |
Output is correct |
29 |
Correct |
109 ms |
22544 KB |
Output is correct |
30 |
Correct |
84 ms |
22356 KB |
Output is correct |
31 |
Correct |
97 ms |
22560 KB |
Output is correct |
32 |
Correct |
97 ms |
22344 KB |
Output is correct |
33 |
Correct |
268 ms |
23316 KB |
Output is correct |
34 |
Correct |
430 ms |
23376 KB |
Output is correct |
35 |
Correct |
321 ms |
21328 KB |
Output is correct |
36 |
Correct |
291 ms |
21320 KB |
Output is correct |
37 |
Correct |
261 ms |
22344 KB |
Output is correct |
38 |
Correct |
265 ms |
20376 KB |
Output is correct |
39 |
Correct |
414 ms |
25864 KB |
Output is correct |
40 |
Correct |
474 ms |
17424 KB |
Output is correct |
41 |
Correct |
209 ms |
18248 KB |
Output is correct |
42 |
Correct |
311 ms |
23884 KB |
Output is correct |
43 |
Correct |
264 ms |
18260 KB |
Output is correct |
44 |
Correct |
315 ms |
19212 KB |
Output is correct |
45 |
Correct |
107 ms |
20552 KB |
Output is correct |
46 |
Correct |
106 ms |
19016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
787 ms |
17616 KB |
Output is correct |
4 |
Correct |
837 ms |
17684 KB |
Output is correct |
5 |
Correct |
823 ms |
17620 KB |
Output is correct |
6 |
Correct |
780 ms |
17504 KB |
Output is correct |
7 |
Correct |
836 ms |
17568 KB |
Output is correct |
8 |
Correct |
334 ms |
10316 KB |
Output is correct |
9 |
Correct |
587 ms |
17736 KB |
Output is correct |
10 |
Correct |
42 ms |
10212 KB |
Output is correct |
11 |
Correct |
128 ms |
22344 KB |
Output is correct |
12 |
Correct |
117 ms |
20552 KB |
Output is correct |
13 |
Correct |
104 ms |
19016 KB |
Output is correct |
14 |
Correct |
77 ms |
19940 KB |
Output is correct |
15 |
Correct |
81 ms |
19944 KB |
Output is correct |
16 |
Correct |
79 ms |
18532 KB |
Output is correct |
17 |
Correct |
94 ms |
23364 KB |
Output is correct |
18 |
Correct |
117 ms |
17784 KB |
Output is correct |
19 |
Correct |
548 ms |
17700 KB |
Output is correct |
20 |
Correct |
512 ms |
21240 KB |
Output is correct |
21 |
Correct |
409 ms |
21068 KB |
Output is correct |
22 |
Correct |
726 ms |
19492 KB |
Output is correct |
23 |
Correct |
521 ms |
19272 KB |
Output is correct |
24 |
Correct |
71 ms |
20936 KB |
Output is correct |
25 |
Correct |
63 ms |
21540 KB |
Output is correct |
26 |
Correct |
79 ms |
22528 KB |
Output is correct |
27 |
Correct |
99 ms |
22344 KB |
Output is correct |
28 |
Correct |
108 ms |
22456 KB |
Output is correct |
29 |
Correct |
98 ms |
22392 KB |
Output is correct |
30 |
Correct |
57 ms |
4376 KB |
Output is correct |
31 |
Correct |
110 ms |
2064 KB |
Output is correct |
32 |
Correct |
70 ms |
5892 KB |
Output is correct |
33 |
Correct |
55 ms |
2496 KB |
Output is correct |
34 |
Correct |
76 ms |
3920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
40 ms |
3412 KB |
Output is correct |
3 |
Correct |
56 ms |
3656 KB |
Output is correct |
4 |
Correct |
74 ms |
4688 KB |
Output is correct |
5 |
Correct |
64 ms |
3664 KB |
Output is correct |
6 |
Correct |
76 ms |
4680 KB |
Output is correct |
7 |
Correct |
76 ms |
4684 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4680 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
80 ms |
4168 KB |
Output is correct |
12 |
Correct |
80 ms |
4168 KB |
Output is correct |
13 |
Correct |
80 ms |
4168 KB |
Output is correct |
14 |
Correct |
80 ms |
4136 KB |
Output is correct |
15 |
Correct |
72 ms |
4424 KB |
Output is correct |
16 |
Correct |
38 ms |
3400 KB |
Output is correct |
17 |
Correct |
67 ms |
4424 KB |
Output is correct |
18 |
Correct |
82 ms |
4424 KB |
Output is correct |
19 |
Correct |
76 ms |
4428 KB |
Output is correct |
20 |
Correct |
71 ms |
4424 KB |
Output is correct |
21 |
Correct |
223 ms |
21576 KB |
Output is correct |
22 |
Correct |
320 ms |
27984 KB |
Output is correct |
23 |
Correct |
324 ms |
27976 KB |
Output is correct |
24 |
Correct |
187 ms |
26696 KB |
Output is correct |
25 |
Correct |
251 ms |
27920 KB |
Output is correct |
26 |
Correct |
242 ms |
27988 KB |
Output is correct |
27 |
Correct |
153 ms |
21584 KB |
Output is correct |
28 |
Correct |
332 ms |
27900 KB |
Output is correct |
29 |
Correct |
109 ms |
22544 KB |
Output is correct |
30 |
Correct |
84 ms |
22356 KB |
Output is correct |
31 |
Correct |
97 ms |
22560 KB |
Output is correct |
32 |
Correct |
97 ms |
22344 KB |
Output is correct |
33 |
Correct |
268 ms |
23316 KB |
Output is correct |
34 |
Correct |
430 ms |
23376 KB |
Output is correct |
35 |
Correct |
321 ms |
21328 KB |
Output is correct |
36 |
Correct |
291 ms |
21320 KB |
Output is correct |
37 |
Correct |
261 ms |
22344 KB |
Output is correct |
38 |
Correct |
265 ms |
20376 KB |
Output is correct |
39 |
Correct |
414 ms |
25864 KB |
Output is correct |
40 |
Correct |
474 ms |
17424 KB |
Output is correct |
41 |
Correct |
209 ms |
18248 KB |
Output is correct |
42 |
Correct |
311 ms |
23884 KB |
Output is correct |
43 |
Correct |
264 ms |
18260 KB |
Output is correct |
44 |
Correct |
315 ms |
19212 KB |
Output is correct |
45 |
Correct |
107 ms |
20552 KB |
Output is correct |
46 |
Correct |
106 ms |
19016 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
787 ms |
17616 KB |
Output is correct |
50 |
Correct |
837 ms |
17684 KB |
Output is correct |
51 |
Correct |
823 ms |
17620 KB |
Output is correct |
52 |
Correct |
780 ms |
17504 KB |
Output is correct |
53 |
Correct |
836 ms |
17568 KB |
Output is correct |
54 |
Correct |
334 ms |
10316 KB |
Output is correct |
55 |
Correct |
587 ms |
17736 KB |
Output is correct |
56 |
Correct |
42 ms |
10212 KB |
Output is correct |
57 |
Correct |
128 ms |
22344 KB |
Output is correct |
58 |
Correct |
117 ms |
20552 KB |
Output is correct |
59 |
Correct |
104 ms |
19016 KB |
Output is correct |
60 |
Correct |
77 ms |
19940 KB |
Output is correct |
61 |
Correct |
81 ms |
19944 KB |
Output is correct |
62 |
Correct |
79 ms |
18532 KB |
Output is correct |
63 |
Correct |
94 ms |
23364 KB |
Output is correct |
64 |
Correct |
117 ms |
17784 KB |
Output is correct |
65 |
Correct |
548 ms |
17700 KB |
Output is correct |
66 |
Correct |
512 ms |
21240 KB |
Output is correct |
67 |
Correct |
409 ms |
21068 KB |
Output is correct |
68 |
Correct |
726 ms |
19492 KB |
Output is correct |
69 |
Correct |
521 ms |
19272 KB |
Output is correct |
70 |
Correct |
71 ms |
20936 KB |
Output is correct |
71 |
Correct |
63 ms |
21540 KB |
Output is correct |
72 |
Correct |
79 ms |
22528 KB |
Output is correct |
73 |
Correct |
99 ms |
22344 KB |
Output is correct |
74 |
Correct |
108 ms |
22456 KB |
Output is correct |
75 |
Correct |
98 ms |
22392 KB |
Output is correct |
76 |
Correct |
57 ms |
4376 KB |
Output is correct |
77 |
Correct |
110 ms |
2064 KB |
Output is correct |
78 |
Correct |
70 ms |
5892 KB |
Output is correct |
79 |
Correct |
55 ms |
2496 KB |
Output is correct |
80 |
Correct |
76 ms |
3920 KB |
Output is correct |
81 |
Correct |
1699 ms |
21456 KB |
Output is correct |
82 |
Correct |
1800 ms |
21400 KB |
Output is correct |
83 |
Correct |
1667 ms |
21164 KB |
Output is correct |
84 |
Correct |
1756 ms |
21168 KB |
Output is correct |
85 |
Correct |
1724 ms |
21228 KB |
Output is correct |
86 |
Correct |
681 ms |
13224 KB |
Output is correct |
87 |
Correct |
1281 ms |
22096 KB |
Output is correct |
88 |
Correct |
1362 ms |
22088 KB |
Output is correct |
89 |
Correct |
1275 ms |
22044 KB |
Output is correct |
90 |
Correct |
519 ms |
13740 KB |
Output is correct |
91 |
Correct |
69 ms |
11204 KB |
Output is correct |
92 |
Correct |
163 ms |
24728 KB |
Output is correct |
93 |
Correct |
177 ms |
24624 KB |
Output is correct |
94 |
Correct |
165 ms |
23416 KB |
Output is correct |
95 |
Correct |
203 ms |
28700 KB |
Output is correct |
96 |
Correct |
315 ms |
22104 KB |
Output is correct |
97 |
Correct |
1631 ms |
21984 KB |
Output is correct |
98 |
Correct |
1691 ms |
26212 KB |
Output is correct |
99 |
Correct |
1348 ms |
26184 KB |
Output is correct |
100 |
Correct |
2294 ms |
24312 KB |
Output is correct |
101 |
Correct |
1631 ms |
24324 KB |
Output is correct |
102 |
Correct |
127 ms |
25788 KB |
Output is correct |
103 |
Correct |
110 ms |
26396 KB |
Output is correct |
104 |
Correct |
141 ms |
27644 KB |
Output is correct |
105 |
Correct |
114 ms |
5760 KB |
Output is correct |
106 |
Correct |
212 ms |
4288 KB |
Output is correct |
107 |
Correct |
198 ms |
9032 KB |
Output is correct |
108 |
Correct |
139 ms |
5236 KB |
Output is correct |
109 |
Correct |
222 ms |
6728 KB |
Output is correct |