This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |