#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 = 300;
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 |
44 ms |
3400 KB |
Output is correct |
3 |
Correct |
55 ms |
3656 KB |
Output is correct |
4 |
Correct |
73 ms |
4680 KB |
Output is correct |
5 |
Correct |
63 ms |
3656 KB |
Output is correct |
6 |
Correct |
93 ms |
4720 KB |
Output is correct |
7 |
Correct |
88 ms |
4768 KB |
Output is correct |
8 |
Correct |
54 ms |
3828 KB |
Output is correct |
9 |
Correct |
71 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
44 ms |
3400 KB |
Output is correct |
3 |
Correct |
55 ms |
3656 KB |
Output is correct |
4 |
Correct |
73 ms |
4680 KB |
Output is correct |
5 |
Correct |
63 ms |
3656 KB |
Output is correct |
6 |
Correct |
93 ms |
4720 KB |
Output is correct |
7 |
Correct |
88 ms |
4768 KB |
Output is correct |
8 |
Correct |
54 ms |
3828 KB |
Output is correct |
9 |
Correct |
71 ms |
4692 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
4152 KB |
Output is correct |
12 |
Correct |
86 ms |
4168 KB |
Output is correct |
13 |
Correct |
82 ms |
4168 KB |
Output is correct |
14 |
Correct |
89 ms |
4124 KB |
Output is correct |
15 |
Correct |
78 ms |
4424 KB |
Output is correct |
16 |
Correct |
52 ms |
3256 KB |
Output is correct |
17 |
Correct |
68 ms |
4512 KB |
Output is correct |
18 |
Correct |
78 ms |
4424 KB |
Output is correct |
19 |
Correct |
79 ms |
4588 KB |
Output is correct |
20 |
Correct |
77 ms |
4248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
44 ms |
3400 KB |
Output is correct |
3 |
Correct |
55 ms |
3656 KB |
Output is correct |
4 |
Correct |
73 ms |
4680 KB |
Output is correct |
5 |
Correct |
63 ms |
3656 KB |
Output is correct |
6 |
Correct |
93 ms |
4720 KB |
Output is correct |
7 |
Correct |
88 ms |
4768 KB |
Output is correct |
8 |
Correct |
54 ms |
3828 KB |
Output is correct |
9 |
Correct |
71 ms |
4692 KB |
Output is correct |
10 |
Correct |
191 ms |
21640 KB |
Output is correct |
11 |
Correct |
306 ms |
27976 KB |
Output is correct |
12 |
Correct |
316 ms |
27976 KB |
Output is correct |
13 |
Correct |
206 ms |
26696 KB |
Output is correct |
14 |
Correct |
267 ms |
27976 KB |
Output is correct |
15 |
Correct |
245 ms |
27776 KB |
Output is correct |
16 |
Correct |
164 ms |
21576 KB |
Output is correct |
17 |
Correct |
286 ms |
27976 KB |
Output is correct |
18 |
Correct |
117 ms |
22572 KB |
Output is correct |
19 |
Correct |
92 ms |
22344 KB |
Output is correct |
20 |
Correct |
94 ms |
22400 KB |
Output is correct |
21 |
Correct |
111 ms |
22344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
44 ms |
3400 KB |
Output is correct |
3 |
Correct |
55 ms |
3656 KB |
Output is correct |
4 |
Correct |
73 ms |
4680 KB |
Output is correct |
5 |
Correct |
63 ms |
3656 KB |
Output is correct |
6 |
Correct |
93 ms |
4720 KB |
Output is correct |
7 |
Correct |
88 ms |
4768 KB |
Output is correct |
8 |
Correct |
54 ms |
3828 KB |
Output is correct |
9 |
Correct |
71 ms |
4692 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
4152 KB |
Output is correct |
12 |
Correct |
86 ms |
4168 KB |
Output is correct |
13 |
Correct |
82 ms |
4168 KB |
Output is correct |
14 |
Correct |
89 ms |
4124 KB |
Output is correct |
15 |
Correct |
78 ms |
4424 KB |
Output is correct |
16 |
Correct |
52 ms |
3256 KB |
Output is correct |
17 |
Correct |
68 ms |
4512 KB |
Output is correct |
18 |
Correct |
78 ms |
4424 KB |
Output is correct |
19 |
Correct |
79 ms |
4588 KB |
Output is correct |
20 |
Correct |
77 ms |
4248 KB |
Output is correct |
21 |
Correct |
191 ms |
21640 KB |
Output is correct |
22 |
Correct |
306 ms |
27976 KB |
Output is correct |
23 |
Correct |
316 ms |
27976 KB |
Output is correct |
24 |
Correct |
206 ms |
26696 KB |
Output is correct |
25 |
Correct |
267 ms |
27976 KB |
Output is correct |
26 |
Correct |
245 ms |
27776 KB |
Output is correct |
27 |
Correct |
164 ms |
21576 KB |
Output is correct |
28 |
Correct |
286 ms |
27976 KB |
Output is correct |
29 |
Correct |
117 ms |
22572 KB |
Output is correct |
30 |
Correct |
92 ms |
22344 KB |
Output is correct |
31 |
Correct |
94 ms |
22400 KB |
Output is correct |
32 |
Correct |
111 ms |
22344 KB |
Output is correct |
33 |
Correct |
300 ms |
23472 KB |
Output is correct |
34 |
Correct |
294 ms |
23368 KB |
Output is correct |
35 |
Correct |
274 ms |
21288 KB |
Output is correct |
36 |
Correct |
252 ms |
21320 KB |
Output is correct |
37 |
Correct |
244 ms |
22344 KB |
Output is correct |
38 |
Correct |
195 ms |
20296 KB |
Output is correct |
39 |
Correct |
275 ms |
25928 KB |
Output is correct |
40 |
Correct |
253 ms |
17448 KB |
Output is correct |
41 |
Correct |
187 ms |
18248 KB |
Output is correct |
42 |
Correct |
232 ms |
23880 KB |
Output is correct |
43 |
Correct |
187 ms |
17992 KB |
Output is correct |
44 |
Correct |
225 ms |
19016 KB |
Output is correct |
45 |
Correct |
103 ms |
20552 KB |
Output is correct |
46 |
Correct |
125 ms |
19212 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 |
824 ms |
17500 KB |
Output is correct |
4 |
Correct |
809 ms |
17636 KB |
Output is correct |
5 |
Correct |
809 ms |
17600 KB |
Output is correct |
6 |
Correct |
813 ms |
17480 KB |
Output is correct |
7 |
Correct |
817 ms |
17480 KB |
Output is correct |
8 |
Correct |
313 ms |
10016 KB |
Output is correct |
9 |
Correct |
54 ms |
18324 KB |
Output is correct |
10 |
Correct |
42 ms |
10220 KB |
Output is correct |
11 |
Correct |
155 ms |
25672 KB |
Output is correct |
12 |
Correct |
131 ms |
23380 KB |
Output is correct |
13 |
Correct |
138 ms |
21832 KB |
Output is correct |
14 |
Correct |
81 ms |
22500 KB |
Output is correct |
15 |
Correct |
86 ms |
22496 KB |
Output is correct |
16 |
Correct |
76 ms |
21320 KB |
Output is correct |
17 |
Correct |
92 ms |
26088 KB |
Output is correct |
18 |
Correct |
116 ms |
20456 KB |
Output is correct |
19 |
Correct |
331 ms |
20552 KB |
Output is correct |
20 |
Correct |
480 ms |
23760 KB |
Output is correct |
21 |
Correct |
421 ms |
23940 KB |
Output is correct |
22 |
Correct |
660 ms |
22064 KB |
Output is correct |
23 |
Correct |
502 ms |
22168 KB |
Output is correct |
24 |
Correct |
64 ms |
23672 KB |
Output is correct |
25 |
Correct |
65 ms |
24148 KB |
Output is correct |
26 |
Correct |
70 ms |
25344 KB |
Output is correct |
27 |
Correct |
90 ms |
24904 KB |
Output is correct |
28 |
Correct |
93 ms |
25428 KB |
Output is correct |
29 |
Correct |
89 ms |
25416 KB |
Output is correct |
30 |
Correct |
58 ms |
7104 KB |
Output is correct |
31 |
Correct |
86 ms |
4356 KB |
Output is correct |
32 |
Correct |
69 ms |
8520 KB |
Output is correct |
33 |
Correct |
52 ms |
4800 KB |
Output is correct |
34 |
Correct |
76 ms |
6216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
44 ms |
3400 KB |
Output is correct |
3 |
Correct |
55 ms |
3656 KB |
Output is correct |
4 |
Correct |
73 ms |
4680 KB |
Output is correct |
5 |
Correct |
63 ms |
3656 KB |
Output is correct |
6 |
Correct |
93 ms |
4720 KB |
Output is correct |
7 |
Correct |
88 ms |
4768 KB |
Output is correct |
8 |
Correct |
54 ms |
3828 KB |
Output is correct |
9 |
Correct |
71 ms |
4692 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
83 ms |
4152 KB |
Output is correct |
12 |
Correct |
86 ms |
4168 KB |
Output is correct |
13 |
Correct |
82 ms |
4168 KB |
Output is correct |
14 |
Correct |
89 ms |
4124 KB |
Output is correct |
15 |
Correct |
78 ms |
4424 KB |
Output is correct |
16 |
Correct |
52 ms |
3256 KB |
Output is correct |
17 |
Correct |
68 ms |
4512 KB |
Output is correct |
18 |
Correct |
78 ms |
4424 KB |
Output is correct |
19 |
Correct |
79 ms |
4588 KB |
Output is correct |
20 |
Correct |
77 ms |
4248 KB |
Output is correct |
21 |
Correct |
191 ms |
21640 KB |
Output is correct |
22 |
Correct |
306 ms |
27976 KB |
Output is correct |
23 |
Correct |
316 ms |
27976 KB |
Output is correct |
24 |
Correct |
206 ms |
26696 KB |
Output is correct |
25 |
Correct |
267 ms |
27976 KB |
Output is correct |
26 |
Correct |
245 ms |
27776 KB |
Output is correct |
27 |
Correct |
164 ms |
21576 KB |
Output is correct |
28 |
Correct |
286 ms |
27976 KB |
Output is correct |
29 |
Correct |
117 ms |
22572 KB |
Output is correct |
30 |
Correct |
92 ms |
22344 KB |
Output is correct |
31 |
Correct |
94 ms |
22400 KB |
Output is correct |
32 |
Correct |
111 ms |
22344 KB |
Output is correct |
33 |
Correct |
300 ms |
23472 KB |
Output is correct |
34 |
Correct |
294 ms |
23368 KB |
Output is correct |
35 |
Correct |
274 ms |
21288 KB |
Output is correct |
36 |
Correct |
252 ms |
21320 KB |
Output is correct |
37 |
Correct |
244 ms |
22344 KB |
Output is correct |
38 |
Correct |
195 ms |
20296 KB |
Output is correct |
39 |
Correct |
275 ms |
25928 KB |
Output is correct |
40 |
Correct |
253 ms |
17448 KB |
Output is correct |
41 |
Correct |
187 ms |
18248 KB |
Output is correct |
42 |
Correct |
232 ms |
23880 KB |
Output is correct |
43 |
Correct |
187 ms |
17992 KB |
Output is correct |
44 |
Correct |
225 ms |
19016 KB |
Output is correct |
45 |
Correct |
103 ms |
20552 KB |
Output is correct |
46 |
Correct |
125 ms |
19212 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
824 ms |
17500 KB |
Output is correct |
50 |
Correct |
809 ms |
17636 KB |
Output is correct |
51 |
Correct |
809 ms |
17600 KB |
Output is correct |
52 |
Correct |
813 ms |
17480 KB |
Output is correct |
53 |
Correct |
817 ms |
17480 KB |
Output is correct |
54 |
Correct |
313 ms |
10016 KB |
Output is correct |
55 |
Correct |
54 ms |
18324 KB |
Output is correct |
56 |
Correct |
42 ms |
10220 KB |
Output is correct |
57 |
Correct |
155 ms |
25672 KB |
Output is correct |
58 |
Correct |
131 ms |
23380 KB |
Output is correct |
59 |
Correct |
138 ms |
21832 KB |
Output is correct |
60 |
Correct |
81 ms |
22500 KB |
Output is correct |
61 |
Correct |
86 ms |
22496 KB |
Output is correct |
62 |
Correct |
76 ms |
21320 KB |
Output is correct |
63 |
Correct |
92 ms |
26088 KB |
Output is correct |
64 |
Correct |
116 ms |
20456 KB |
Output is correct |
65 |
Correct |
331 ms |
20552 KB |
Output is correct |
66 |
Correct |
480 ms |
23760 KB |
Output is correct |
67 |
Correct |
421 ms |
23940 KB |
Output is correct |
68 |
Correct |
660 ms |
22064 KB |
Output is correct |
69 |
Correct |
502 ms |
22168 KB |
Output is correct |
70 |
Correct |
64 ms |
23672 KB |
Output is correct |
71 |
Correct |
65 ms |
24148 KB |
Output is correct |
72 |
Correct |
70 ms |
25344 KB |
Output is correct |
73 |
Correct |
90 ms |
24904 KB |
Output is correct |
74 |
Correct |
93 ms |
25428 KB |
Output is correct |
75 |
Correct |
89 ms |
25416 KB |
Output is correct |
76 |
Correct |
58 ms |
7104 KB |
Output is correct |
77 |
Correct |
86 ms |
4356 KB |
Output is correct |
78 |
Correct |
69 ms |
8520 KB |
Output is correct |
79 |
Correct |
52 ms |
4800 KB |
Output is correct |
80 |
Correct |
76 ms |
6216 KB |
Output is correct |
81 |
Correct |
1979 ms |
25532 KB |
Output is correct |
82 |
Correct |
1946 ms |
25416 KB |
Output is correct |
83 |
Correct |
1722 ms |
25260 KB |
Output is correct |
84 |
Correct |
1787 ms |
25380 KB |
Output is correct |
85 |
Correct |
1832 ms |
25416 KB |
Output is correct |
86 |
Correct |
680 ms |
17480 KB |
Output is correct |
87 |
Correct |
123 ms |
27644 KB |
Output is correct |
88 |
Correct |
1332 ms |
26248 KB |
Output is correct |
89 |
Correct |
1404 ms |
26184 KB |
Output is correct |
90 |
Correct |
528 ms |
17992 KB |
Output is correct |
91 |
Correct |
70 ms |
13872 KB |
Output is correct |
92 |
Correct |
178 ms |
30160 KB |
Output is correct |
93 |
Correct |
177 ms |
29700 KB |
Output is correct |
94 |
Correct |
178 ms |
28724 KB |
Output is correct |
95 |
Correct |
215 ms |
33820 KB |
Output is correct |
96 |
Correct |
405 ms |
26572 KB |
Output is correct |
97 |
Correct |
990 ms |
26440 KB |
Output is correct |
98 |
Correct |
1816 ms |
30792 KB |
Output is correct |
99 |
Correct |
1329 ms |
31148 KB |
Output is correct |
100 |
Correct |
2463 ms |
28900 KB |
Output is correct |
101 |
Correct |
1749 ms |
29084 KB |
Output is correct |
102 |
Correct |
154 ms |
30504 KB |
Output is correct |
103 |
Correct |
105 ms |
30836 KB |
Output is correct |
104 |
Correct |
143 ms |
32644 KB |
Output is correct |
105 |
Correct |
111 ms |
10272 KB |
Output is correct |
106 |
Correct |
202 ms |
7884 KB |
Output is correct |
107 |
Correct |
206 ms |
13788 KB |
Output is correct |
108 |
Correct |
146 ms |
9220 KB |
Output is correct |
109 |
Correct |
231 ms |
10824 KB |
Output is correct |