QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#559308 | #7605. Yet Another Mex Problem | liuziao | WA | 3ms | 32380kb | C++14 | 5.4kb | 2024-09-11 21:17:37 | 2024-09-11 21:17:39 |
Judging History
answer
#include <bits/stdc++.h>
#define int int64_t
using pii = std::pair<int, int>;
const int kMaxN = 2e5 + 5, kMaxT = kMaxN * 40;
int n, k, line_tot;
int a[kMaxN], sum[kMaxN], f[kMaxN];
pii line[kMaxT];
int addl(int k, int b) {
line[++line_tot] = {k, b};
return line_tot;
}
pii sub(pii a, pii b) { return {a.first - b.first, a.second - b.second}; }
int mul(pii a, pii b) { return a.first * b.second - a.second * b.first; }
int func(pii p, int x) { return x * p.first + p.second;}
int func(int k, int x) {
if (!k) return -1e18;
else return func(line[k], x);
}
struct SGT_MEX {
int mi[kMaxN * 4];
void pushup(int x) {
mi[x] = std::min(mi[x << 1], mi[x << 1 | 1]);
}
void build(int x, int l, int r) {
if (l == r) return;
int mid = (l + r) >> 1;
build(x << 1, l, mid), build(x << 1 | 1, mid + 1, r);
}
void update(int x, int l, int r, int ql, int v) {
if (l == r) return void(mi[x] = v);
int mid = (l + r) >> 1;
if (ql <= mid) update(x << 1, l, mid, ql, v);
else update(x << 1 | 1, mid + 1, r, ql, v);
pushup(x);
}
int getmex(int x, int l, int r, int p) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (mi[x << 1] < p) return getmex(x << 1, l, mid, p);
else return getmex(x << 1 | 1, mid + 1, r, p);
}
int getmin(int x, int l, int r, int ql, int qr) {
if (l > qr || r < ql) return 1e9;
else if (l >= ql && r <= qr) return mi[x];
int mid = (l + r) >> 1;
return std::min(getmin(x << 1, l, mid, ql, qr), getmin(x << 1 | 1, mid + 1, r, ql, qr));
}
} sgt_mex;
struct SGT_VEC {
std::vector<pii> v[kMaxN * 4];
void ins(int x, pii p) {
for (; (int)v[x].size() >= 2 && mul(sub(p, v[x].back()), sub(v[x].back(), v[x][(int)v[x].size() - 2])) <= 0; v[x].pop_back()) {}
v[x].emplace_back(p);
}
int getval(int x, int y) {
if (!v[x].size()) return -1e18;
int L = 0, R = (int)v[x].size(), res = 0;
while (L + 1 < R) {
int mid = (L + R) >> 1;
if (func(v[x][mid], y) >= func(v[x][mid - 1], y)) L = res = mid;
else R = mid;
}
return func(v[x][res], y);
}
void update(int x, int l, int r, int ql, pii p) {
ins(x, p);
if (l == r) return;
int mid = (l + r) >> 1;
if (ql <= mid) update(x << 1, l, mid, ql, p);
else update(x << 1 | 1, mid + 1, r, ql, p);
}
int query(int x, int l, int r, int ql, int qr, int v) {
if (l > qr || r < ql) return -1e18;
else if (l >= ql && r <= qr) return getval(x, v);
int mid = (l + r) >> 1;
return std::max(query(x << 1, l, mid, ql, qr, v), query(x << 1 | 1, mid + 1, r, ql, qr, v));
}
} sgt_vec;
struct SGT_LCT {
int tot, ls[kMaxT], rs[kMaxT], tag[kMaxT];
void update(int &x, int l, int r, int k) {
if (!x) return void(tag[x = ++tot] = k);
int mid = (l + r) >> 1;
if (func(tag[x], mid) < func(k, mid)) std::swap(tag[x], k);
if (l == r) return;
if (func(tag[x], l) < func(k, l)) update(ls[x], l, mid, k);
if (func(tag[x], r) < func(k, r)) update(rs[x], mid + 1, r, k);
}
int query(int x, int l, int r, int k) {
if (!x || l > k || r < k) return -1e18;
else if (l == r) return func(tag[x], k);
int mid = (l + r) >> 1;
return std::max({query(ls[x], l, mid, k), query(rs[x], mid + 1, r, k), func(tag[x], k)});
}
} sgt_lct;
struct SGT {
int rt[kMaxN * 4];
void update(int x, int l, int r, int ql, int k) {
sgt_lct.update(rt[x], 0, 1e18, k);
if (l == r) return;
int mid = (l + r) >> 1;
if (ql <= mid) update(x << 1, l, mid, ql, k);
else update(x << 1 | 1, mid + 1, r, ql, k);
}
int query(int x, int l, int r, int ql, int qr, int k) {
if (l > qr || r < ql) return -1e18;
else if (l >= ql && r <= qr) return sgt_lct.query(rt[x], 0, 1e18, k);
int mid = (l + r) >> 1;
return std::max(query(x << 1, l, mid, ql, qr, k), query(x << 1 | 1, mid + 1, r, ql, qr, k));
}
} sgt;
void dickdreamer() {
std::cin >> n >> k;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
sgt_vec.update(1, 0, n, 0, {0, 0});
for (int i = 1; i <= n; ++i) {
// std::cerr << "!!!\n";
int r = (!a[i] ? i : sgt_mex.getmin(1, 0, n, 0, a[i] - 1)), l = sgt_mex.getmin(1, 0, n, 0, a[i]);
sgt_mex.update(1, 0, n, a[i], i);
// std::cerr << i << ' ' << l << ' ' << r << '\n';
for (; r > l;) {
int mex = sgt_mex.getmex(1, 0, n, r), p = sgt_mex.getmin(1, 0, n, 0, mex);
int val = sgt_vec.query(1, 0, n, p, r - 1, -mex);
sgt.update(1, 1, n, p + 1, addl(mex, val));
r = p;
}
int p = std::max<int>(i - k + 1, 1), mex = sgt_mex.getmex(1, 0, n, p);
f[i] = sgt.query(1, 1, n, p, i, sum[i]);
r = (!mex ? i : sgt_mex.getmin(1, 0, n, 0, mex - 1));
f[i] = std::max(f[i], mex * sum[i] + sgt_vec.query(1, 0, n, p - 1, r - 1, -mex));
assert(p <= r && sgt_mex.getmex(1, 0, n, r) == mex);
sgt_vec.update(1, 0, n, i, {sum[i], f[i]});
sgt.update(1, 1, n, i, addl(0, 0));
}
std::cout << f[n] << '\n';
}
int32_t main() {
#ifdef ORZXKR
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
// std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 32272kb
input:
5 3 3 4 0 0 3
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: 0
Accepted
time: 0ms
memory: 32320kb
input:
8 4 0 1 2 0 3 1 4 1
output:
26
result:
ok 1 number(s): "26"
Test #3:
score: 0
Accepted
time: 0ms
memory: 32248kb
input:
10 5 0 2 0 1 2 1 0 2 2 1
output:
33
result:
ok 1 number(s): "33"
Test #4:
score: 0
Accepted
time: 0ms
memory: 32380kb
input:
20 10 3 1 3 2 3 3 0 1 3 0 2 3 3 3 3 1 3 0 0 3
output:
160
result:
ok 1 number(s): "160"
Test #5:
score: 0
Accepted
time: 0ms
memory: 32332kb
input:
30 10 14 15 10 1 14 1 8 0 12 8 6 15 1 8 9 12 15 10 11 12 7 10 10 3 3 10 8 14 13 13
output:
172
result:
ok 1 number(s): "172"
Test #6:
score: 0
Accepted
time: 2ms
memory: 32276kb
input:
40 3 0 4 2 4 3 1 5 5 2 3 4 2 1 1 1 5 5 4 1 3 3 0 1 0 2 0 1 4 2 1 5 3 0 4 0 0 0 5 3 3
output:
51
result:
ok 1 number(s): "51"
Test #7:
score: 0
Accepted
time: 0ms
memory: 32248kb
input:
50 20 29 6 30 26 8 11 22 26 24 8 30 25 19 12 28 19 28 4 13 9 2 23 30 15 21 5 30 5 19 17 25 29 2 28 20 16 0 4 26 23 22 30 3 25 29 5 29 24 11 27
output:
378
result:
ok 1 number(s): "378"
Test #8:
score: 0
Accepted
time: 0ms
memory: 32260kb
input:
80 15 2 13 20 11 12 10 19 17 3 7 10 2 14 11 9 17 19 11 17 15 10 18 11 11 14 5 20 8 8 12 13 17 14 19 11 20 13 2 12 2 19 12 6 7 3 4 11 16 1 12 4 16 17 4 1 2 5 11 17 12 13 7 8 12 2 4 15 20 18 1 1 13 1 14 6 5 20 12 12 11
output:
0
result:
ok 1 number(s): "0"
Test #9:
score: -100
Wrong Answer
time: 0ms
memory: 30244kb
input:
100 30 41 50 33 22 1 34 7 14 31 44 46 49 49 23 33 12 9 41 25 23 22 1 49 45 45 8 10 49 37 23 48 17 32 44 38 21 29 27 23 27 47 44 6 12 7 2 18 12 9 43 20 26 26 8 14 25 18 48 2 41 49 7 48 38 10 45 34 27 17 12 1 19 9 29 50 13 25 21 9 13 26 15 6 9 5 13 47 30 26 17 40 0 21 6 25 24 22 31 43 16
output:
1222
result:
wrong answer 1st numbers differ - expected: '1308', found: '1222'