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;
#define int long long
const int N = 5e5 + 10;
int n;
int a[N], b[N];
int st[4 * N];
int lzy[4 * N];
void propagate(int index, int l, int mid, int r) {
if(2 * index + 1 < 4 * N) {
st[2 * index] += lzy[index];
lzy[2 * index] += lzy[index];
st[2 * index + 1] += lzy[index];
lzy[2 * index + 1] += lzy[index];
lzy[index] = 0;
}
}
void update(int index, int l, int r, int L, int R, int val) {
if(l > r || r < L || R < l) return;
if(L <= l && r <= R) {
st[index] += val;
lzy[index] += val;
return;
}
int mid = (l + r) >> 1;
propagate(index, l, mid, r);
update(2 * index, l, mid, L, R, val);
update(2 * index + 1, mid + 1, r, L, R, val);
st[index] = max(st[2 * index], st[2 * index + 1]);
}
int get(int index, int l, int r, int L, int R) {
if(l > r || r < L || R < l) return 0;
if(L <= l && r <= R) return st[index];
int mid = (l + r) >> 1;
propagate(index, l, mid, r);
return max(get(2 * index, l, mid, L, R), get(2 * index + 1, mid + 1, r, L, R));
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i] >> b[i];
pair<int, int> p[n + 1];
for(int i = 1; i <= n; i++) {
p[i].first = a[i];
p[i].second = b[i];
}
sort(p + 1, p + n + 1);
for(int i = 1; i <= n; i++) {
a[i] = p[i].first;
b[i] = p[i].second;
}
int res = 0;
for(int i = 1; i <= n; i++) {
update(1, 1, n, i, i, a[i]);
update(1, 1, n, 1, i, b[i]);
res = max(res, get(1, 1, n, 1, i) - a[i]);
}
cout << res;
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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |