#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;
};
int q; cin>>q;
for (int i = 1; i <= q; i++){
int l, r; cin>>l>>r;
cout<<get(l, r)<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
3400 KB |
Output is correct |
3 |
Correct |
50 ms |
3656 KB |
Output is correct |
4 |
Correct |
69 ms |
4584 KB |
Output is correct |
5 |
Correct |
55 ms |
3764 KB |
Output is correct |
6 |
Correct |
70 ms |
4672 KB |
Output is correct |
7 |
Correct |
74 ms |
4596 KB |
Output is correct |
8 |
Correct |
51 ms |
3568 KB |
Output is correct |
9 |
Correct |
70 ms |
4692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
3400 KB |
Output is correct |
3 |
Correct |
50 ms |
3656 KB |
Output is correct |
4 |
Correct |
69 ms |
4584 KB |
Output is correct |
5 |
Correct |
55 ms |
3764 KB |
Output is correct |
6 |
Correct |
70 ms |
4672 KB |
Output is correct |
7 |
Correct |
74 ms |
4596 KB |
Output is correct |
8 |
Correct |
51 ms |
3568 KB |
Output is correct |
9 |
Correct |
70 ms |
4692 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
87 ms |
4244 KB |
Output is correct |
12 |
Correct |
78 ms |
4168 KB |
Output is correct |
13 |
Correct |
74 ms |
4108 KB |
Output is correct |
14 |
Correct |
75 ms |
4172 KB |
Output is correct |
15 |
Correct |
82 ms |
4384 KB |
Output is correct |
16 |
Correct |
39 ms |
3408 KB |
Output is correct |
17 |
Correct |
68 ms |
4436 KB |
Output is correct |
18 |
Correct |
70 ms |
4424 KB |
Output is correct |
19 |
Correct |
69 ms |
4424 KB |
Output is correct |
20 |
Correct |
67 ms |
4424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
3400 KB |
Output is correct |
3 |
Correct |
50 ms |
3656 KB |
Output is correct |
4 |
Correct |
69 ms |
4584 KB |
Output is correct |
5 |
Correct |
55 ms |
3764 KB |
Output is correct |
6 |
Correct |
70 ms |
4672 KB |
Output is correct |
7 |
Correct |
74 ms |
4596 KB |
Output is correct |
8 |
Correct |
51 ms |
3568 KB |
Output is correct |
9 |
Correct |
70 ms |
4692 KB |
Output is correct |
10 |
Correct |
227 ms |
23412 KB |
Output is correct |
11 |
Correct |
415 ms |
33488 KB |
Output is correct |
12 |
Correct |
331 ms |
33352 KB |
Output is correct |
13 |
Correct |
264 ms |
31492 KB |
Output is correct |
14 |
Correct |
298 ms |
33352 KB |
Output is correct |
15 |
Correct |
325 ms |
33352 KB |
Output is correct |
16 |
Correct |
185 ms |
25672 KB |
Output is correct |
17 |
Correct |
322 ms |
33548 KB |
Output is correct |
18 |
Correct |
124 ms |
25416 KB |
Output is correct |
19 |
Correct |
94 ms |
24912 KB |
Output is correct |
20 |
Correct |
104 ms |
25416 KB |
Output is correct |
21 |
Correct |
138 ms |
25380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
3400 KB |
Output is correct |
3 |
Correct |
50 ms |
3656 KB |
Output is correct |
4 |
Correct |
69 ms |
4584 KB |
Output is correct |
5 |
Correct |
55 ms |
3764 KB |
Output is correct |
6 |
Correct |
70 ms |
4672 KB |
Output is correct |
7 |
Correct |
74 ms |
4596 KB |
Output is correct |
8 |
Correct |
51 ms |
3568 KB |
Output is correct |
9 |
Correct |
70 ms |
4692 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
87 ms |
4244 KB |
Output is correct |
12 |
Correct |
78 ms |
4168 KB |
Output is correct |
13 |
Correct |
74 ms |
4108 KB |
Output is correct |
14 |
Correct |
75 ms |
4172 KB |
Output is correct |
15 |
Correct |
82 ms |
4384 KB |
Output is correct |
16 |
Correct |
39 ms |
3408 KB |
Output is correct |
17 |
Correct |
68 ms |
4436 KB |
Output is correct |
18 |
Correct |
70 ms |
4424 KB |
Output is correct |
19 |
Correct |
69 ms |
4424 KB |
Output is correct |
20 |
Correct |
67 ms |
4424 KB |
Output is correct |
21 |
Correct |
227 ms |
23412 KB |
Output is correct |
22 |
Correct |
415 ms |
33488 KB |
Output is correct |
23 |
Correct |
331 ms |
33352 KB |
Output is correct |
24 |
Correct |
264 ms |
31492 KB |
Output is correct |
25 |
Correct |
298 ms |
33352 KB |
Output is correct |
26 |
Correct |
325 ms |
33352 KB |
Output is correct |
27 |
Correct |
185 ms |
25672 KB |
Output is correct |
28 |
Correct |
322 ms |
33548 KB |
Output is correct |
29 |
Correct |
124 ms |
25416 KB |
Output is correct |
30 |
Correct |
94 ms |
24912 KB |
Output is correct |
31 |
Correct |
104 ms |
25416 KB |
Output is correct |
32 |
Correct |
138 ms |
25380 KB |
Output is correct |
33 |
Correct |
279 ms |
28492 KB |
Output is correct |
34 |
Correct |
285 ms |
28488 KB |
Output is correct |
35 |
Correct |
252 ms |
26252 KB |
Output is correct |
36 |
Correct |
308 ms |
26272 KB |
Output is correct |
37 |
Correct |
220 ms |
27464 KB |
Output is correct |
38 |
Correct |
210 ms |
24392 KB |
Output is correct |
39 |
Correct |
289 ms |
31048 KB |
Output is correct |
40 |
Correct |
241 ms |
22088 KB |
Output is correct |
41 |
Correct |
189 ms |
22720 KB |
Output is correct |
42 |
Correct |
218 ms |
29256 KB |
Output is correct |
43 |
Correct |
184 ms |
22868 KB |
Output is correct |
44 |
Correct |
223 ms |
24392 KB |
Output is correct |
45 |
Correct |
108 ms |
23576 KB |
Output is correct |
46 |
Correct |
101 ms |
21832 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 |
767 ms |
17740 KB |
Output is correct |
4 |
Correct |
753 ms |
20080 KB |
Output is correct |
5 |
Correct |
803 ms |
20016 KB |
Output is correct |
6 |
Correct |
827 ms |
19792 KB |
Output is correct |
7 |
Correct |
839 ms |
20040 KB |
Output is correct |
8 |
Correct |
371 ms |
12564 KB |
Output is correct |
9 |
Correct |
752 ms |
20320 KB |
Output is correct |
10 |
Execution timed out |
3052 ms |
10468 KB |
Time limit exceeded |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
36 ms |
3400 KB |
Output is correct |
3 |
Correct |
50 ms |
3656 KB |
Output is correct |
4 |
Correct |
69 ms |
4584 KB |
Output is correct |
5 |
Correct |
55 ms |
3764 KB |
Output is correct |
6 |
Correct |
70 ms |
4672 KB |
Output is correct |
7 |
Correct |
74 ms |
4596 KB |
Output is correct |
8 |
Correct |
51 ms |
3568 KB |
Output is correct |
9 |
Correct |
70 ms |
4692 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
87 ms |
4244 KB |
Output is correct |
12 |
Correct |
78 ms |
4168 KB |
Output is correct |
13 |
Correct |
74 ms |
4108 KB |
Output is correct |
14 |
Correct |
75 ms |
4172 KB |
Output is correct |
15 |
Correct |
82 ms |
4384 KB |
Output is correct |
16 |
Correct |
39 ms |
3408 KB |
Output is correct |
17 |
Correct |
68 ms |
4436 KB |
Output is correct |
18 |
Correct |
70 ms |
4424 KB |
Output is correct |
19 |
Correct |
69 ms |
4424 KB |
Output is correct |
20 |
Correct |
67 ms |
4424 KB |
Output is correct |
21 |
Correct |
227 ms |
23412 KB |
Output is correct |
22 |
Correct |
415 ms |
33488 KB |
Output is correct |
23 |
Correct |
331 ms |
33352 KB |
Output is correct |
24 |
Correct |
264 ms |
31492 KB |
Output is correct |
25 |
Correct |
298 ms |
33352 KB |
Output is correct |
26 |
Correct |
325 ms |
33352 KB |
Output is correct |
27 |
Correct |
185 ms |
25672 KB |
Output is correct |
28 |
Correct |
322 ms |
33548 KB |
Output is correct |
29 |
Correct |
124 ms |
25416 KB |
Output is correct |
30 |
Correct |
94 ms |
24912 KB |
Output is correct |
31 |
Correct |
104 ms |
25416 KB |
Output is correct |
32 |
Correct |
138 ms |
25380 KB |
Output is correct |
33 |
Correct |
279 ms |
28492 KB |
Output is correct |
34 |
Correct |
285 ms |
28488 KB |
Output is correct |
35 |
Correct |
252 ms |
26252 KB |
Output is correct |
36 |
Correct |
308 ms |
26272 KB |
Output is correct |
37 |
Correct |
220 ms |
27464 KB |
Output is correct |
38 |
Correct |
210 ms |
24392 KB |
Output is correct |
39 |
Correct |
289 ms |
31048 KB |
Output is correct |
40 |
Correct |
241 ms |
22088 KB |
Output is correct |
41 |
Correct |
189 ms |
22720 KB |
Output is correct |
42 |
Correct |
218 ms |
29256 KB |
Output is correct |
43 |
Correct |
184 ms |
22868 KB |
Output is correct |
44 |
Correct |
223 ms |
24392 KB |
Output is correct |
45 |
Correct |
108 ms |
23576 KB |
Output is correct |
46 |
Correct |
101 ms |
21832 KB |
Output is correct |
47 |
Correct |
1 ms |
336 KB |
Output is correct |
48 |
Correct |
1 ms |
336 KB |
Output is correct |
49 |
Correct |
767 ms |
17740 KB |
Output is correct |
50 |
Correct |
753 ms |
20080 KB |
Output is correct |
51 |
Correct |
803 ms |
20016 KB |
Output is correct |
52 |
Correct |
827 ms |
19792 KB |
Output is correct |
53 |
Correct |
839 ms |
20040 KB |
Output is correct |
54 |
Correct |
371 ms |
12564 KB |
Output is correct |
55 |
Correct |
752 ms |
20320 KB |
Output is correct |
56 |
Execution timed out |
3052 ms |
10468 KB |
Time limit exceeded |
57 |
Halted |
0 ms |
0 KB |
- |