QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#668230 | #7685. Barkley II | xDarkbluex | WA | 85ms | 3784kb | C++17 | 2.6kb | 2024-10-23 12:52:02 | 2024-10-23 12:52:03 |
Judging History
answer
#include <bits/stdc++.h>
constexpr int N = 5e5 + 5;
constexpr int INF = 1e9;
struct Tree {
int ls, rs;
int val;
}t[N * 45];
int idx;
void pushup(int p) {
t[p].val = t[t[p].ls].val + t[t[p].rs].val;
}
int build(int l, int r) {
int p = ++idx;
if (l == r) {
return p;
}
int mid = l + r >> 1;
t[p].ls = build(l, mid);
t[p].rs = build(mid + 1, r);
return p;
}
int modify(int now, int l, int r, int x, int val) {
int p = ++idx;
t[p] = t[now];
if (l == r) {
t[p].val = val;
return p;
}
int mid = l + r >> 1;
if (x <= mid) t[p].ls = modify(t[now].ls, l, mid, x, val);
else t[p].rs = modify(t[now].rs, mid + 1, r, x, val);
pushup(p);
return p;
}
int query(int p, int l, int r, int ql, int qr) {
if (ql > qr) return 0;
if (!p) return 0;
if (l >= ql && qr >= r) {
return t[p].val;
}
int mid = l + r >> 1, ans = 0;
if (ql <= mid) ans += query(t[p].ls, l, mid, ql, qr);
if (qr > mid) ans += query(t[p].rs, mid + 1, r, ql, qr);
return ans;
}
void solve() {
int n, m;
std::cin >> n >> m;
idx = 0;
std::vector<int> a(n + 1, 0);
std::vector<std::vector<int>> pos(m + 1);
std::vector<int> allnum;
allnum.emplace_back(1);
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
pos[a[i]].emplace_back(i);
allnum.emplace_back(a[i]);
}
std::sort(allnum.begin(), allnum.end());
allnum.erase(std::unique(allnum.begin(), allnum.end()), allnum.end());
std::vector<int> root(n + 1, 0);
root[0] = build(1, n);
std::vector<int> pre(m + 1, 0);
for (int i = 1; i <= n; i++) {
if (!pre[a[i]]) {
root[i] = modify(root[i - 1], 1, n, i, 1);
pre[a[i]] = i;
} else {
int tmp = modify(root[i - 1], 1, n, pre[a[i]], 0);
root[i] = modify(tmp, 1, n, i, 1);
pre[a[i]] = i;
}
}
int ans = -1;
for (auto mex : allnum) {
for (int i = -1, len = (int)pos[mex].size(); i < len; i++) {
int l = (i >= 0) ? pos[mex][i] : 0, r = (i + 1 < len) ? pos[mex][i + 1] : n + 1;
ans = std::max(ans, ((l + 1 > r - 1) ? 0 : query(root[r - 1], 1, n, l + 1, r - 1)) - mex);
}
}
std::cout << ans << '\n';
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr), std::cout.tie(nullptr);
int T;
std::cin >> T;
while (T--) {
solve();
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 3784kb
input:
2 5 4 1 2 2 3 4 5 10000 5 2 3 4 1
output:
2 3
result:
ok 2 number(s): "2 3"
Test #2:
score: -100
Wrong Answer
time: 85ms
memory: 3572kb
input:
50000 10 19 12 6 1 12 11 15 4 1 13 18 10 8 8 7 6 7 6 2 2 3 4 8 10 6 3 2 6 6 5 2 3 4 5 6 10 11 6 3 7 9 2 1 2 10 10 4 10 6 6 1 2 6 1 1 3 4 2 1 10 9 8 5 3 9 1 7 5 5 1 1 10 5 1 4 3 2 5 4 5 3 5 2 10 14 3 8 12 10 4 2 3 13 7 3 10 14 5 5 12 2 8 1 13 9 8 5 10 7 5 5 6 6 1 5 3 7 3 4 10 7 5 1 4 6 1 6 4 3 7 5 10...
output:
3 5 4 4 2 3 3 7 3 3 4 5 2 3 6 6 7 3 7 6 5 5 6 2 6 8 6 2 5 5 6 1 2 2 4 5 3 3 7 3 2 5 6 1 3 3 2 3 1 4 6 6 4 7 2 4 5 3 6 6 6 3 7 3 6 3 3 4 7 6 6 7 4 3 3 4 4 6 3 3 6 6 4 5 5 9 4 5 7 5 3 5 1 2 5 6 6 8 4 3 4 5 5 3 7 4 3 2 3 4 3 5 4 4 2 6 6 4 4 5 7 4 5 7 4 7 3 7 6 6 6 5 4 2 5 4 2 3 4 4 2 6 4 5 4 3 5 6 6 6 ...
result:
wrong answer 1st numbers differ - expected: '6', found: '3'