#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define pb push_back
#define mp make_pair
#define f first
#define s second
void solve(){
int n;
cin >> n;
vector<pi> a(n+1);
vi b(n);
for(int i = 0; i <= n; i++){
cin >> a[i].f;
a[i].s = i;
}
for(int i = 0; i < n; i++)
cin >> b[i];
sort(a.begin(), a.end());
sort(b.begin(), b.end());
vi diff_same(n), diff_next(n);
for(int i = 0; i < n; i++){
diff_same[i] = max(a[i].f - b[i], 0);
diff_next[i] = max(a[i+1].f - b[i], 0);
}
vector<vi> sparse_same(n+1, vi(26)), sparse_next(n+1, vi(26));
for(int i = 1; i <= n; i++){
sparse_same[i][0] = diff_same[i-1];
sparse_next[i][0] = diff_next[i-1];
}
for(int j = 1; j < 26; j++){
for(int i = 1; i + (1 << j) - 1 <= n; i++){
sparse_same[i][j] = max(sparse_same[i][j-1], sparse_same[i + (1 << (j-1))][j-1]);
sparse_next[i][j] = max(sparse_next[i][j-1], sparse_next[i + (1 << (j-1))][j-1]);
}
}
vi ans(n+1);
for(int i = 1; i <= n+1; i++){
int ind = a[i-1].s;
if(i == n+1){
int l = 1, r = n;
int w = (int)log2(r - l + 1);
ans[ind] = max(sparse_same[l][w], sparse_same[r - (1 << w) + 1][w]);
} else if(i == 1){
int l = 1, r = n;
int w = (int)log2(r - l + 1);
ans[ind] = max(sparse_next[l][w], sparse_next[r - (1 << w) + 1][w]);
} else {
int l1 = 1, r1 = i-1, l2 = i, r2 = n;
int w1 = (int)log2(r1 - l1 + 1), w2 = (int)log2(r2 - l2 + 1);
int max1 = max(sparse_same[l1][w1], sparse_same[r1 - (1 << w1) + 1][w1]);
int max2 = max(sparse_next[l2][w2], sparse_next[r2 - (1 << w2) + 1][w2]);
ans[ind] = max(max1, max2);
}
}
for(int i = 0; i <= n; i++)
cout << ans[i] << ' ';
cout << '\n';
}
int main(){
//freopen("input.txt", "r", stdin);
//freopen("test.out", "w", stdout);
//ios::sync_with_stdio(0); cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
3 ms |
824 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
820 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
3 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
292 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
296 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
2 ms |
468 KB |
Output is correct |
15 |
Correct |
2 ms |
720 KB |
Output is correct |
16 |
Correct |
1 ms |
296 KB |
Output is correct |
17 |
Correct |
3 ms |
824 KB |
Output is correct |
18 |
Correct |
3 ms |
724 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
3 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
724 KB |
Output is correct |
25 |
Correct |
3 ms |
852 KB |
Output is correct |
26 |
Correct |
5 ms |
852 KB |
Output is correct |
27 |
Correct |
3 ms |
820 KB |
Output is correct |
28 |
Correct |
3 ms |
852 KB |
Output is correct |
29 |
Correct |
3 ms |
724 KB |
Output is correct |
30 |
Correct |
3 ms |
852 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
3 ms |
852 KB |
Output is correct |
33 |
Correct |
291 ms |
60620 KB |
Output is correct |
34 |
Correct |
329 ms |
63148 KB |
Output is correct |
35 |
Correct |
291 ms |
61020 KB |
Output is correct |
36 |
Correct |
309 ms |
61948 KB |
Output is correct |
37 |
Correct |
331 ms |
63928 KB |
Output is correct |
38 |
Correct |
298 ms |
63100 KB |
Output is correct |
39 |
Correct |
319 ms |
61408 KB |
Output is correct |
40 |
Correct |
309 ms |
61192 KB |
Output is correct |
41 |
Correct |
327 ms |
61952 KB |
Output is correct |
42 |
Correct |
295 ms |
61704 KB |
Output is correct |
43 |
Correct |
298 ms |
61296 KB |
Output is correct |
44 |
Correct |
305 ms |
59912 KB |
Output is correct |
45 |
Correct |
293 ms |
60520 KB |
Output is correct |
46 |
Correct |
296 ms |
59968 KB |
Output is correct |
47 |
Correct |
310 ms |
62792 KB |
Output is correct |
48 |
Correct |
297 ms |
62656 KB |
Output is correct |
49 |
Correct |
307 ms |
63184 KB |
Output is correct |
50 |
Correct |
305 ms |
63436 KB |
Output is correct |
51 |
Correct |
349 ms |
63208 KB |
Output is correct |
52 |
Correct |
355 ms |
63308 KB |
Output is correct |
53 |
Correct |
307 ms |
63180 KB |
Output is correct |