#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 = 700;
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){
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
37 ms |
3320 KB |
Output is correct |
3 |
Correct |
51 ms |
3664 KB |
Output is correct |
4 |
Correct |
78 ms |
4692 KB |
Output is correct |
5 |
Correct |
59 ms |
3656 KB |
Output is correct |
6 |
Correct |
74 ms |
4496 KB |
Output is correct |
7 |
Correct |
72 ms |
4636 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
37 ms |
3320 KB |
Output is correct |
3 |
Correct |
51 ms |
3664 KB |
Output is correct |
4 |
Correct |
78 ms |
4692 KB |
Output is correct |
5 |
Correct |
59 ms |
3656 KB |
Output is correct |
6 |
Correct |
74 ms |
4496 KB |
Output is correct |
7 |
Correct |
72 ms |
4636 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
83 ms |
4088 KB |
Output is correct |
12 |
Correct |
83 ms |
4168 KB |
Output is correct |
13 |
Correct |
78 ms |
3972 KB |
Output is correct |
14 |
Correct |
78 ms |
4168 KB |
Output is correct |
15 |
Correct |
74 ms |
4424 KB |
Output is correct |
16 |
Correct |
38 ms |
3408 KB |
Output is correct |
17 |
Correct |
68 ms |
4564 KB |
Output is correct |
18 |
Correct |
80 ms |
4488 KB |
Output is correct |
19 |
Correct |
71 ms |
4512 KB |
Output is correct |
20 |
Correct |
68 ms |
4424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
37 ms |
3320 KB |
Output is correct |
3 |
Correct |
51 ms |
3664 KB |
Output is correct |
4 |
Correct |
78 ms |
4692 KB |
Output is correct |
5 |
Correct |
59 ms |
3656 KB |
Output is correct |
6 |
Correct |
74 ms |
4496 KB |
Output is correct |
7 |
Correct |
72 ms |
4636 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4544 KB |
Output is correct |
10 |
Correct |
207 ms |
21780 KB |
Output is correct |
11 |
Correct |
376 ms |
27976 KB |
Output is correct |
12 |
Correct |
361 ms |
28048 KB |
Output is correct |
13 |
Correct |
325 ms |
26696 KB |
Output is correct |
14 |
Correct |
244 ms |
27976 KB |
Output is correct |
15 |
Correct |
307 ms |
27976 KB |
Output is correct |
16 |
Correct |
160 ms |
21576 KB |
Output is correct |
17 |
Correct |
282 ms |
27976 KB |
Output is correct |
18 |
Correct |
160 ms |
22344 KB |
Output is correct |
19 |
Correct |
87 ms |
22344 KB |
Output is correct |
20 |
Correct |
100 ms |
22344 KB |
Output is correct |
21 |
Correct |
101 ms |
22344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
37 ms |
3320 KB |
Output is correct |
3 |
Correct |
51 ms |
3664 KB |
Output is correct |
4 |
Correct |
78 ms |
4692 KB |
Output is correct |
5 |
Correct |
59 ms |
3656 KB |
Output is correct |
6 |
Correct |
74 ms |
4496 KB |
Output is correct |
7 |
Correct |
72 ms |
4636 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
83 ms |
4088 KB |
Output is correct |
12 |
Correct |
83 ms |
4168 KB |
Output is correct |
13 |
Correct |
78 ms |
3972 KB |
Output is correct |
14 |
Correct |
78 ms |
4168 KB |
Output is correct |
15 |
Correct |
74 ms |
4424 KB |
Output is correct |
16 |
Correct |
38 ms |
3408 KB |
Output is correct |
17 |
Correct |
68 ms |
4564 KB |
Output is correct |
18 |
Correct |
80 ms |
4488 KB |
Output is correct |
19 |
Correct |
71 ms |
4512 KB |
Output is correct |
20 |
Correct |
68 ms |
4424 KB |
Output is correct |
21 |
Correct |
207 ms |
21780 KB |
Output is correct |
22 |
Correct |
376 ms |
27976 KB |
Output is correct |
23 |
Correct |
361 ms |
28048 KB |
Output is correct |
24 |
Correct |
325 ms |
26696 KB |
Output is correct |
25 |
Correct |
244 ms |
27976 KB |
Output is correct |
26 |
Correct |
307 ms |
27976 KB |
Output is correct |
27 |
Correct |
160 ms |
21576 KB |
Output is correct |
28 |
Correct |
282 ms |
27976 KB |
Output is correct |
29 |
Correct |
160 ms |
22344 KB |
Output is correct |
30 |
Correct |
87 ms |
22344 KB |
Output is correct |
31 |
Correct |
100 ms |
22344 KB |
Output is correct |
32 |
Correct |
101 ms |
22344 KB |
Output is correct |
33 |
Correct |
301 ms |
23304 KB |
Output is correct |
34 |
Correct |
378 ms |
23372 KB |
Output is correct |
35 |
Correct |
243 ms |
21324 KB |
Output is correct |
36 |
Correct |
243 ms |
21320 KB |
Output is correct |
37 |
Correct |
212 ms |
22344 KB |
Output is correct |
38 |
Correct |
198 ms |
20272 KB |
Output is correct |
39 |
Correct |
254 ms |
25788 KB |
Output is correct |
40 |
Correct |
235 ms |
17480 KB |
Output is correct |
41 |
Correct |
174 ms |
18248 KB |
Output is correct |
42 |
Correct |
226 ms |
23888 KB |
Output is correct |
43 |
Correct |
176 ms |
17992 KB |
Output is correct |
44 |
Correct |
213 ms |
19276 KB |
Output is correct |
45 |
Correct |
104 ms |
20552 KB |
Output is correct |
46 |
Correct |
101 ms |
19016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
813 ms |
17596 KB |
Output is correct |
4 |
Correct |
826 ms |
17480 KB |
Output is correct |
5 |
Correct |
945 ms |
17552 KB |
Output is correct |
6 |
Correct |
1018 ms |
17480 KB |
Output is correct |
7 |
Correct |
965 ms |
17484 KB |
Output is correct |
8 |
Correct |
320 ms |
10060 KB |
Output is correct |
9 |
Correct |
588 ms |
17888 KB |
Output is correct |
10 |
Correct |
41 ms |
10220 KB |
Output is correct |
11 |
Correct |
129 ms |
22608 KB |
Output is correct |
12 |
Correct |
104 ms |
20552 KB |
Output is correct |
13 |
Correct |
110 ms |
19016 KB |
Output is correct |
14 |
Correct |
87 ms |
19940 KB |
Output is correct |
15 |
Correct |
79 ms |
19892 KB |
Output is correct |
16 |
Correct |
77 ms |
18504 KB |
Output is correct |
17 |
Correct |
105 ms |
23364 KB |
Output is correct |
18 |
Correct |
120 ms |
17784 KB |
Output is correct |
19 |
Correct |
560 ms |
17480 KB |
Output is correct |
20 |
Correct |
491 ms |
21272 KB |
Output is correct |
21 |
Correct |
413 ms |
21064 KB |
Output is correct |
22 |
Correct |
680 ms |
19272 KB |
Output is correct |
23 |
Correct |
522 ms |
19272 KB |
Output is correct |
24 |
Correct |
64 ms |
20872 KB |
Output is correct |
25 |
Correct |
62 ms |
21396 KB |
Output is correct |
26 |
Correct |
77 ms |
22528 KB |
Output is correct |
27 |
Correct |
106 ms |
22252 KB |
Output is correct |
28 |
Correct |
108 ms |
22392 KB |
Output is correct |
29 |
Correct |
100 ms |
22344 KB |
Output is correct |
30 |
Correct |
57 ms |
4288 KB |
Output is correct |
31 |
Correct |
101 ms |
2060 KB |
Output is correct |
32 |
Correct |
77 ms |
5768 KB |
Output is correct |
33 |
Correct |
55 ms |
2496 KB |
Output is correct |
34 |
Correct |
78 ms |
3920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
37 ms |
3320 KB |
Output is correct |
3 |
Correct |
51 ms |
3664 KB |
Output is correct |
4 |
Correct |
78 ms |
4692 KB |
Output is correct |
5 |
Correct |
59 ms |
3656 KB |
Output is correct |
6 |
Correct |
74 ms |
4496 KB |
Output is correct |
7 |
Correct |
72 ms |
4636 KB |
Output is correct |
8 |
Correct |
52 ms |
3656 KB |
Output is correct |
9 |
Correct |
76 ms |
4544 KB |
Output is correct |
10 |
Correct |
1 ms |
504 KB |
Output is correct |
11 |
Correct |
83 ms |
4088 KB |
Output is correct |
12 |
Correct |
83 ms |
4168 KB |
Output is correct |
13 |
Correct |
78 ms |
3972 KB |
Output is correct |
14 |
Correct |
78 ms |
4168 KB |
Output is correct |
15 |
Correct |
74 ms |
4424 KB |
Output is correct |
16 |
Correct |
38 ms |
3408 KB |
Output is correct |
17 |
Correct |
68 ms |
4564 KB |
Output is correct |
18 |
Correct |
80 ms |
4488 KB |
Output is correct |
19 |
Correct |
71 ms |
4512 KB |
Output is correct |
20 |
Correct |
68 ms |
4424 KB |
Output is correct |
21 |
Correct |
207 ms |
21780 KB |
Output is correct |
22 |
Correct |
376 ms |
27976 KB |
Output is correct |
23 |
Correct |
361 ms |
28048 KB |
Output is correct |
24 |
Correct |
325 ms |
26696 KB |
Output is correct |
25 |
Correct |
244 ms |
27976 KB |
Output is correct |
26 |
Correct |
307 ms |
27976 KB |
Output is correct |
27 |
Correct |
160 ms |
21576 KB |
Output is correct |
28 |
Correct |
282 ms |
27976 KB |
Output is correct |
29 |
Correct |
160 ms |
22344 KB |
Output is correct |
30 |
Correct |
87 ms |
22344 KB |
Output is correct |
31 |
Correct |
100 ms |
22344 KB |
Output is correct |
32 |
Correct |
101 ms |
22344 KB |
Output is correct |
33 |
Correct |
301 ms |
23304 KB |
Output is correct |
34 |
Correct |
378 ms |
23372 KB |
Output is correct |
35 |
Correct |
243 ms |
21324 KB |
Output is correct |
36 |
Correct |
243 ms |
21320 KB |
Output is correct |
37 |
Correct |
212 ms |
22344 KB |
Output is correct |
38 |
Correct |
198 ms |
20272 KB |
Output is correct |
39 |
Correct |
254 ms |
25788 KB |
Output is correct |
40 |
Correct |
235 ms |
17480 KB |
Output is correct |
41 |
Correct |
174 ms |
18248 KB |
Output is correct |
42 |
Correct |
226 ms |
23888 KB |
Output is correct |
43 |
Correct |
176 ms |
17992 KB |
Output is correct |
44 |
Correct |
213 ms |
19276 KB |
Output is correct |
45 |
Correct |
104 ms |
20552 KB |
Output is correct |
46 |
Correct |
101 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 |
813 ms |
17596 KB |
Output is correct |
50 |
Correct |
826 ms |
17480 KB |
Output is correct |
51 |
Correct |
945 ms |
17552 KB |
Output is correct |
52 |
Correct |
1018 ms |
17480 KB |
Output is correct |
53 |
Correct |
965 ms |
17484 KB |
Output is correct |
54 |
Correct |
320 ms |
10060 KB |
Output is correct |
55 |
Correct |
588 ms |
17888 KB |
Output is correct |
56 |
Correct |
41 ms |
10220 KB |
Output is correct |
57 |
Correct |
129 ms |
22608 KB |
Output is correct |
58 |
Correct |
104 ms |
20552 KB |
Output is correct |
59 |
Correct |
110 ms |
19016 KB |
Output is correct |
60 |
Correct |
87 ms |
19940 KB |
Output is correct |
61 |
Correct |
79 ms |
19892 KB |
Output is correct |
62 |
Correct |
77 ms |
18504 KB |
Output is correct |
63 |
Correct |
105 ms |
23364 KB |
Output is correct |
64 |
Correct |
120 ms |
17784 KB |
Output is correct |
65 |
Correct |
560 ms |
17480 KB |
Output is correct |
66 |
Correct |
491 ms |
21272 KB |
Output is correct |
67 |
Correct |
413 ms |
21064 KB |
Output is correct |
68 |
Correct |
680 ms |
19272 KB |
Output is correct |
69 |
Correct |
522 ms |
19272 KB |
Output is correct |
70 |
Correct |
64 ms |
20872 KB |
Output is correct |
71 |
Correct |
62 ms |
21396 KB |
Output is correct |
72 |
Correct |
77 ms |
22528 KB |
Output is correct |
73 |
Correct |
106 ms |
22252 KB |
Output is correct |
74 |
Correct |
108 ms |
22392 KB |
Output is correct |
75 |
Correct |
100 ms |
22344 KB |
Output is correct |
76 |
Correct |
57 ms |
4288 KB |
Output is correct |
77 |
Correct |
101 ms |
2060 KB |
Output is correct |
78 |
Correct |
77 ms |
5768 KB |
Output is correct |
79 |
Correct |
55 ms |
2496 KB |
Output is correct |
80 |
Correct |
78 ms |
3920 KB |
Output is correct |
81 |
Correct |
1946 ms |
21284 KB |
Output is correct |
82 |
Correct |
1810 ms |
21320 KB |
Output is correct |
83 |
Correct |
1712 ms |
21096 KB |
Output is correct |
84 |
Correct |
1724 ms |
21080 KB |
Output is correct |
85 |
Correct |
1676 ms |
21064 KB |
Output is correct |
86 |
Correct |
668 ms |
13128 KB |
Output is correct |
87 |
Correct |
1213 ms |
22088 KB |
Output is correct |
88 |
Correct |
1359 ms |
21936 KB |
Output is correct |
89 |
Correct |
1431 ms |
21832 KB |
Output is correct |
90 |
Correct |
560 ms |
14152 KB |
Output is correct |
91 |
Correct |
70 ms |
11204 KB |
Output is correct |
92 |
Correct |
173 ms |
24696 KB |
Output is correct |
93 |
Correct |
205 ms |
24584 KB |
Output is correct |
94 |
Correct |
176 ms |
23368 KB |
Output is correct |
95 |
Correct |
219 ms |
28572 KB |
Output is correct |
96 |
Correct |
328 ms |
22116 KB |
Output is correct |
97 |
Correct |
1674 ms |
21840 KB |
Output is correct |
98 |
Correct |
1741 ms |
26248 KB |
Output is correct |
99 |
Correct |
1300 ms |
26028 KB |
Output is correct |
100 |
Correct |
2297 ms |
24348 KB |
Output is correct |
101 |
Correct |
1676 ms |
24316 KB |
Output is correct |
102 |
Correct |
141 ms |
25628 KB |
Output is correct |
103 |
Correct |
133 ms |
26448 KB |
Output is correct |
104 |
Correct |
155 ms |
27580 KB |
Output is correct |
105 |
Correct |
120 ms |
5624 KB |
Output is correct |
106 |
Correct |
246 ms |
4392 KB |
Output is correct |
107 |
Correct |
195 ms |
9092 KB |
Output is correct |
108 |
Correct |
150 ms |
5116 KB |
Output is correct |
109 |
Correct |
247 ms |
6676 KB |
Output is correct |