QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#725420 | #7685. Barkley II | 2024tangjiaqi | WA | 167ms | 11856kb | C++14 | 2.4kb | 2024-11-08 17:37:10 | 2024-11-08 17:37:11 |
Judging History
answer
#include <bits/stdc++.h>
#define PII pair <int, int>
#define ll long long
#define MP make_pair
#define PB push_back
using namespace std;
constexpr int N = 5e5 + 10;
int T, n, m, a[N], cnt, last[N], Next[N], app[N], fir[N], root[N], ans, mx;
map <int, int> maps;
struct node
{
int ls, rs, val;
#define ls(p) t[p].ls
#define rs(p) t[p].rs
#define val(p) t[p].val
} t[N << 5];
int update (int p, int l, int r, int pos, int x)
{
int rt = ++cnt;
t[rt] = t[p]; val (rt) += x;
if (l == r) return rt;
int mid = (l + r) >> 1;
if (mid >= pos) ls (rt) = update (ls (p), l, mid, pos, x);
else rs (rt) = update (rs (p), mid + 1, r, pos, x);
return rt;
}
int query (int p, int l, int r, int pos)
{
if (l == r) return val (p);
int mid = (l + r) >> 1;
if (mid >= pos) return query (ls (p), l, mid, pos) + val (rs (p));
else return query (rs (p), mid + 1, r, pos);
}
inline void clear ()
{
ans = -1; cnt = 0;
maps.clear ();
for (int i = 1; i <= n; i++) last[i] = 0;
for (int i = 1; i <= m; i++) fir[i] = app[i] = 0;
}
int main()
{
ios::sync_with_stdio (false);
cin.tie (0);
cout.tie (0);
cin >> T;
while (T--)
{
clear ();
cin >> n >> m;
for (int i = 1; i <= n; i++) {cin >> a[i]; mx = max (mx, a[i]); if (!fir[a[i]]) fir[a[i]] = i;}
for (int i = n; i >= 1; i--) {Next[i] = maps[a[i]]; maps[a[i]] = i; app[a[i]]++;}
for (int i = 1; i <= n; i++) {if (!Next[i]) Next[i] = n + 1;}
for (int i = 1; i <= n; i++)
{
if (!last[a[i]]) root[i] = update (root[i - 1], 1, N, i, 1);
else {root[i] = update (root[i - 1], 1, N, last[a[i]], -1); root[i] = update (root[i], 1, N, i, 1);}
last[a[i]] = i;
}
// cout << query (root[n], 1, N, 1) << "\n";
// cout << app[1] << "\n";
for (int i = 1; i <= mx; i++)
{
int pos = fir[i];
// cout << i << " " << pos << " " << Next[pos] << " " << query (root[pos -1], 1, N, 1) - i << "\n";
if (!app[i]) {ans = max (ans, query (root[n], 1, N, 1) - i); continue;}
ans = max (ans, query (root[pos - 1], 1, N, 1) - i);
for (int j = 1; j <= app[i]; j++) {ans = max (ans, query (root[Next[pos] - 1], 1, N, pos + 1) - i); pos = Next[pos];}
}
cout << ans << "\n";
}
return 0;
}
/*
1
1 1
1
*/
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 11856kb
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: 167ms
memory: 11800kb
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:
6 5 4 4 2 4 3 5 2 4 4 5 2 3 2 6 5 3 3 5 5 2 4 2 4 4 3 2 4 5 5 2 2 3 4 5 1 3 7 3 2 2 6 1 3 2 3 3 3 3 4 4 4 4 4 4 5 0 5 4 2 2 2 3 6 3 3 2 6 3 4 4 4 3 3 4 1 3 3 4 4 4 4 5 1 4 4 5 4 3 3 5 2 2 4 6 4 4 4 3 3 3 3 3 3 3 3 2 3 1 3 4 2 4 2 5 5 3 3 2 4 4 5 4 4 4 1 4 6 2 5 5 3 2 3 3 2 3 3 2 2 3 3 5 4 3 3 6 5 3 ...
result:
wrong answer 8th numbers differ - expected: '7', found: '5'