QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#520125 | #5459. Goose, goose, DUCK? | no_RED_no_DEAD | WA | 2ms | 5868kb | C++20 | 2.9kb | 2024-08-15 10:58:11 | 2024-08-15 10:58:12 |
Judging History
answer
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
const ll N = 1e6 + 1;
const ll M = 1e9 + 7;
struct STN {ll val, cnt, lazy;};
struct SegmentTree {
const STN DEF = {0, 1, 0};
const STN DED = {10, 0, 0};
vector<STN> s; ll uu, vv;
SegmentTree(ll n): s(4 * n + 10, DEF), uu(1), vv(n) {}
STN merge(STN a, STN b) {
if (a.cnt == 0) return b;
if (b.cnt == 0) return a;
STN res;
if (a.val == b.val) res.cnt = a.cnt + b.cnt;
else if (a.val < b.val) res.cnt = a.cnt;
else res.cnt = b.cnt;
res.val = min(a.val, b.val);
res.lazy = 0;
return res;
}
void down(ll id, ll l, ll r) {
if (s[id].lazy == 0) return;
ll tmp = s[id].lazy;
s[id].lazy = 0;
s[id << 1].val += tmp;
s[id << 1 | 1].val += tmp;
s[id << 1].lazy += tmp;
s[id << 1 | 1].lazy += tmp;
}
void update(ll u, ll v, ll val) {update(u, v, val, 1, uu, vv);}
void update(ll u, ll v, ll val, ll id, ll l, ll r) {
if (l > v || r < u) return;
if (u <= l && r <= v) {
s[id].val += val;
s[id].lazy += val;
return;
}
down(id, l, r);
ll mid = (l + r) >> 1;
update(u, v, val, id << 1, l, mid);
update(u, v, val, id << 1 | 1, mid + 1, r);
s[id] = merge(s[id << 1], s[id << 1 | 1]);
}
STN get(ll u, ll v) {return get(u, v, 1, uu, vv);}
STN get(ll u, ll v, ll id, ll l, ll r) {
if (l > v || r < u) return DED;
if (u <= l && r <= v) return s[id];
down(id, l, r);
ll mid = (l + r) >> 1;
return merge(get(u, v, id << 1, l, mid), get(u, v, id << 1 | 1, mid + 1, r));
}
};
ll n, k;
ll a[N];
vector<ll> v[N];
void doTest(ll testID) {
cin >> n >> k; SegmentTree st(n);
for (int i = 1; i <= n; i ++) cin >> a[i], st.update(i, i, 0);
for (int i = 1; i <= n; i ++) v[i].push_back(0);
for (int i = 1; i <= n; i ++) v[a[i]].push_back(i);
ll res = 0;
for (int i = 1; i <= n; i ++) {
ll pos = lower_bound(v[a[i]].begin(), v[a[i]].end(), i) - v[a[i]].begin();
if (pos - k - 1 >= 0) {
ll l = v[a[i]][pos - k - 1] + 1, r = v[a[i]][pos - k];
st.update(l, r, -1);
}
if (pos - k >= 0) {
ll l = v[a[i]][pos - k] + 1, r = v[a[i]][pos - k + 1];
st.update(l, r, 1);
}
STN node = st.get(1, i);
if (node.val == 0) res += node.cnt;
// cout << "Debug: " << i << ' ' << pos << ' ' << node.val << ' ' << node.cnt << endl;
// for (int i = 1; i <= n; i ++) cout << st.get(i, i).val << ' '; cout << endl;
}
cout << res;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int test = 1;
// cin >> test;
for (int _ = 1; _ <= test; _ ++) doTest(test);
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 5568kb
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: 5868kb
input:
6 1 1 2 3 4 5 6
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: -100
Wrong Answer
time: 2ms
memory: 5692kb
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:
4325
result:
wrong answer 1st numbers differ - expected: '4309', found: '4325'