#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++ ){
vector <ar<int,3>> q;
for ( int k = 0; k < m[i + 1]; k++ ){
auto [x, y] = a[i + 1][k];
q.pb({-x, y, k});
}
sort(all(q));
set <pair<int,int>> st;
int mn = inf;
for ( auto [x, y, k]: q ){
if ( y >= mn ) continue;
st.insert({-x, k});
mn = y;
}
for ( int j = 0; j < m[i]; j++ ){
auto [l, r] = a[i][j];
ar <int,3> opt;
auto it = st.lower_bound({r, -1});
if ( it != st.end() ){
opt = {0, a[i + 1][it -> second][1], it -> second};
} else{
int k = st.begin() -> second;
opt = {1, a[i + 1][k][1], k};
}
//~ 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);
map <pair<int,int>,int> memo;
auto qry = [&](int l, int r){
if ( !memo.count({l, r}) ){
if ( a[l].size() < a[r - 1].size() ){
memo[{l, r}] = lf.qry(l, r);
} else{
memo[{l, r}] = rg.qry(n - r - 1, n - l - 1);
}
}
return memo[{l, r}];
};
int q; cin >> q;
while ( q-- ){
int l, r; cin >> l >> r;
--l, --r;
cout << qry(l, r) << 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_){
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3392 KB |
Output is correct |
3 |
Correct |
171 ms |
18356 KB |
Output is correct |
4 |
Correct |
258 ms |
24400 KB |
Output is correct |
5 |
Correct |
188 ms |
19652 KB |
Output is correct |
6 |
Correct |
250 ms |
23992 KB |
Output is correct |
7 |
Correct |
259 ms |
24144 KB |
Output is correct |
8 |
Correct |
183 ms |
18004 KB |
Output is correct |
9 |
Correct |
252 ms |
23908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3392 KB |
Output is correct |
3 |
Correct |
171 ms |
18356 KB |
Output is correct |
4 |
Correct |
258 ms |
24400 KB |
Output is correct |
5 |
Correct |
188 ms |
19652 KB |
Output is correct |
6 |
Correct |
250 ms |
23992 KB |
Output is correct |
7 |
Correct |
259 ms |
24144 KB |
Output is correct |
8 |
Correct |
183 ms |
18004 KB |
Output is correct |
9 |
Correct |
252 ms |
23908 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
152 ms |
10832 KB |
Output is correct |
12 |
Correct |
159 ms |
10944 KB |
Output is correct |
13 |
Correct |
151 ms |
10576 KB |
Output is correct |
14 |
Correct |
155 ms |
10580 KB |
Output is correct |
15 |
Correct |
196 ms |
17232 KB |
Output is correct |
16 |
Correct |
29 ms |
3412 KB |
Output is correct |
17 |
Correct |
293 ms |
22868 KB |
Output is correct |
18 |
Correct |
199 ms |
18260 KB |
Output is correct |
19 |
Correct |
246 ms |
22352 KB |
Output is correct |
20 |
Correct |
218 ms |
16656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3392 KB |
Output is correct |
3 |
Correct |
171 ms |
18356 KB |
Output is correct |
4 |
Correct |
258 ms |
24400 KB |
Output is correct |
5 |
Correct |
188 ms |
19652 KB |
Output is correct |
6 |
Correct |
250 ms |
23992 KB |
Output is correct |
7 |
Correct |
259 ms |
24144 KB |
Output is correct |
8 |
Correct |
183 ms |
18004 KB |
Output is correct |
9 |
Correct |
252 ms |
23908 KB |
Output is correct |
10 |
Correct |
443 ms |
107792 KB |
Output is correct |
11 |
Correct |
658 ms |
139624 KB |
Output is correct |
12 |
Correct |
679 ms |
139736 KB |
Output is correct |
13 |
Correct |
555 ms |
135184 KB |
Output is correct |
14 |
Correct |
687 ms |
141096 KB |
Output is correct |
15 |
Correct |
735 ms |
141072 KB |
Output is correct |
16 |
Correct |
459 ms |
109044 KB |
Output is correct |
17 |
Correct |
702 ms |
141096 KB |
Output is correct |
18 |
Correct |
252 ms |
112916 KB |
Output is correct |
19 |
Correct |
220 ms |
111996 KB |
Output is correct |
20 |
Correct |
250 ms |
112932 KB |
Output is correct |
21 |
Correct |
243 ms |
112912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3392 KB |
Output is correct |
3 |
Correct |
171 ms |
18356 KB |
Output is correct |
4 |
Correct |
258 ms |
24400 KB |
Output is correct |
5 |
Correct |
188 ms |
19652 KB |
Output is correct |
6 |
Correct |
250 ms |
23992 KB |
Output is correct |
7 |
Correct |
259 ms |
24144 KB |
Output is correct |
8 |
Correct |
183 ms |
18004 KB |
Output is correct |
9 |
Correct |
252 ms |
23908 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
152 ms |
10832 KB |
Output is correct |
12 |
Correct |
159 ms |
10944 KB |
Output is correct |
13 |
Correct |
151 ms |
10576 KB |
Output is correct |
14 |
Correct |
155 ms |
10580 KB |
Output is correct |
15 |
Correct |
196 ms |
17232 KB |
Output is correct |
16 |
Correct |
29 ms |
3412 KB |
Output is correct |
17 |
Correct |
293 ms |
22868 KB |
Output is correct |
18 |
Correct |
199 ms |
18260 KB |
Output is correct |
19 |
Correct |
246 ms |
22352 KB |
Output is correct |
20 |
Correct |
218 ms |
16656 KB |
Output is correct |
21 |
Correct |
443 ms |
107792 KB |
Output is correct |
22 |
Correct |
658 ms |
139624 KB |
Output is correct |
23 |
Correct |
679 ms |
139736 KB |
Output is correct |
24 |
Correct |
555 ms |
135184 KB |
Output is correct |
25 |
Correct |
687 ms |
141096 KB |
Output is correct |
26 |
Correct |
735 ms |
141072 KB |
Output is correct |
27 |
Correct |
459 ms |
109044 KB |
Output is correct |
28 |
Correct |
702 ms |
141096 KB |
Output is correct |
29 |
Correct |
252 ms |
112916 KB |
Output is correct |
30 |
Correct |
220 ms |
111996 KB |
Output is correct |
31 |
Correct |
250 ms |
112932 KB |
Output is correct |
32 |
Correct |
243 ms |
112912 KB |
Output is correct |
33 |
Correct |
872 ms |
118748 KB |
Output is correct |
34 |
Correct |
831 ms |
118656 KB |
Output is correct |
35 |
Correct |
940 ms |
109596 KB |
Output is correct |
36 |
Correct |
872 ms |
109812 KB |
Output is correct |
37 |
Correct |
609 ms |
112684 KB |
Output is correct |
38 |
Correct |
517 ms |
101252 KB |
Output is correct |
39 |
Correct |
644 ms |
127152 KB |
Output is correct |
40 |
Correct |
531 ms |
85960 KB |
Output is correct |
41 |
Correct |
661 ms |
100436 KB |
Output is correct |
42 |
Correct |
830 ms |
131392 KB |
Output is correct |
43 |
Correct |
753 ms |
117072 KB |
Output is correct |
44 |
Correct |
882 ms |
122416 KB |
Output is correct |
45 |
Correct |
368 ms |
104400 KB |
Output is correct |
46 |
Correct |
321 ms |
95924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1489 ms |
85808 KB |
Output is correct |
4 |
Correct |
1492 ms |
85876 KB |
Output is correct |
5 |
Correct |
1455 ms |
85904 KB |
Output is correct |
6 |
Correct |
1745 ms |
85692 KB |
Output is correct |
7 |
Correct |
1796 ms |
88064 KB |
Output is correct |
8 |
Correct |
443 ms |
88256 KB |
Output is correct |
9 |
Correct |
285 ms |
88404 KB |
Output is correct |
10 |
Correct |
81 ms |
86228 KB |
Output is correct |
11 |
Correct |
262 ms |
115732 KB |
Output is correct |
12 |
Correct |
328 ms |
107292 KB |
Output is correct |
13 |
Correct |
312 ms |
98704 KB |
Output is correct |
14 |
Correct |
192 ms |
94532 KB |
Output is correct |
15 |
Correct |
184 ms |
93552 KB |
Output is correct |
16 |
Correct |
238 ms |
96400 KB |
Output is correct |
17 |
Correct |
222 ms |
95896 KB |
Output is correct |
18 |
Correct |
192 ms |
89532 KB |
Output is correct |
19 |
Correct |
292 ms |
87936 KB |
Output is correct |
20 |
Correct |
171 ms |
107512 KB |
Output is correct |
21 |
Correct |
227 ms |
107512 KB |
Output is correct |
22 |
Correct |
266 ms |
97224 KB |
Output is correct |
23 |
Correct |
332 ms |
97356 KB |
Output is correct |
24 |
Correct |
195 ms |
99228 KB |
Output is correct |
25 |
Correct |
160 ms |
98380 KB |
Output is correct |
26 |
Correct |
266 ms |
99168 KB |
Output is correct |
27 |
Correct |
276 ms |
114816 KB |
Output is correct |
28 |
Correct |
314 ms |
115896 KB |
Output is correct |
29 |
Correct |
296 ms |
116064 KB |
Output is correct |
30 |
Correct |
223 ms |
92804 KB |
Output is correct |
31 |
Correct |
133 ms |
88184 KB |
Output is correct |
32 |
Correct |
325 ms |
91988 KB |
Output is correct |
33 |
Correct |
138 ms |
88192 KB |
Output is correct |
34 |
Correct |
404 ms |
89248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
34 ms |
3392 KB |
Output is correct |
3 |
Correct |
171 ms |
18356 KB |
Output is correct |
4 |
Correct |
258 ms |
24400 KB |
Output is correct |
5 |
Correct |
188 ms |
19652 KB |
Output is correct |
6 |
Correct |
250 ms |
23992 KB |
Output is correct |
7 |
Correct |
259 ms |
24144 KB |
Output is correct |
8 |
Correct |
183 ms |
18004 KB |
Output is correct |
9 |
Correct |
252 ms |
23908 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
152 ms |
10832 KB |
Output is correct |
12 |
Correct |
159 ms |
10944 KB |
Output is correct |
13 |
Correct |
151 ms |
10576 KB |
Output is correct |
14 |
Correct |
155 ms |
10580 KB |
Output is correct |
15 |
Correct |
196 ms |
17232 KB |
Output is correct |
16 |
Correct |
29 ms |
3412 KB |
Output is correct |
17 |
Correct |
293 ms |
22868 KB |
Output is correct |
18 |
Correct |
199 ms |
18260 KB |
Output is correct |
19 |
Correct |
246 ms |
22352 KB |
Output is correct |
20 |
Correct |
218 ms |
16656 KB |
Output is correct |
21 |
Correct |
443 ms |
107792 KB |
Output is correct |
22 |
Correct |
658 ms |
139624 KB |
Output is correct |
23 |
Correct |
679 ms |
139736 KB |
Output is correct |
24 |
Correct |
555 ms |
135184 KB |
Output is correct |
25 |
Correct |
687 ms |
141096 KB |
Output is correct |
26 |
Correct |
735 ms |
141072 KB |
Output is correct |
27 |
Correct |
459 ms |
109044 KB |
Output is correct |
28 |
Correct |
702 ms |
141096 KB |
Output is correct |
29 |
Correct |
252 ms |
112916 KB |
Output is correct |
30 |
Correct |
220 ms |
111996 KB |
Output is correct |
31 |
Correct |
250 ms |
112932 KB |
Output is correct |
32 |
Correct |
243 ms |
112912 KB |
Output is correct |
33 |
Correct |
872 ms |
118748 KB |
Output is correct |
34 |
Correct |
831 ms |
118656 KB |
Output is correct |
35 |
Correct |
940 ms |
109596 KB |
Output is correct |
36 |
Correct |
872 ms |
109812 KB |
Output is correct |
37 |
Correct |
609 ms |
112684 KB |
Output is correct |
38 |
Correct |
517 ms |
101252 KB |
Output is correct |
39 |
Correct |
644 ms |
127152 KB |
Output is correct |
40 |
Correct |
531 ms |
85960 KB |
Output is correct |
41 |
Correct |
661 ms |
100436 KB |
Output is correct |
42 |
Correct |
830 ms |
131392 KB |
Output is correct |
43 |
Correct |
753 ms |
117072 KB |
Output is correct |
44 |
Correct |
882 ms |
122416 KB |
Output is correct |
45 |
Correct |
368 ms |
104400 KB |
Output is correct |
46 |
Correct |
321 ms |
95924 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1489 ms |
85808 KB |
Output is correct |
50 |
Correct |
1492 ms |
85876 KB |
Output is correct |
51 |
Correct |
1455 ms |
85904 KB |
Output is correct |
52 |
Correct |
1745 ms |
85692 KB |
Output is correct |
53 |
Correct |
1796 ms |
88064 KB |
Output is correct |
54 |
Correct |
443 ms |
88256 KB |
Output is correct |
55 |
Correct |
285 ms |
88404 KB |
Output is correct |
56 |
Correct |
81 ms |
86228 KB |
Output is correct |
57 |
Correct |
262 ms |
115732 KB |
Output is correct |
58 |
Correct |
328 ms |
107292 KB |
Output is correct |
59 |
Correct |
312 ms |
98704 KB |
Output is correct |
60 |
Correct |
192 ms |
94532 KB |
Output is correct |
61 |
Correct |
184 ms |
93552 KB |
Output is correct |
62 |
Correct |
238 ms |
96400 KB |
Output is correct |
63 |
Correct |
222 ms |
95896 KB |
Output is correct |
64 |
Correct |
192 ms |
89532 KB |
Output is correct |
65 |
Correct |
292 ms |
87936 KB |
Output is correct |
66 |
Correct |
171 ms |
107512 KB |
Output is correct |
67 |
Correct |
227 ms |
107512 KB |
Output is correct |
68 |
Correct |
266 ms |
97224 KB |
Output is correct |
69 |
Correct |
332 ms |
97356 KB |
Output is correct |
70 |
Correct |
195 ms |
99228 KB |
Output is correct |
71 |
Correct |
160 ms |
98380 KB |
Output is correct |
72 |
Correct |
266 ms |
99168 KB |
Output is correct |
73 |
Correct |
276 ms |
114816 KB |
Output is correct |
74 |
Correct |
314 ms |
115896 KB |
Output is correct |
75 |
Correct |
296 ms |
116064 KB |
Output is correct |
76 |
Correct |
223 ms |
92804 KB |
Output is correct |
77 |
Correct |
133 ms |
88184 KB |
Output is correct |
78 |
Correct |
325 ms |
91988 KB |
Output is correct |
79 |
Correct |
138 ms |
88192 KB |
Output is correct |
80 |
Correct |
404 ms |
89248 KB |
Output is correct |
81 |
Execution timed out |
3047 ms |
101460 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |