#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 {
long long mx = -2e18;
long long mn = 2e18;
void apply(const Tag &t) & {
mx += t.add;
mn += t.add;
}
Info operator+(const Info &b) {
return {max(mx, b.mx), min(mn, b.mn)};
}
};
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();
Fenwick<int> fen(C);
LazySegmentTree<Info, Tag> seg(C);
long long ans = 0;
auto query = [&]() -> long long {
return ans;
};
auto add = [&](int x) -> void {
int i = lower_bound(vals.begin(), vals.end(), x) - vals.begin();
vals[i]--;
fen.add(i, +1);
seg.rangeApply(i + 1, C, {-d});
long long v = x - 1LL * fen.sum(i) * d;
seg.modify(i, {v, v});
long long l = seg.rangeQuery(0, i).mx;
long long r = seg.rangeQuery(i + 1, C).mn;
ans = max(ans, l - r);
ans = max(ans, l - v);
ans = max(ans, v - r);
};
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 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
2 ms |
476 KB |
Output is correct |
3 |
Correct |
2 ms |
592 KB |
Output is correct |
4 |
Correct |
2 ms |
592 KB |
Output is correct |
5 |
Correct |
2 ms |
592 KB |
Output is correct |
6 |
Correct |
2 ms |
636 KB |
Output is correct |
7 |
Correct |
2 ms |
592 KB |
Output is correct |
8 |
Correct |
2 ms |
764 KB |
Output is correct |
9 |
Correct |
237 ms |
20088 KB |
Output is correct |
10 |
Correct |
223 ms |
20024 KB |
Output is correct |
11 |
Correct |
155 ms |
20020 KB |
Output is correct |
12 |
Correct |
191 ms |
20024 KB |
Output is correct |
13 |
Correct |
168 ms |
19656 KB |
Output is correct |
14 |
Correct |
169 ms |
20168 KB |
Output is correct |
15 |
Correct |
235 ms |
19512 KB |
Output is correct |
16 |
Correct |
165 ms |
20136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
18256 KB |
Output is correct |
2 |
Correct |
180 ms |
20052 KB |
Output is correct |
3 |
Correct |
184 ms |
20040 KB |
Output is correct |
4 |
Correct |
164 ms |
20060 KB |
Output is correct |
5 |
Correct |
190 ms |
20048 KB |
Output is correct |
6 |
Correct |
170 ms |
20040 KB |
Output is correct |
7 |
Correct |
172 ms |
20048 KB |
Output is correct |
8 |
Correct |
168 ms |
20048 KB |
Output is correct |
9 |
Correct |
180 ms |
20048 KB |
Output is correct |
10 |
Correct |
180 ms |
20232 KB |
Output is correct |
11 |
Correct |
179 ms |
20164 KB |
Output is correct |
12 |
Correct |
186 ms |
20048 KB |
Output is correct |
13 |
Correct |
170 ms |
20048 KB |
Output is correct |
14 |
Correct |
176 ms |
20040 KB |
Output is correct |
15 |
Correct |
183 ms |
20048 KB |
Output is correct |
16 |
Correct |
174 ms |
19528 KB |
Output is correct |
17 |
Correct |
186 ms |
20044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
168 ms |
18256 KB |
Output is correct |
2 |
Correct |
180 ms |
20052 KB |
Output is correct |
3 |
Correct |
184 ms |
20040 KB |
Output is correct |
4 |
Correct |
164 ms |
20060 KB |
Output is correct |
5 |
Correct |
190 ms |
20048 KB |
Output is correct |
6 |
Correct |
170 ms |
20040 KB |
Output is correct |
7 |
Correct |
172 ms |
20048 KB |
Output is correct |
8 |
Correct |
168 ms |
20048 KB |
Output is correct |
9 |
Correct |
180 ms |
20048 KB |
Output is correct |
10 |
Correct |
180 ms |
20232 KB |
Output is correct |
11 |
Correct |
179 ms |
20164 KB |
Output is correct |
12 |
Correct |
186 ms |
20048 KB |
Output is correct |
13 |
Correct |
170 ms |
20048 KB |
Output is correct |
14 |
Correct |
176 ms |
20040 KB |
Output is correct |
15 |
Correct |
183 ms |
20048 KB |
Output is correct |
16 |
Correct |
174 ms |
19528 KB |
Output is correct |
17 |
Correct |
186 ms |
20044 KB |
Output is correct |
18 |
Correct |
235 ms |
20044 KB |
Output is correct |
19 |
Correct |
242 ms |
20108 KB |
Output is correct |
20 |
Correct |
176 ms |
20048 KB |
Output is correct |
21 |
Correct |
205 ms |
20040 KB |
Output is correct |
22 |
Correct |
209 ms |
20052 KB |
Output is correct |
23 |
Correct |
188 ms |
20048 KB |
Output is correct |
24 |
Correct |
254 ms |
20176 KB |
Output is correct |
25 |
Correct |
181 ms |
20040 KB |
Output is correct |
26 |
Correct |
249 ms |
20048 KB |
Output is correct |
27 |
Correct |
276 ms |
20236 KB |
Output is correct |
28 |
Correct |
226 ms |
20020 KB |
Output is correct |
29 |
Correct |
224 ms |
20040 KB |
Output is correct |
30 |
Correct |
197 ms |
20048 KB |
Output is correct |
31 |
Correct |
199 ms |
20040 KB |
Output is correct |
32 |
Correct |
179 ms |
20040 KB |
Output is correct |
33 |
Correct |
217 ms |
19528 KB |
Output is correct |
34 |
Correct |
181 ms |
20060 KB |
Output is correct |