QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#673828 | #5278. Mex and Cards | IllusionaryDominance# | WA | 1ms | 5700kb | C++20 | 3.6kb | 2024-10-25 10:44:22 | 2024-10-25 10:44:24 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAX_N = 200000 + 5;
int N, Q, cnt[MAX_N], pre[MAX_N], minv[1 << 19];
struct Node{
int l, r, v;
Node (int l_ = 0, int r_ = 0, int v_ = 0) : l(l_), r(r_), v(v_) {}
inline bool operator < (const Node &comp) const {return l < comp.l;}
};
set <Node> s;
ll ans;
// find the first element < x in [ql, qr]
int segment_find(int i, int l, int r, int ql, int qr, int x) {
if (minv[i] >= x) return -1;
if (l < ql || r > qr) {
int mid = l + r >> 1, res = -1;
if (ql <= mid) res = segment_find(i << 1, l, mid, ql, qr, x);
if (res < 0 && mid < qr) res = segment_find(i << 1 | 1, mid + 1, r, ql, qr, x);
return res;
}
while (l < r) {
int mid = l + r >> 1;
if (minv[i << 1] < x) {
r = mid;
i <<= 1;
}else {
l = mid + 1;
i = i << 1 | 1;
}
}
assert(minv[i] < x);
return l;
}
void segment_build(int i, int l, int r) {
if (l == r) {
minv[i] = cnt[l];
return ;
}
int mid = l + r >> 1;
segment_build(i << 1, l, mid);
segment_build(i << 1 | 1, mid + 1, r);
minv[i] = min(minv[i << 1], minv[i << 1 | 1]);
}
void segment_modify(int i, int l, int r, int x, int v) {
if (l == r) {
minv[i] += v;
return ;
}
int mid = l + r >> 1;
mid < x ? segment_modify(i << 1 | 1, mid + 1, r, x, v) : segment_modify(i << 1, l, mid, x, v);
minv[i] = min(minv[i << 1], minv[i << 1 | 1]);
}
void modify(int p, int x) {
segment_modify(1, 1, N, p, x);
if (x < 0) {
auto it = -- s.upper_bound(Node(p));
auto [l, r, v] = *it;
if (v == cnt[p]) {
auto it1 = it; it1 ++;
s.erase(it);
ans -= r - p + 1;
if (l < p) s.insert(Node(l, p - 1, v));
if (r < N) {
assert(it1 != s.end());
auto [l1, r1, v1] = *it1;
if (v1 == v - 1) {
r = r1;
s.erase(it1);
}
}
s.insert(Node(p, r, v - 1));
}
cnt[p] --;
}else {
auto it = -- s.upper_bound(Node(p));
auto [l, r, v] = *it;
if (v == cnt[p] && p == l) {
auto it1 = it;
if (it != s.begin()) it1 --;
s.erase(it);
int p1 = segment_find(1, 1, N, l, r, v + 1);
if (p1 == -1) p1 = r + 1;
ans += p1 - p;
if (p1 <= r) {
assert(cnt[p1] == v);
s.insert(Node(p1, r, v));
r = p1 - 1;
}
if (l > 1) {
auto [l1, r1, v1] = *it1;
if (l1 == v + 1) {
l = l1;
s.erase(it1);
}
}
s.insert(Node(l, r, v + 1));
}
cnt[p] ++;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> N;
pre[0] = 0x3f3f3f3f;
for (int i = 1; i <= N; i ++) {
cin >> cnt[i];
pre[i] = min(pre[i - 1], cnt[i]);
ans += pre[i];
}
segment_build(1, 1, N);
cout << ans << '\n';
for (int i = 1, j; i <= N; i = j) {
for (j = i; j <= N && pre[i] == pre[j]; j ++);
s.insert(Node(i, j - 1, pre[i]));
}
cin >> Q;
while (Q --) {
int ty, v;
cin >> ty >> v;
if (ty == 2) ty = -1;
modify(v + 1, ty);
cout << ans << '\n';
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3628kb
input:
5 2 1 3 0 2 6 1 0 1 1 2 4 1 3 2 1 2 1
output:
4 5 7 7 9 7 3
result:
ok 7 numbers
Test #2:
score: 0
Accepted
time: 1ms
memory: 5700kb
input:
1 0 0
output:
0
result:
ok 1 number(s): "0"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3688kb
input:
10 3 8 1 4 10 3 10 9 7 10 20 2 5 1 4 1 2 1 4 1 3 1 3 1 0 2 8 1 5 1 4 1 0 1 3 1 8 1 6 1 4 1 1 1 5 1 9 1 6 2 7
output:
14 14 14 22 22 22 22 24 24 24 24 26 26 26 26 26 26 26 26 26 26
result:
ok 21 numbers
Test #4:
score: -100
Wrong Answer
time: 1ms
memory: 5628kb
input:
10 9 8 7 5 5 4 3 2 1 1 20 2 4 1 8 2 6 1 2 1 2 2 5 2 2 1 0 1 6 1 6 2 9 1 2 2 7 2 8 2 3 1 9 1 7 1 4 2 6 1 7
output:
45 44 45 44 45 46 45 44 45 46 47 46 47 46 45 44 45 46 46 45 46
result:
wrong answer 6th numbers differ - expected: '45', found: '46'