#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 = 500;
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 |
39 ms |
3412 KB |
Output is correct |
3 |
Correct |
54 ms |
3620 KB |
Output is correct |
4 |
Correct |
68 ms |
4680 KB |
Output is correct |
5 |
Correct |
59 ms |
3728 KB |
Output is correct |
6 |
Correct |
77 ms |
4584 KB |
Output is correct |
7 |
Correct |
72 ms |
4684 KB |
Output is correct |
8 |
Correct |
58 ms |
3576 KB |
Output is correct |
9 |
Correct |
74 ms |
4688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
39 ms |
3412 KB |
Output is correct |
3 |
Correct |
54 ms |
3620 KB |
Output is correct |
4 |
Correct |
68 ms |
4680 KB |
Output is correct |
5 |
Correct |
59 ms |
3728 KB |
Output is correct |
6 |
Correct |
77 ms |
4584 KB |
Output is correct |
7 |
Correct |
72 ms |
4684 KB |
Output is correct |
8 |
Correct |
58 ms |
3576 KB |
Output is correct |
9 |
Correct |
74 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
4024 KB |
Output is correct |
12 |
Correct |
80 ms |
4168 KB |
Output is correct |
13 |
Correct |
78 ms |
4168 KB |
Output is correct |
14 |
Correct |
75 ms |
4136 KB |
Output is correct |
15 |
Correct |
77 ms |
4384 KB |
Output is correct |
16 |
Correct |
38 ms |
3408 KB |
Output is correct |
17 |
Correct |
71 ms |
4424 KB |
Output is correct |
18 |
Correct |
77 ms |
4436 KB |
Output is correct |
19 |
Correct |
76 ms |
4424 KB |
Output is correct |
20 |
Correct |
74 ms |
4436 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
39 ms |
3412 KB |
Output is correct |
3 |
Correct |
54 ms |
3620 KB |
Output is correct |
4 |
Correct |
68 ms |
4680 KB |
Output is correct |
5 |
Correct |
59 ms |
3728 KB |
Output is correct |
6 |
Correct |
77 ms |
4584 KB |
Output is correct |
7 |
Correct |
72 ms |
4684 KB |
Output is correct |
8 |
Correct |
58 ms |
3576 KB |
Output is correct |
9 |
Correct |
74 ms |
4688 KB |
Output is correct |
10 |
Correct |
300 ms |
21588 KB |
Output is correct |
11 |
Correct |
290 ms |
27976 KB |
Output is correct |
12 |
Correct |
256 ms |
28028 KB |
Output is correct |
13 |
Correct |
171 ms |
26868 KB |
Output is correct |
14 |
Correct |
246 ms |
27976 KB |
Output is correct |
15 |
Correct |
258 ms |
27760 KB |
Output is correct |
16 |
Correct |
234 ms |
21576 KB |
Output is correct |
17 |
Correct |
329 ms |
27976 KB |
Output is correct |
18 |
Correct |
97 ms |
22344 KB |
Output is correct |
19 |
Correct |
92 ms |
22348 KB |
Output is correct |
20 |
Correct |
92 ms |
22532 KB |
Output is correct |
21 |
Correct |
93 ms |
22336 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
39 ms |
3412 KB |
Output is correct |
3 |
Correct |
54 ms |
3620 KB |
Output is correct |
4 |
Correct |
68 ms |
4680 KB |
Output is correct |
5 |
Correct |
59 ms |
3728 KB |
Output is correct |
6 |
Correct |
77 ms |
4584 KB |
Output is correct |
7 |
Correct |
72 ms |
4684 KB |
Output is correct |
8 |
Correct |
58 ms |
3576 KB |
Output is correct |
9 |
Correct |
74 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
4024 KB |
Output is correct |
12 |
Correct |
80 ms |
4168 KB |
Output is correct |
13 |
Correct |
78 ms |
4168 KB |
Output is correct |
14 |
Correct |
75 ms |
4136 KB |
Output is correct |
15 |
Correct |
77 ms |
4384 KB |
Output is correct |
16 |
Correct |
38 ms |
3408 KB |
Output is correct |
17 |
Correct |
71 ms |
4424 KB |
Output is correct |
18 |
Correct |
77 ms |
4436 KB |
Output is correct |
19 |
Correct |
76 ms |
4424 KB |
Output is correct |
20 |
Correct |
74 ms |
4436 KB |
Output is correct |
21 |
Correct |
300 ms |
21588 KB |
Output is correct |
22 |
Correct |
290 ms |
27976 KB |
Output is correct |
23 |
Correct |
256 ms |
28028 KB |
Output is correct |
24 |
Correct |
171 ms |
26868 KB |
Output is correct |
25 |
Correct |
246 ms |
27976 KB |
Output is correct |
26 |
Correct |
258 ms |
27760 KB |
Output is correct |
27 |
Correct |
234 ms |
21576 KB |
Output is correct |
28 |
Correct |
329 ms |
27976 KB |
Output is correct |
29 |
Correct |
97 ms |
22344 KB |
Output is correct |
30 |
Correct |
92 ms |
22348 KB |
Output is correct |
31 |
Correct |
92 ms |
22532 KB |
Output is correct |
32 |
Correct |
93 ms |
22336 KB |
Output is correct |
33 |
Correct |
269 ms |
23624 KB |
Output is correct |
34 |
Correct |
269 ms |
23368 KB |
Output is correct |
35 |
Correct |
248 ms |
21372 KB |
Output is correct |
36 |
Correct |
280 ms |
21320 KB |
Output is correct |
37 |
Correct |
223 ms |
22344 KB |
Output is correct |
38 |
Correct |
208 ms |
20296 KB |
Output is correct |
39 |
Correct |
282 ms |
25780 KB |
Output is correct |
40 |
Correct |
244 ms |
17480 KB |
Output is correct |
41 |
Correct |
196 ms |
18256 KB |
Output is correct |
42 |
Correct |
224 ms |
23888 KB |
Output is correct |
43 |
Correct |
174 ms |
18000 KB |
Output is correct |
44 |
Correct |
216 ms |
19180 KB |
Output is correct |
45 |
Correct |
130 ms |
20604 KB |
Output is correct |
46 |
Correct |
110 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 |
504 KB |
Output is correct |
3 |
Correct |
803 ms |
17572 KB |
Output is correct |
4 |
Correct |
761 ms |
17604 KB |
Output is correct |
5 |
Correct |
749 ms |
17596 KB |
Output is correct |
6 |
Correct |
788 ms |
17480 KB |
Output is correct |
7 |
Correct |
804 ms |
17572 KB |
Output is correct |
8 |
Correct |
308 ms |
10140 KB |
Output is correct |
9 |
Correct |
58 ms |
18328 KB |
Output is correct |
10 |
Correct |
40 ms |
10220 KB |
Output is correct |
11 |
Correct |
110 ms |
22344 KB |
Output is correct |
12 |
Correct |
101 ms |
20552 KB |
Output is correct |
13 |
Correct |
107 ms |
19016 KB |
Output is correct |
14 |
Correct |
89 ms |
19940 KB |
Output is correct |
15 |
Correct |
85 ms |
19904 KB |
Output is correct |
16 |
Correct |
80 ms |
18504 KB |
Output is correct |
17 |
Correct |
105 ms |
23364 KB |
Output is correct |
18 |
Correct |
117 ms |
17828 KB |
Output is correct |
19 |
Correct |
529 ms |
17680 KB |
Output is correct |
20 |
Correct |
503 ms |
21320 KB |
Output is correct |
21 |
Correct |
393 ms |
21064 KB |
Output is correct |
22 |
Correct |
692 ms |
19272 KB |
Output is correct |
23 |
Correct |
522 ms |
19288 KB |
Output is correct |
24 |
Correct |
67 ms |
20852 KB |
Output is correct |
25 |
Correct |
64 ms |
21396 KB |
Output is correct |
26 |
Correct |
67 ms |
22528 KB |
Output is correct |
27 |
Correct |
82 ms |
22344 KB |
Output is correct |
28 |
Correct |
110 ms |
22344 KB |
Output is correct |
29 |
Correct |
90 ms |
22352 KB |
Output is correct |
30 |
Correct |
56 ms |
4292 KB |
Output is correct |
31 |
Correct |
100 ms |
2052 KB |
Output is correct |
32 |
Correct |
70 ms |
5812 KB |
Output is correct |
33 |
Correct |
64 ms |
2496 KB |
Output is correct |
34 |
Correct |
83 ms |
3864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
39 ms |
3412 KB |
Output is correct |
3 |
Correct |
54 ms |
3620 KB |
Output is correct |
4 |
Correct |
68 ms |
4680 KB |
Output is correct |
5 |
Correct |
59 ms |
3728 KB |
Output is correct |
6 |
Correct |
77 ms |
4584 KB |
Output is correct |
7 |
Correct |
72 ms |
4684 KB |
Output is correct |
8 |
Correct |
58 ms |
3576 KB |
Output is correct |
9 |
Correct |
74 ms |
4688 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
4024 KB |
Output is correct |
12 |
Correct |
80 ms |
4168 KB |
Output is correct |
13 |
Correct |
78 ms |
4168 KB |
Output is correct |
14 |
Correct |
75 ms |
4136 KB |
Output is correct |
15 |
Correct |
77 ms |
4384 KB |
Output is correct |
16 |
Correct |
38 ms |
3408 KB |
Output is correct |
17 |
Correct |
71 ms |
4424 KB |
Output is correct |
18 |
Correct |
77 ms |
4436 KB |
Output is correct |
19 |
Correct |
76 ms |
4424 KB |
Output is correct |
20 |
Correct |
74 ms |
4436 KB |
Output is correct |
21 |
Correct |
300 ms |
21588 KB |
Output is correct |
22 |
Correct |
290 ms |
27976 KB |
Output is correct |
23 |
Correct |
256 ms |
28028 KB |
Output is correct |
24 |
Correct |
171 ms |
26868 KB |
Output is correct |
25 |
Correct |
246 ms |
27976 KB |
Output is correct |
26 |
Correct |
258 ms |
27760 KB |
Output is correct |
27 |
Correct |
234 ms |
21576 KB |
Output is correct |
28 |
Correct |
329 ms |
27976 KB |
Output is correct |
29 |
Correct |
97 ms |
22344 KB |
Output is correct |
30 |
Correct |
92 ms |
22348 KB |
Output is correct |
31 |
Correct |
92 ms |
22532 KB |
Output is correct |
32 |
Correct |
93 ms |
22336 KB |
Output is correct |
33 |
Correct |
269 ms |
23624 KB |
Output is correct |
34 |
Correct |
269 ms |
23368 KB |
Output is correct |
35 |
Correct |
248 ms |
21372 KB |
Output is correct |
36 |
Correct |
280 ms |
21320 KB |
Output is correct |
37 |
Correct |
223 ms |
22344 KB |
Output is correct |
38 |
Correct |
208 ms |
20296 KB |
Output is correct |
39 |
Correct |
282 ms |
25780 KB |
Output is correct |
40 |
Correct |
244 ms |
17480 KB |
Output is correct |
41 |
Correct |
196 ms |
18256 KB |
Output is correct |
42 |
Correct |
224 ms |
23888 KB |
Output is correct |
43 |
Correct |
174 ms |
18000 KB |
Output is correct |
44 |
Correct |
216 ms |
19180 KB |
Output is correct |
45 |
Correct |
130 ms |
20604 KB |
Output is correct |
46 |
Correct |
110 ms |
19016 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
504 KB |
Output is correct |
49 |
Correct |
803 ms |
17572 KB |
Output is correct |
50 |
Correct |
761 ms |
17604 KB |
Output is correct |
51 |
Correct |
749 ms |
17596 KB |
Output is correct |
52 |
Correct |
788 ms |
17480 KB |
Output is correct |
53 |
Correct |
804 ms |
17572 KB |
Output is correct |
54 |
Correct |
308 ms |
10140 KB |
Output is correct |
55 |
Correct |
58 ms |
18328 KB |
Output is correct |
56 |
Correct |
40 ms |
10220 KB |
Output is correct |
57 |
Correct |
110 ms |
22344 KB |
Output is correct |
58 |
Correct |
101 ms |
20552 KB |
Output is correct |
59 |
Correct |
107 ms |
19016 KB |
Output is correct |
60 |
Correct |
89 ms |
19940 KB |
Output is correct |
61 |
Correct |
85 ms |
19904 KB |
Output is correct |
62 |
Correct |
80 ms |
18504 KB |
Output is correct |
63 |
Correct |
105 ms |
23364 KB |
Output is correct |
64 |
Correct |
117 ms |
17828 KB |
Output is correct |
65 |
Correct |
529 ms |
17680 KB |
Output is correct |
66 |
Correct |
503 ms |
21320 KB |
Output is correct |
67 |
Correct |
393 ms |
21064 KB |
Output is correct |
68 |
Correct |
692 ms |
19272 KB |
Output is correct |
69 |
Correct |
522 ms |
19288 KB |
Output is correct |
70 |
Correct |
67 ms |
20852 KB |
Output is correct |
71 |
Correct |
64 ms |
21396 KB |
Output is correct |
72 |
Correct |
67 ms |
22528 KB |
Output is correct |
73 |
Correct |
82 ms |
22344 KB |
Output is correct |
74 |
Correct |
110 ms |
22344 KB |
Output is correct |
75 |
Correct |
90 ms |
22352 KB |
Output is correct |
76 |
Correct |
56 ms |
4292 KB |
Output is correct |
77 |
Correct |
100 ms |
2052 KB |
Output is correct |
78 |
Correct |
70 ms |
5812 KB |
Output is correct |
79 |
Correct |
64 ms |
2496 KB |
Output is correct |
80 |
Correct |
83 ms |
3864 KB |
Output is correct |
81 |
Correct |
1812 ms |
21452 KB |
Output is correct |
82 |
Correct |
1746 ms |
21328 KB |
Output is correct |
83 |
Correct |
1670 ms |
21252 KB |
Output is correct |
84 |
Correct |
1710 ms |
21048 KB |
Output is correct |
85 |
Correct |
1723 ms |
21072 KB |
Output is correct |
86 |
Correct |
665 ms |
13132 KB |
Output is correct |
87 |
Correct |
1237 ms |
21980 KB |
Output is correct |
88 |
Correct |
1340 ms |
21900 KB |
Output is correct |
89 |
Correct |
1425 ms |
22024 KB |
Output is correct |
90 |
Correct |
538 ms |
13812 KB |
Output is correct |
91 |
Correct |
66 ms |
10952 KB |
Output is correct |
92 |
Correct |
188 ms |
24780 KB |
Output is correct |
93 |
Correct |
201 ms |
24624 KB |
Output is correct |
94 |
Correct |
184 ms |
23408 KB |
Output is correct |
95 |
Correct |
222 ms |
28700 KB |
Output is correct |
96 |
Correct |
352 ms |
22220 KB |
Output is correct |
97 |
Correct |
1700 ms |
21816 KB |
Output is correct |
98 |
Correct |
1739 ms |
26268 KB |
Output is correct |
99 |
Correct |
1275 ms |
26068 KB |
Output is correct |
100 |
Correct |
2287 ms |
24304 KB |
Output is correct |
101 |
Correct |
1767 ms |
24124 KB |
Output is correct |
102 |
Correct |
144 ms |
25832 KB |
Output is correct |
103 |
Correct |
128 ms |
26392 KB |
Output is correct |
104 |
Correct |
170 ms |
27640 KB |
Output is correct |
105 |
Correct |
114 ms |
5760 KB |
Output is correct |
106 |
Correct |
227 ms |
4288 KB |
Output is correct |
107 |
Correct |
189 ms |
9032 KB |
Output is correct |
108 |
Correct |
135 ms |
5040 KB |
Output is correct |
109 |
Correct |
218 ms |
6984 KB |
Output is correct |