QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#339453 | #7605. Yet Another Mex Problem | oscaryang | RE | 0ms | 0kb | C++14 | 6.1kb | 2024-02-27 13:05:34 | 2024-02-27 13:05:36 |
answer
#include<bits/stdc++.h>
#define vc vector
#define pb emplace_back
#define pii pair<int, int>
#define mkp make_pair
#define rep(i, a, b) for(int i = (a); i <= (b); i++)
#define lep(i, a, b) for(int i = (a); i >= (b); i--)
#define int long long
using namespace std;
mt19937 gen(time(0));
inline int read() {
int x = 0, w = 0; char ch = getchar(); while(!isdigit(ch)) w |= (ch == '-'), ch = getchar();
while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar(); return w ? -x : x;
}
const int N = 2e5 + 5, inf = 1e17;
int n, k, f[N], a[N], pr[N], s[N], len, b[N << 1];
struct Mex { int l, r, v; bool friend operator < (Mex A, Mex B) { return A.l < B.l; } };
vc<Mex> o[N];
set<Mex> S;
#define mid (l + r >> 1)
#define ls (k << 1)
#define rs (k << 1 | 1)
namespace sgt1 {
int mn[N << 2], mx[N << 2];
inline void build(int k, int l, int r) {
mx[k] = n + 1; mn[k] = 0;
if(l != r) build(ls, l, mid), build(rs, mid + 1, r);
}
inline void psu(int k) {
mx[k] = max(mx[ls], mx[rs]);
mn[k] = min(mn[ls], mn[rs]);
}
inline void ins(int k, int l, int r, int p, int v) {
if(l == r) return mn[k] = mx[k] = v, void();
return (p <= mid ? ins(ls, l, mid, p, v) : ins(rs, mid + 1, r, p, v)), psu(k);
}
inline int askmn(int k, int l, int r, int L, int R) {
if(L > R || L > r || R < l) return n + 1;
if(L <= l && R >= r) return mn[k];
return min(askmn(ls, l, mid, L, R), askmn(rs, mid + 1, r, L, R));
}
inline int askmx(int k, int l, int r, int L, int R) {
if(L > R || L > r || R < l) return 0;
if(L <= l && R >= r) return mx[k];
return max(askmx(ls, l, mid, L, R), askmx(rs, mid + 1, r, L, R));
}
}
struct line { int k, b; } tre[N * 40];
inline int calc(line A, int x) { return A.k * x + A.b; }
namespace lichao {
int lc[N * 40], rc[N * 40], idx = 1;
inline void ins(int &k, int l, int r, line v) {
if(!k) return tre[k = ++idx] = v, void();
if(calc(v, b[mid]) > calc(tre[k], b[mid])) swap(tre[k], v);
if(calc(v, b[l]) > calc(tre[k], b[l])) ins(lc[k], l, mid, v);
if(calc(v, b[r]) > calc(tre[k], b[r])) ins(rc[k], mid + 1, r, v);
}
inline int ask(int k, int l, int r, int p) {
if(!k) return -inf; int val = calc(tre[k], b[p]);
if(l == r) return val;
return max(val, p <= mid ? ask(lc[k], l, mid, p) : ask(rc[k], mid + 1, r, p));
}
}
namespace sgt2 {
int tre[N << 2];
inline void modify(int k, int l, int r, int L, int R, line v) {
if(L > R || L > r || R < l) return ;
if(L <= l && R >= r) return lichao :: ins(tre[k], 1, len, v);
modify(ls, l, mid, L, R, v); modify(rs, mid + 1, r, L, R, v);
}
inline int ask(int k, int l, int r, int p, int v) {
int val = lichao :: ask(tre[k], 1, len, v);
if(l == r) return val;
return max(val, p <= mid ? ask(ls, l, mid, p, v) : ask(rs, mid + 1, r, p, v));
}
}
namespace sgt3 {
int tre[N << 2];
inline void ins(int k, int l, int r, int p, line v) {
lichao :: ins(tre[k], 1, len, v);
if(l != r) return p <= mid ? ins(ls, l, mid, p, v) : ins(rs, mid + 1, r, p, v);
}
inline int ask(int k, int l, int r, int L, int R, int p) {
if(L > R || L > r || R < l) return -inf;
if(L <= l && R >= r) return lichao :: ask(tre[k], 1, len, p);
return max(ask(ls, l, mid, L, R, p), ask(rs, mid + 1, r, L, R, p));
}
}
namespace sgt4 {
int rt[N], tre[N * 20], lc[N * 20], rc[N * 20], idx;
inline void psu(int k) { tre[k] = min(tre[lc[k]], tre[rc[k]]); }
inline void ins(int &k, int pr, int l, int r, int p, int v) {
k = ++idx; tre[k] = tre[pr]; lc[k] = lc[pr]; rc[k] = rc[pr];
if(l == r) return tre[k] = v, void();
return (p <= mid ? ins(lc[k], lc[pr], l, mid, p, v) : ins(rc[k], rc[pr], mid + 1, r, p, v)), psu(k);
}
inline int binary(int k, int l, int r, int lmt) {
if(!k || l == r) return l;
if(tre[lc[k]] < lmt) return binary(lc[k], l, mid, lmt);
else return binary(rc[k], mid + 1, r, lmt);
}
inline int ask(int k, int l, int r, int p) {
if(!k) return 0;
if(l == r) return tre[k];
return p <= mid ? ask(lc[k], l, mid, p) : ask(rc[k], mid + 1, r, p);
}
}
inline int mex(int l, int r) {
return sgt4 :: binary(sgt4 :: rt[r], 0, n, l);
}
inline void collect() {
sgt1 :: build(1, 0, n);
rep(i, 1, n) {
int r = a[i] ? sgt1 :: askmn(1, 0, n, 0, a[i] - 1) : i;
if(r) {
int v = mex(r, i);
int l = sgt4 :: ask(sgt4 :: rt[i], 0, n, v);
if(l < r) o[i].pb(Mex{l + 1, r, v});
}
if(a[i]) o[i].pb(Mex{sgt1 :: askmn(1, 0, n, 0, 0) + 1, i, 0});
sgt1 :: ins(1, 0, n, a[i], i);
}
sgt1 :: build(1, 0, n);
lep(i, n, 1) {
int r = a[i] ? sgt1 :: askmx(1, 0, n, 0, a[i] - 1) : i;
if(r <= n) {
int v = mex(i, r);
int l = sgt4 :: ask(sgt4 :: rt[r], 0, n, v);
if(l < i) o[r].pb(Mex{l + 1, i, v});
}
sgt1 :: ins(1, 0, n, a[i], i);
}
/*
rep(i, 1, n) {
cerr << i << endl;
for(auto u : o[i]) cerr << u.l << " " << u.r << " " << i << " " << u.v << endl;
}
*/
}
inline void upd(int l, int r, int v) {
int val = sgt3 :: ask(1, 1, n, l, r, v + 1);
sgt2 :: modify(1, 1, n, l, l + k - 1, line{v, val});
S.insert(Mex{l, r, v});
}
signed main() {
freopen("seq.in", "r", stdin);
freopen("seq.out", "w", stdout);
n = read(); k = read();
rep(i, 0, n) b[++len] = i;
rep(i, 1, n) {
a[i] = read(), b[++len] = s[i] = s[i - 1] + a[i];
sgt4 :: ins(sgt4 :: rt[i], sgt4 :: rt[i - 1], 0, n, a[i], i);
}
sort(b + 1, b + 1 + len); len = unique(b + 1, b + 1 + len) - b - 1;
collect();
// exit(0);
rep(i, 1, n) {
sgt3 :: ins(1, 1, n, i, line{-s[i - 1], f[i - 1]});
upd(i, i, a[i] ? 0 : 1);
if((*S.begin()).l + k <= i) {
auto u = *S.begin(); S.erase(u);
if(u.l < u.r) upd(u.l + 1, u.r, u.v);
}
for(auto u : o[i]) {
vc<Mex> cur;
for(auto it = S.lower_bound(Mex{u.l, 0, 0}); it != S.end() && (*it).l <= u.r; ++it) cur.pb(*it);
for(auto v : cur) S.erase(v);
for(auto v : cur) {
int l = max(u.l, v.l), r = min(u.r, v.r);
if(v.l < l) upd(v.l, l - 1, v.v);
if(v.r > r) upd(r + 1, v.r, v.v);
}
upd(max(u.l, i - k + 1), u.r, u.v);
}
f[i] = sgt2 :: ask(1, 1, n, i, lower_bound(b + 1, b + 1 + len, s[i]) - b);
}
cout << f[n] << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
5 3 3 4 0 0 3