#include <bits/stdc++.h>
using namespace std;
template <class T>
struct Fenwick {
int n, log;
vector<T> bit;
Fenwick(int n) : n(n), log(32 - __builtin_clz(n + 1)), bit(n + 1, 0) {}
void add(int i, T delta) {
for (i++; i <= n; i += i & -i) {
bit[i] += delta;
}
}
T sum(int i) {
T res = 0;
for (; i > 0; i -= i & -i) {
res += bit[i];
}
return res;
}
int kth(T k) {
T sum = 0;
int pos = 0;
for (int l = log - 1; l >= 0; l--) {
if (pos + (1 << l) <= n && sum + bit[pos + (1 << l)] <= k) {
pos += 1 << l;
sum += bit[pos];
}
}
return pos;
}
};
template<class Info, class Tag>
struct LazySegmentTree {
int n;
vector<Info> info;
vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(vector<Info>(n_, v_));
}
template<class T>
void init(vector<T> init_) {
n = init_.size();
info.assign(4 << __lg(n), Info());
tag.assign(4 << __lg(n), Tag());
function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
struct Tag {
long long add = 0;
void apply(const Tag &t) & {
add += t.add;
}
};
struct Info { // a+bx
long long mx = -8e18;
void apply(const Tag &t) & {
mx += t.add;
}
Info operator+(const Info &b) {
return {max(mx, b.mx)};
}
};
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, q, d;
cin >> n >> q >> d;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> b(q);
for (int i = 0; i < q; i++) {
cin >> b[i];
}
vector<int> vals;
vals.insert(vals.end(), a.begin(), a.end());
vals.insert(vals.end(), b.begin(), b.end());
sort(vals.begin(), vals.end());
int C = vals.size();
vector<bool> taken(C, false);
Fenwick<int> fen(C);
LazySegmentTree<Info, Tag> seg1(C), seg2(C);
int mn = 1e9 + 10;
int mx = -mn;
n = 0;
auto query = [&]() -> long long {
return max({ mn - mx + 1LL * (n - 1) * d, seg1.rangeQuery(0, C).mx, seg2.rangeQuery(0, C).mx });
};
auto add = [&](int x) -> void {
if (x < mn) {
seg1.rangeApply(0, C, {-mn + x});
mn = x;
}
if (x > mx) {
seg2.rangeApply(0, C, {mx - x});
mx = x;
}
int i = lower_bound(vals.begin(), vals.end(), x) - vals.begin();
while (taken[i]) i++;
taken[i] = true;
fen.add(i, +1);
n++;
seg1.rangeApply(i + 1, C, {+d});
seg2.rangeApply(i + 1, C, {-d});
seg1.modify(i, {mn - x + 1LL * d * fen.sum(i)});
seg2.modify(i, {-mx + x + 1LL * (n - 1) * d - 1LL * d * fen.sum(i)});
};
for (int x : a) {
add(x);
}
for (int x : b) {
add(x);
long long ans = query();
cout << ans / 2;
if (ans & 1) cout << ".5";
cout << ' ';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
189 ms |
21040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
189 ms |
21040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |