QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#176925 | #5459. Goose, goose, DUCK? | lostintianyi# | WA | 7ms | 52920kb | C++17 | 1.7kb | 2023-09-12 10:39:32 | 2023-09-12 10:39:33 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = int64_t;
constexpr int N = 1e6 + 10;
int Front[N];
int pre[N], a[N];
int nxt[N];
int fst[N];
int cnt[N];
int n, k;
struct node {
int l, r;
int num;
int pre;
int lst;
} tr[N << 2];
inline void pushup(int u) {
tr[u].pre = std::min(tr[u << 1].pre, tr[u << 1 | 1].pre);
tr[u].lst = std::max(tr[u << 1].lst, tr[u << 1 | 1].lst);
}
void build(int u, int l, int r) {
tr[u].l = l, tr[u].r = r;
if (l == r) {
tr[u].pre = tr[u].lst = 1;
return ;
}
int mid = l + r >> 1;
build(u << 1, l, mid);
build(u << 1 | 1, mid + 1, r);
pushup(u);
}
void insert(int u, const int &col) {
if (tr[u].l == tr[u].r) {
tr[u].num++;
if (tr[u].num == k)
tr[u].lst = fst[col], tr[u].pre = 1;
else if (tr[u].num > k) {
tr[u].pre = tr[u].lst + 1, tr[u].lst = nxt[tr[u].lst];
}
return ;
}
int mid = tr[u].l + tr[u].r >> 1;
if (col <= mid)
insert(u << 1, col);
else
insert(u << 1 | 1, col);
pushup(u);
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cin >> n >> k;
for (int i = 1; i <= n; i++)
nxt[i] = n + 1;
int mx = 0;
for (int i = 1; i <= n; i++) {
std::cin >> a[i];
pre[i] = Front[a[i]];
nxt[Front[a[i]]] = i;
Front[a[i]] = i;
if (!fst[a[i]])
fst[a[i]] = i;
mx = std::max(mx, a[i]);
}
build(1, 1, mx);
bool gek = false;
i64 ans = 0;
for (int i = 1; i <= n; i++) {
insert(1, a[i]);
cnt[a[i]]++;
if (cnt[a[i]] >= k)
gek = true;
if (gek) {
int minn = tr[1].pre, maxn = tr[1].lst;
ans += minn - 1 + (i - maxn);
} else {
ans += i;
}
}
std::cout << ans << '\n';
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 11860kb
input:
6 2 1 2 2 1 3 3
output:
10
result:
ok 1 number(s): "10"
Test #2:
score: 0
Accepted
time: 2ms
memory: 11788kb
input:
6 1 1 2 3 4 5 6
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 7ms
memory: 52920kb
input:
100 10 142826 764475 142826 986320 764475 142826 142826 986320 764475 986320 764475 764475 764475 142826 142826 986320 764475 986320 764475 764475 142826 764475 142826 764475 986320 986320 764475 142826 764475 764475 142826 764475 764475 986320 142826 142826 142826 142826 764475 986320 986320 764475...
output:
1943
result:
wrong answer 1st numbers differ - expected: '4309', found: '1943'