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;
const int N = 2e5;
int ans[N + 2];
vector<pair<int, int>>a, b;
struct Segtree{
int tree[4*N + 2];
void update(int l, int r, int i, int pos, int val){
if(l == r){
tree[i] = val;
return;
}
int mid = (l + r)/2;
if(pos <= mid) update(l, mid, 2*i, pos, val);
else update(mid + 1, r, 2*i + 1, pos, val);
tree[i] = max(tree[2*i], tree[2*i + 1]);
}
int query(int l, int r, int i, int left, int right){
if(l > right || r < left || l > r) return 0;
if(left <= l && r <= right) return tree[i];
int mid = (l + r)/2;
return max(
query(l, mid, 2*i, left, right),
query(mid + 1, r, 2*i + 1, left, right)
);
}
} l, r;
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int n; cin >> n;
a.resize(n + 2); b.resize(n + 1);
for(int i = 1; i <= n + 1; i++){
cin >> a[i].first;
a[i].second = i;
}
for(int i = 1; i <= n; i++){
cin >> b[i].first;
b[i].second = i;
}
sort(a.begin() + 1, a.end());
sort(b.begin() + 1, b.end());
for(int i = 1; i <= n; i++){
l.update(1, n, 1, i, max(a[i].first - b[i].first, 0));
}
for(int i = 1; i <= n; i++){
r.update(1, n, 1, i, max(a[i + 1].first - b[i].first, 0));
}
for(int i = 1; i <= n + 1; i++){
ans[a[i].second] = max(l.query(1, n, 1, 1, i - 1), r.query(1, n, 1, i, n));
}
for(int i = 1; i <= n + 1; i++) cout << ans[i] << " ";
cout << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |