#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const int inf = 1e15;
int T;
struct func{
// messy struct
vector <int> m, b;
vector <vector<ar<int,2>>> a;
vector <vector<int>> id, up, w;
vector <ar<int,2>> rev;
int n;
void modify(auto a_){
a = a_, n = a.size();
m.resize(n); id.resize(n);
int ct = 0;
for ( int i = 0; i + 1 < n; i++ ){
m[i] = a[i].size();
id[i].resize(m[i]);
for ( auto &u: id[i] ) u = ct++;
}
rev.resize(ct);
b.resize(ct);
for ( int i = 0; i + 1 < n; i++ ){
for ( int j = 0; j < m[i]; j++ ){
rev[id[i][j]] = a[i][j];
b[id[i][j]] = i;
}
}
up.resize(ct);
w.resize(ct);
for ( int i = 0; i < ct; i++ ){
up[i].resize(20, -1);
w[i].resize(20);
}
for ( int i = 0; i + 2 < n; i++ ){
for ( int j = 0; j < m[i]; j++ ){
auto [l, r] = a[i][j];
ar <int,3> opt = {inf, -1, -1};
for ( int k = 0; k < m[i + 1]; k++ ){
auto [x, y] = a[i + 1][k];
ar <int,3> nxt;
if ( x >= r ){
nxt = {0, y, k};
} else{
nxt = {1, y, k};
}
chmin(opt, nxt);
}
up[id[i][j]][0] = id[i + 1][opt[2]];
w[id[i][j]][0] = opt[0];
}
}
for ( int j = 1; j < 20; j++ ){
for ( int i = 0; i < ct; i++ ){
if ( up[i][j - 1] == -1 ){
up[i][j] = up[i][j - 1];
w[i][j] = w[i][j - 1];
} else{
up[i][j] = up[up[i][j - 1]][j - 1];
w[i][j] = w[i][j - 1] + w[up[i][j - 1]][j - 1];
}
}
}
}
int get(int u, int r){
int cnt = 0;
for ( int i = 19; i >= 0; i-- ){
if ( up[u][i] == -1 ){
continue;
}
if ( b[up[u][i]] < r ){
cnt += w[u][i];
u = up[u][i];
}
}
cnt = cnt * T + rev[u][1];
return cnt;
}
int qry(int l, int r){
int opt = inf;
for ( int j = 0; j < m[l]; j++ ){
auto [x, y] = a[l][j];
chmin(opt, get(id[l][j], r) - x);
}
return opt;
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n; cin >> n >> T;
vector <vector<ar<int,2>>> a(n), b(n);
for ( int i = 0; i + 1 < n; i++ ){
int m; cin >> m;
a[i].resize(m);
for ( auto &[l, r]: a[i] ){
cin >> l >> r;
}
}
func lf; lf.modify(a);
// reversing a
for ( int i = 0; i + 1 < n; i++ ){
int j = n - i - 2, s = a[j].size();
b[i].resize(s);
for ( int k = 0; k < s; k++ ){
int x = a[j][k][0], y = a[j][k][1];
x = T - x - 1, y = T - y - 1;
b[i][k] = {y, x};
}
}
func rg; rg.modify(b);
int q; cin >> q;
while ( q-- ){
int l, r; cin >> l >> r;
--l, --r;
if ( a[l].size() < a[r - 1].size() ){
cout << lf.qry(l, r) << ln;
} else{
cout << rg.qry(n - r - 1, n - l - 1) << ln;
}
}
cout << '\n';
}
Compilation message
Main.cpp:45:14: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
45 | void modify(auto a_){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
33 ms |
3320 KB |
Output is correct |
3 |
Correct |
72 ms |
5088 KB |
Output is correct |
4 |
Correct |
83 ms |
6740 KB |
Output is correct |
5 |
Correct |
72 ms |
5808 KB |
Output is correct |
6 |
Correct |
90 ms |
6736 KB |
Output is correct |
7 |
Correct |
98 ms |
6740 KB |
Output is correct |
8 |
Correct |
63 ms |
4944 KB |
Output is correct |
9 |
Correct |
91 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
33 ms |
3320 KB |
Output is correct |
3 |
Correct |
72 ms |
5088 KB |
Output is correct |
4 |
Correct |
83 ms |
6740 KB |
Output is correct |
5 |
Correct |
72 ms |
5808 KB |
Output is correct |
6 |
Correct |
90 ms |
6736 KB |
Output is correct |
7 |
Correct |
98 ms |
6740 KB |
Output is correct |
8 |
Correct |
63 ms |
4944 KB |
Output is correct |
9 |
Correct |
91 ms |
6736 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
123 ms |
5728 KB |
Output is correct |
12 |
Correct |
125 ms |
5712 KB |
Output is correct |
13 |
Correct |
128 ms |
5460 KB |
Output is correct |
14 |
Correct |
129 ms |
5456 KB |
Output is correct |
15 |
Correct |
94 ms |
5968 KB |
Output is correct |
16 |
Correct |
48 ms |
3412 KB |
Output is correct |
17 |
Correct |
87 ms |
6484 KB |
Output is correct |
18 |
Correct |
96 ms |
6240 KB |
Output is correct |
19 |
Correct |
105 ms |
6480 KB |
Output is correct |
20 |
Correct |
104 ms |
6224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
33 ms |
3320 KB |
Output is correct |
3 |
Correct |
72 ms |
5088 KB |
Output is correct |
4 |
Correct |
83 ms |
6740 KB |
Output is correct |
5 |
Correct |
72 ms |
5808 KB |
Output is correct |
6 |
Correct |
90 ms |
6736 KB |
Output is correct |
7 |
Correct |
98 ms |
6740 KB |
Output is correct |
8 |
Correct |
63 ms |
4944 KB |
Output is correct |
9 |
Correct |
91 ms |
6736 KB |
Output is correct |
10 |
Correct |
301 ms |
100428 KB |
Output is correct |
11 |
Correct |
470 ms |
127780 KB |
Output is correct |
12 |
Correct |
478 ms |
127712 KB |
Output is correct |
13 |
Correct |
383 ms |
126508 KB |
Output is correct |
14 |
Correct |
450 ms |
127620 KB |
Output is correct |
15 |
Correct |
481 ms |
127624 KB |
Output is correct |
16 |
Correct |
298 ms |
100376 KB |
Output is correct |
17 |
Correct |
476 ms |
127784 KB |
Output is correct |
18 |
Correct |
201 ms |
112292 KB |
Output is correct |
19 |
Correct |
185 ms |
112148 KB |
Output is correct |
20 |
Correct |
198 ms |
112404 KB |
Output is correct |
21 |
Correct |
197 ms |
112400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
33 ms |
3320 KB |
Output is correct |
3 |
Correct |
72 ms |
5088 KB |
Output is correct |
4 |
Correct |
83 ms |
6740 KB |
Output is correct |
5 |
Correct |
72 ms |
5808 KB |
Output is correct |
6 |
Correct |
90 ms |
6736 KB |
Output is correct |
7 |
Correct |
98 ms |
6740 KB |
Output is correct |
8 |
Correct |
63 ms |
4944 KB |
Output is correct |
9 |
Correct |
91 ms |
6736 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
123 ms |
5728 KB |
Output is correct |
12 |
Correct |
125 ms |
5712 KB |
Output is correct |
13 |
Correct |
128 ms |
5460 KB |
Output is correct |
14 |
Correct |
129 ms |
5456 KB |
Output is correct |
15 |
Correct |
94 ms |
5968 KB |
Output is correct |
16 |
Correct |
48 ms |
3412 KB |
Output is correct |
17 |
Correct |
87 ms |
6484 KB |
Output is correct |
18 |
Correct |
96 ms |
6240 KB |
Output is correct |
19 |
Correct |
105 ms |
6480 KB |
Output is correct |
20 |
Correct |
104 ms |
6224 KB |
Output is correct |
21 |
Correct |
301 ms |
100428 KB |
Output is correct |
22 |
Correct |
470 ms |
127780 KB |
Output is correct |
23 |
Correct |
478 ms |
127712 KB |
Output is correct |
24 |
Correct |
383 ms |
126508 KB |
Output is correct |
25 |
Correct |
450 ms |
127620 KB |
Output is correct |
26 |
Correct |
481 ms |
127624 KB |
Output is correct |
27 |
Correct |
298 ms |
100376 KB |
Output is correct |
28 |
Correct |
476 ms |
127784 KB |
Output is correct |
29 |
Correct |
201 ms |
112292 KB |
Output is correct |
30 |
Correct |
185 ms |
112148 KB |
Output is correct |
31 |
Correct |
198 ms |
112404 KB |
Output is correct |
32 |
Correct |
197 ms |
112400 KB |
Output is correct |
33 |
Correct |
566 ms |
102280 KB |
Output is correct |
34 |
Correct |
568 ms |
102264 KB |
Output is correct |
35 |
Correct |
633 ms |
93052 KB |
Output is correct |
36 |
Correct |
614 ms |
93048 KB |
Output is correct |
37 |
Correct |
418 ms |
96384 KB |
Output is correct |
38 |
Correct |
387 ms |
91780 KB |
Output is correct |
39 |
Correct |
480 ms |
117048 KB |
Output is correct |
40 |
Correct |
404 ms |
73024 KB |
Output is correct |
41 |
Correct |
383 ms |
87380 KB |
Output is correct |
42 |
Correct |
528 ms |
116792 KB |
Output is correct |
43 |
Correct |
420 ms |
104408 KB |
Output is correct |
44 |
Correct |
495 ms |
106560 KB |
Output is correct |
45 |
Correct |
251 ms |
102608 KB |
Output is correct |
46 |
Correct |
211 ms |
93468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2939 ms |
84416 KB |
Output is correct |
4 |
Correct |
2899 ms |
84504 KB |
Output is correct |
5 |
Correct |
2346 ms |
84308 KB |
Output is correct |
6 |
Execution timed out |
3062 ms |
86612 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
33 ms |
3320 KB |
Output is correct |
3 |
Correct |
72 ms |
5088 KB |
Output is correct |
4 |
Correct |
83 ms |
6740 KB |
Output is correct |
5 |
Correct |
72 ms |
5808 KB |
Output is correct |
6 |
Correct |
90 ms |
6736 KB |
Output is correct |
7 |
Correct |
98 ms |
6740 KB |
Output is correct |
8 |
Correct |
63 ms |
4944 KB |
Output is correct |
9 |
Correct |
91 ms |
6736 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
123 ms |
5728 KB |
Output is correct |
12 |
Correct |
125 ms |
5712 KB |
Output is correct |
13 |
Correct |
128 ms |
5460 KB |
Output is correct |
14 |
Correct |
129 ms |
5456 KB |
Output is correct |
15 |
Correct |
94 ms |
5968 KB |
Output is correct |
16 |
Correct |
48 ms |
3412 KB |
Output is correct |
17 |
Correct |
87 ms |
6484 KB |
Output is correct |
18 |
Correct |
96 ms |
6240 KB |
Output is correct |
19 |
Correct |
105 ms |
6480 KB |
Output is correct |
20 |
Correct |
104 ms |
6224 KB |
Output is correct |
21 |
Correct |
301 ms |
100428 KB |
Output is correct |
22 |
Correct |
470 ms |
127780 KB |
Output is correct |
23 |
Correct |
478 ms |
127712 KB |
Output is correct |
24 |
Correct |
383 ms |
126508 KB |
Output is correct |
25 |
Correct |
450 ms |
127620 KB |
Output is correct |
26 |
Correct |
481 ms |
127624 KB |
Output is correct |
27 |
Correct |
298 ms |
100376 KB |
Output is correct |
28 |
Correct |
476 ms |
127784 KB |
Output is correct |
29 |
Correct |
201 ms |
112292 KB |
Output is correct |
30 |
Correct |
185 ms |
112148 KB |
Output is correct |
31 |
Correct |
198 ms |
112404 KB |
Output is correct |
32 |
Correct |
197 ms |
112400 KB |
Output is correct |
33 |
Correct |
566 ms |
102280 KB |
Output is correct |
34 |
Correct |
568 ms |
102264 KB |
Output is correct |
35 |
Correct |
633 ms |
93052 KB |
Output is correct |
36 |
Correct |
614 ms |
93048 KB |
Output is correct |
37 |
Correct |
418 ms |
96384 KB |
Output is correct |
38 |
Correct |
387 ms |
91780 KB |
Output is correct |
39 |
Correct |
480 ms |
117048 KB |
Output is correct |
40 |
Correct |
404 ms |
73024 KB |
Output is correct |
41 |
Correct |
383 ms |
87380 KB |
Output is correct |
42 |
Correct |
528 ms |
116792 KB |
Output is correct |
43 |
Correct |
420 ms |
104408 KB |
Output is correct |
44 |
Correct |
495 ms |
106560 KB |
Output is correct |
45 |
Correct |
251 ms |
102608 KB |
Output is correct |
46 |
Correct |
211 ms |
93468 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
2939 ms |
84416 KB |
Output is correct |
50 |
Correct |
2899 ms |
84504 KB |
Output is correct |
51 |
Correct |
2346 ms |
84308 KB |
Output is correct |
52 |
Execution timed out |
3062 ms |
86612 KB |
Time limit exceeded |
53 |
Halted |
0 ms |
0 KB |
- |