#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 = 0; i <= n; i++){
int elem = a[i].f, ind = a[i].s;
if(ind == n){
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(ind == 0){
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 = ind, l2 = ind+1, 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(auto i : ans)
cout << 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();
}
}
Compilation message
ho_t1.cpp: In function 'void solve()':
ho_t1.cpp:45:13: warning: unused variable 'elem' [-Wunused-variable]
45 | int elem = a[i].f, ind = a[i].s;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |