#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;
const int H = 18;
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(H, -1);
w[i].resize(H);
}
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};
}
up[id[i][j]][0] = id[i + 1][opt[2]];
w[id[i][j]][0] = opt[0];
}
}
for ( int j = 1; j < H; 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 = H - 1; 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:47:14: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
47 | void modify(auto a_){
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
29 ms |
3232 KB |
Output is correct |
3 |
Correct |
199 ms |
18516 KB |
Output is correct |
4 |
Correct |
284 ms |
24216 KB |
Output is correct |
5 |
Correct |
275 ms |
19544 KB |
Output is correct |
6 |
Correct |
305 ms |
23792 KB |
Output is correct |
7 |
Correct |
268 ms |
23884 KB |
Output is correct |
8 |
Correct |
237 ms |
17968 KB |
Output is correct |
9 |
Correct |
312 ms |
24000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
29 ms |
3232 KB |
Output is correct |
3 |
Correct |
199 ms |
18516 KB |
Output is correct |
4 |
Correct |
284 ms |
24216 KB |
Output is correct |
5 |
Correct |
275 ms |
19544 KB |
Output is correct |
6 |
Correct |
305 ms |
23792 KB |
Output is correct |
7 |
Correct |
268 ms |
23884 KB |
Output is correct |
8 |
Correct |
237 ms |
17968 KB |
Output is correct |
9 |
Correct |
312 ms |
24000 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
201 ms |
10576 KB |
Output is correct |
12 |
Correct |
168 ms |
10580 KB |
Output is correct |
13 |
Correct |
187 ms |
10324 KB |
Output is correct |
14 |
Correct |
216 ms |
10440 KB |
Output is correct |
15 |
Correct |
228 ms |
16976 KB |
Output is correct |
16 |
Correct |
36 ms |
3328 KB |
Output is correct |
17 |
Correct |
268 ms |
22868 KB |
Output is correct |
18 |
Correct |
239 ms |
17748 KB |
Output is correct |
19 |
Correct |
290 ms |
22148 KB |
Output is correct |
20 |
Correct |
239 ms |
16468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
29 ms |
3232 KB |
Output is correct |
3 |
Correct |
199 ms |
18516 KB |
Output is correct |
4 |
Correct |
284 ms |
24216 KB |
Output is correct |
5 |
Correct |
275 ms |
19544 KB |
Output is correct |
6 |
Correct |
305 ms |
23792 KB |
Output is correct |
7 |
Correct |
268 ms |
23884 KB |
Output is correct |
8 |
Correct |
237 ms |
17968 KB |
Output is correct |
9 |
Correct |
312 ms |
24000 KB |
Output is correct |
10 |
Correct |
541 ms |
102776 KB |
Output is correct |
11 |
Correct |
854 ms |
133492 KB |
Output is correct |
12 |
Correct |
799 ms |
133484 KB |
Output is correct |
13 |
Correct |
608 ms |
129096 KB |
Output is correct |
14 |
Correct |
772 ms |
134696 KB |
Output is correct |
15 |
Correct |
826 ms |
135056 KB |
Output is correct |
16 |
Correct |
531 ms |
103964 KB |
Output is correct |
17 |
Correct |
785 ms |
134704 KB |
Output is correct |
18 |
Correct |
299 ms |
107280 KB |
Output is correct |
19 |
Correct |
254 ms |
106516 KB |
Output is correct |
20 |
Correct |
273 ms |
107284 KB |
Output is correct |
21 |
Correct |
276 ms |
107292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
29 ms |
3232 KB |
Output is correct |
3 |
Correct |
199 ms |
18516 KB |
Output is correct |
4 |
Correct |
284 ms |
24216 KB |
Output is correct |
5 |
Correct |
275 ms |
19544 KB |
Output is correct |
6 |
Correct |
305 ms |
23792 KB |
Output is correct |
7 |
Correct |
268 ms |
23884 KB |
Output is correct |
8 |
Correct |
237 ms |
17968 KB |
Output is correct |
9 |
Correct |
312 ms |
24000 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
201 ms |
10576 KB |
Output is correct |
12 |
Correct |
168 ms |
10580 KB |
Output is correct |
13 |
Correct |
187 ms |
10324 KB |
Output is correct |
14 |
Correct |
216 ms |
10440 KB |
Output is correct |
15 |
Correct |
228 ms |
16976 KB |
Output is correct |
16 |
Correct |
36 ms |
3328 KB |
Output is correct |
17 |
Correct |
268 ms |
22868 KB |
Output is correct |
18 |
Correct |
239 ms |
17748 KB |
Output is correct |
19 |
Correct |
290 ms |
22148 KB |
Output is correct |
20 |
Correct |
239 ms |
16468 KB |
Output is correct |
21 |
Correct |
541 ms |
102776 KB |
Output is correct |
22 |
Correct |
854 ms |
133492 KB |
Output is correct |
23 |
Correct |
799 ms |
133484 KB |
Output is correct |
24 |
Correct |
608 ms |
129096 KB |
Output is correct |
25 |
Correct |
772 ms |
134696 KB |
Output is correct |
26 |
Correct |
826 ms |
135056 KB |
Output is correct |
27 |
Correct |
531 ms |
103964 KB |
Output is correct |
28 |
Correct |
785 ms |
134704 KB |
Output is correct |
29 |
Correct |
299 ms |
107280 KB |
Output is correct |
30 |
Correct |
254 ms |
106516 KB |
Output is correct |
31 |
Correct |
273 ms |
107284 KB |
Output is correct |
32 |
Correct |
276 ms |
107292 KB |
Output is correct |
33 |
Correct |
962 ms |
112504 KB |
Output is correct |
34 |
Correct |
902 ms |
112248 KB |
Output is correct |
35 |
Correct |
933 ms |
104060 KB |
Output is correct |
36 |
Correct |
926 ms |
104096 KB |
Output is correct |
37 |
Correct |
655 ms |
107156 KB |
Output is correct |
38 |
Correct |
549 ms |
96388 KB |
Output is correct |
39 |
Correct |
709 ms |
120880 KB |
Output is correct |
40 |
Correct |
555 ms |
81728 KB |
Output is correct |
41 |
Correct |
624 ms |
95432 KB |
Output is correct |
42 |
Correct |
792 ms |
125240 KB |
Output is correct |
43 |
Correct |
694 ms |
110924 KB |
Output is correct |
44 |
Correct |
718 ms |
116288 KB |
Output is correct |
45 |
Correct |
300 ms |
98676 KB |
Output is correct |
46 |
Correct |
248 ms |
90512 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 |
1192 ms |
80120 KB |
Output is correct |
4 |
Correct |
1269 ms |
80140 KB |
Output is correct |
5 |
Correct |
1161 ms |
80208 KB |
Output is correct |
6 |
Correct |
1669 ms |
79956 KB |
Output is correct |
7 |
Correct |
1627 ms |
79988 KB |
Output is correct |
8 |
Correct |
410 ms |
80212 KB |
Output is correct |
9 |
Correct |
260 ms |
80368 KB |
Output is correct |
10 |
Correct |
72 ms |
78288 KB |
Output is correct |
11 |
Correct |
286 ms |
107144 KB |
Output is correct |
12 |
Correct |
289 ms |
98632 KB |
Output is correct |
13 |
Correct |
271 ms |
90508 KB |
Output is correct |
14 |
Correct |
177 ms |
86036 KB |
Output is correct |
15 |
Correct |
168 ms |
85348 KB |
Output is correct |
16 |
Correct |
193 ms |
87952 KB |
Output is correct |
17 |
Correct |
208 ms |
87520 KB |
Output is correct |
18 |
Correct |
176 ms |
81296 KB |
Output is correct |
19 |
Correct |
251 ms |
79816 KB |
Output is correct |
20 |
Correct |
154 ms |
99152 KB |
Output is correct |
21 |
Correct |
236 ms |
99148 KB |
Output is correct |
22 |
Correct |
215 ms |
89104 KB |
Output is correct |
23 |
Correct |
274 ms |
89168 KB |
Output is correct |
24 |
Correct |
172 ms |
91160 KB |
Output is correct |
25 |
Correct |
140 ms |
90028 KB |
Output is correct |
26 |
Correct |
206 ms |
90892 KB |
Output is correct |
27 |
Correct |
231 ms |
106516 KB |
Output is correct |
28 |
Correct |
263 ms |
107344 KB |
Output is correct |
29 |
Correct |
259 ms |
107316 KB |
Output is correct |
30 |
Correct |
185 ms |
84684 KB |
Output is correct |
31 |
Correct |
106 ms |
80556 KB |
Output is correct |
32 |
Correct |
320 ms |
83796 KB |
Output is correct |
33 |
Correct |
126 ms |
80200 KB |
Output is correct |
34 |
Correct |
366 ms |
81236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
29 ms |
3232 KB |
Output is correct |
3 |
Correct |
199 ms |
18516 KB |
Output is correct |
4 |
Correct |
284 ms |
24216 KB |
Output is correct |
5 |
Correct |
275 ms |
19544 KB |
Output is correct |
6 |
Correct |
305 ms |
23792 KB |
Output is correct |
7 |
Correct |
268 ms |
23884 KB |
Output is correct |
8 |
Correct |
237 ms |
17968 KB |
Output is correct |
9 |
Correct |
312 ms |
24000 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
201 ms |
10576 KB |
Output is correct |
12 |
Correct |
168 ms |
10580 KB |
Output is correct |
13 |
Correct |
187 ms |
10324 KB |
Output is correct |
14 |
Correct |
216 ms |
10440 KB |
Output is correct |
15 |
Correct |
228 ms |
16976 KB |
Output is correct |
16 |
Correct |
36 ms |
3328 KB |
Output is correct |
17 |
Correct |
268 ms |
22868 KB |
Output is correct |
18 |
Correct |
239 ms |
17748 KB |
Output is correct |
19 |
Correct |
290 ms |
22148 KB |
Output is correct |
20 |
Correct |
239 ms |
16468 KB |
Output is correct |
21 |
Correct |
541 ms |
102776 KB |
Output is correct |
22 |
Correct |
854 ms |
133492 KB |
Output is correct |
23 |
Correct |
799 ms |
133484 KB |
Output is correct |
24 |
Correct |
608 ms |
129096 KB |
Output is correct |
25 |
Correct |
772 ms |
134696 KB |
Output is correct |
26 |
Correct |
826 ms |
135056 KB |
Output is correct |
27 |
Correct |
531 ms |
103964 KB |
Output is correct |
28 |
Correct |
785 ms |
134704 KB |
Output is correct |
29 |
Correct |
299 ms |
107280 KB |
Output is correct |
30 |
Correct |
254 ms |
106516 KB |
Output is correct |
31 |
Correct |
273 ms |
107284 KB |
Output is correct |
32 |
Correct |
276 ms |
107292 KB |
Output is correct |
33 |
Correct |
962 ms |
112504 KB |
Output is correct |
34 |
Correct |
902 ms |
112248 KB |
Output is correct |
35 |
Correct |
933 ms |
104060 KB |
Output is correct |
36 |
Correct |
926 ms |
104096 KB |
Output is correct |
37 |
Correct |
655 ms |
107156 KB |
Output is correct |
38 |
Correct |
549 ms |
96388 KB |
Output is correct |
39 |
Correct |
709 ms |
120880 KB |
Output is correct |
40 |
Correct |
555 ms |
81728 KB |
Output is correct |
41 |
Correct |
624 ms |
95432 KB |
Output is correct |
42 |
Correct |
792 ms |
125240 KB |
Output is correct |
43 |
Correct |
694 ms |
110924 KB |
Output is correct |
44 |
Correct |
718 ms |
116288 KB |
Output is correct |
45 |
Correct |
300 ms |
98676 KB |
Output is correct |
46 |
Correct |
248 ms |
90512 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
1192 ms |
80120 KB |
Output is correct |
50 |
Correct |
1269 ms |
80140 KB |
Output is correct |
51 |
Correct |
1161 ms |
80208 KB |
Output is correct |
52 |
Correct |
1669 ms |
79956 KB |
Output is correct |
53 |
Correct |
1627 ms |
79988 KB |
Output is correct |
54 |
Correct |
410 ms |
80212 KB |
Output is correct |
55 |
Correct |
260 ms |
80368 KB |
Output is correct |
56 |
Correct |
72 ms |
78288 KB |
Output is correct |
57 |
Correct |
286 ms |
107144 KB |
Output is correct |
58 |
Correct |
289 ms |
98632 KB |
Output is correct |
59 |
Correct |
271 ms |
90508 KB |
Output is correct |
60 |
Correct |
177 ms |
86036 KB |
Output is correct |
61 |
Correct |
168 ms |
85348 KB |
Output is correct |
62 |
Correct |
193 ms |
87952 KB |
Output is correct |
63 |
Correct |
208 ms |
87520 KB |
Output is correct |
64 |
Correct |
176 ms |
81296 KB |
Output is correct |
65 |
Correct |
251 ms |
79816 KB |
Output is correct |
66 |
Correct |
154 ms |
99152 KB |
Output is correct |
67 |
Correct |
236 ms |
99148 KB |
Output is correct |
68 |
Correct |
215 ms |
89104 KB |
Output is correct |
69 |
Correct |
274 ms |
89168 KB |
Output is correct |
70 |
Correct |
172 ms |
91160 KB |
Output is correct |
71 |
Correct |
140 ms |
90028 KB |
Output is correct |
72 |
Correct |
206 ms |
90892 KB |
Output is correct |
73 |
Correct |
231 ms |
106516 KB |
Output is correct |
74 |
Correct |
263 ms |
107344 KB |
Output is correct |
75 |
Correct |
259 ms |
107316 KB |
Output is correct |
76 |
Correct |
185 ms |
84684 KB |
Output is correct |
77 |
Correct |
106 ms |
80556 KB |
Output is correct |
78 |
Correct |
320 ms |
83796 KB |
Output is correct |
79 |
Correct |
126 ms |
80200 KB |
Output is correct |
80 |
Correct |
366 ms |
81236 KB |
Output is correct |
81 |
Execution timed out |
3035 ms |
95852 KB |
Time limit exceeded |
82 |
Halted |
0 ms |
0 KB |
- |