QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#520851 | #7605. Yet Another Mex Problem | liuziao | WA | 8ms | 46880kb | C++14 | 4.1kb | 2024-08-15 16:33:26 | 2024-08-15 16:33:26 |
Judging History
answer
#include <bits/stdc++.h>
#define int int64_t
using pii = std::pair<int, int>;
const int kMaxN = 2e5 + 5;
int n, k;
int a[kMaxN], sum[kMaxN], f[kMaxN];
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 a, int k) { return a.first * k + a.second; }
struct SGT1 {
int mi[kMaxN * 4];
void pushup(int x) { mi[x] = std::min(mi[x << 1], mi[x << 1 | 1]); }
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 ql) {
assert(mi[x] < ql);
if (l == r) return l;
int mid = (l + r) >> 1;
if (mi[x << 1] < ql)
return getmex(x << 1, l, mid, ql);
else
return getmex(x << 1 | 1, mid + 1, r, ql);
}
int query(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(query(x << 1, l, mid, ql, qr),
query(x << 1 | 1, mid + 1, r, ql, qr));
}
} sgt1;
struct SGT2 { // f[l] - mex * sum[l]
// (sum[l], f[l]) 上凸壳
std::vector<pii> v[kMaxN * 4];
void ins(int x, pii p) {
// if (!v[x].empty() && p <= v[x].back()) return;
// for (;
// v[x].size() >= 2 &&
// mul(sub(p, v[x].back()), sub(v[x].back(), v[x][v[x].size() - 2])) <= 0;
// v[x].pop_back()) {
// }
v[x].emplace_back(p);
}
int getmax(int x, int k) {
if (!v[x].size()) return -1e18;
// int L = 0, R = v[x].size(), res = 0;
// while (L + 1 < R) {
// int mid = (L + R) >> 1;
// if (func(v[x][mid], k) >= func(v[x][mid - 1], k))
// L = res = mid;
// else
// R = mid;
// }
// return func(v[x][res], k);
int ret = -1e18;
for (auto p : v[x]) ret = std::max(ret, func(p, k));
return ret;
}
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 k) {
if (l > qr || r < ql)
return -1e18;
else if (l >= ql && r <= qr)
return getmax(x, 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));
}
} sgt2, sgt3;
void dickdreamer() {
std::cin >> n >> k;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
sgt2.update(1, 0, n, 0, {0, 0}), sgt3.update(1, 0, n, 0, {0, 0});
for (int i = 1; i <= n; ++i) {
int l = sgt1.query(1, 0, n, 0, a[i]) + 1,
r = (a[i] ? sgt1.query(1, 0, n, 0, a[i] - 1) : i);
sgt1.update(1, 0, n, a[i], i);
// std::cerr << i << " : ---------\n";
for (; l <= r;) {
int mex = sgt1.getmex(1, 0, n, r), t = sgt1.query(1, 0, n, 0, mex);
int val = sgt2.query(1, 0, n, t, r - 1, -mex);
sgt3.update(1, 0, n, t, {mex, val});
r = t;
}
l = std::max<int>(i - k + 1, 1);
int mex = sgt1.getmex(1, 0, n, l);
r = (mex ? sgt1.query(1, 0, n, 0, mex - 1) : i);
assert(l <= r);
f[i] = std::max(sgt3.query(1, 0, n, l - 1, i - 1, sum[i]),
sgt2.query(1, 0, n, l - 1, r - 1, -mex) + sum[i] * mex);
sgt2.update(1, 0, n, i, {sum[i], f[i]});
if (a[i]) sgt3.update(1, 0, n, i, {0, f[i]});
// std::cerr << i << ' ' << f[i] << '\n';
}
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: 0ms
memory: 46576kb
input:
5 3 3 4 0 0 3
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: 0
Accepted
time: 7ms
memory: 44204kb
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: 7ms
memory: 44436kb
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: 8ms
memory: 46848kb
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: 45500kb
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: 0ms
memory: 46576kb
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: 4ms
memory: 46880kb
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: 4ms
memory: 44028kb
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: 4ms
memory: 45548kb
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:
1254
result:
wrong answer 1st numbers differ - expected: '1308', found: '1254'