QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#311880 | #7605. Yet Another Mex Problem | Cocoly1990 | WA | 48ms | 316192kb | C++17 | 4.1kb | 2024-01-22 22:25:47 | 2024-01-22 22:25:47 |
Judging History
answer
// Stop the noise and stop the clocks
// Problem: J. Yet Another Mex Problem
// Contest: Codeforces - MEX Foundation Contest (supported by AIM Tech)
// URL: https://codeforces.com/gym/102412/problem/J
// Time: 2024-01-22 20:28:59
// Memory Limit: 512 MB
// Author: Cocoly1990
// Time Limit: 4000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#pragma GCC optimize("Ofast","inline","-ffast-math")
#pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<bits/stdc++.h>
using namespace std;
using i64 = long long;
const int N = 2e5 + 7, SEG_SIZE = N * 100;
const i64 inf = 0x3f3f3f3f3f3f3f3f;
struct Line {
i64 k, b;
Line (i64 k = 0, i64 b = -inf) : k (k), b (b) {}
i64 val (i64 x) {
return 1ll * k * x + b;
}
} info[SEG_SIZE];
struct Node {
int cnt = 0, L[SEG_SIZE], R[SEG_SIZE];
void insert (int &p, i64 l, i64 r, Line g) {
if (! p) {
p = ++ cnt;
assert (cnt < SEG_SIZE);
info[p] = g;
return void ();
}
if (l == r) return;
i64 mid = (l + r) >> 1;
if (info[p].val (mid) < g.val (mid)) swap (info[p], g);
if (g.k > info[p].k) insert (R[p], mid + 1, r, g);
else insert (L[p], l, mid, g);
}
i64 query (int p, i64 l, i64 r, i64 x) {
if (! p or l == r) return info[p].val (x);
i64 res = info[p].val (x);
i64 mid = (l + r) >> 1;
if (x <= mid) res = max (res, query (L[p], l, mid, x));
else res = max (res, query (R[p], mid + 1, r, x));
return res;
}
} t;
struct Seg {
int rt[N << 2];
i64 V;
void modify (int p, int l, int r, int x, Line g) {
t.insert (rt[p], 0, V, g);
if (l == r) {
assert (l == x);
return void ();
}
int mid = (l + r) >> 1;
if (x <= mid) modify (p << 1, l, mid, x, g);
else modify (p << 1 | 1, mid + 1, r, x, g);
}
i64 query (int p, int l, int r, int ql, int qr, i64 x) {
if (ql <= l and r <= qr) return t.query (rt[p], 0, V, x);
int mid = (l + r) >> 1;
i64 res = -inf;
if (ql <= mid) res = max (res, query (p << 1, l, mid, ql, qr, x));
if (qr > mid) res = max (res, query (p << 1 | 1, mid + 1, r, ql, qr, x));
return res;
}
} seg1, seg2;
struct Segmex {
int mn[N << 2];
void modify (int p, int l, int r, int x, int k) {
if (l == r) {
assert (l == x);
mn[p] = k;
return void ();
}
int mid = (l + r) >> 1;
if (x <= mid) modify (p << 1, l, mid, x, k);
else modify (p << 1 | 1, mid + 1, r, x, k);
mn[p] = min (mn[p << 1], mn[p << 1 | 1]);
}
i64 query (int p, int l, int r, int ql, int qr) {
if (ql <= l and r <= qr) return mn[p];
i64 res = inf;
int mid = (l + r) >> 1;
if (ql <= mid) res = min (res, query (p << 1, l, mid, ql, qr));
if (qr > mid) res = min (res, query (p << 1 | 1, mid + 1, r, ql, qr));
return res;
}
int find (int p, int l, int r, int x) {
if (l == r) return l;
int mid = (l + r) >> 1;
if (mn[p << 1] <= x) return find (p << 1, l, mid, x);
else return find (p << 1 | 1, mid + 1, r, x);
}
} segmx;
int n, K, a[N];
i64 sum[N], f[N];
signed main () {
ios :: sync_with_stdio (false); cin.tie (0); cout.tie (0);
cin >> n >> K;
for (int i = 1; i <= n; i ++)
cin >> a[i], sum[i] = sum[i - 1] + a[i];
seg1.V = n;
seg2.V = sum[n];
seg1.modify (1, 0, n, 0, Line (0, 0));
seg2.modify (1, 0, n, 0, Line (0, 0));
for (int i = 1; i <= n; i ++) {
int l = segmx.query (1, 0, n, 0, a[i]);
int r = (! a[i]) ? i : segmx.query (1, 0, n, 0, a[i] - 1);
if (i == 3) cout << l << " " << r << "\n";
segmx.modify (1, 0, n, a[i], i);
while (l < r) {
int v = segmx.find (1, 0, n, r - 1);
int pos = segmx.query (1, 0, n, 0, v);
seg2.modify (1, 0, n, pos, Line (v, seg1.query (1, 0, n, pos, r - 1, v)));
r = pos;
}
f[i] = seg2.query (1, 0, n, max (0, i - K), i - 1, sum[i]);
l = max (0, i - K);
int v = segmx.find (1, 0, n, l - 1);
r = (! v) ? i : segmx.query (1, 0, n, 0, v - 1);
if (l < r) f[i] = max (f[i], 1ll * v * sum[i] + seg1.query (1, 0, n, l, r - 1, v));
seg1.modify (1, 0, n, i, Line (-sum[i], f[i]));
seg2.modify (1, 0, n, segmx.query (1, 0, n, 0, 0), Line (0, f[i]));
}
cout << f[n];
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 48ms
memory: 316192kb
input:
5 3 3 4 0 0 3
output:
0 3 10
result:
wrong answer 1st numbers differ - expected: '10', found: '0'