QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#646557 | #7969. 套娃 | hos_lyric | AC ✓ | 67ms | 27616kb | C++14 | 5.7kb | 2024-10-17 00:55:30 | 2024-10-17 00:55:31 |
Judging History
answer
#include <algorithm>
#include <utility>
#include <vector>
using std::max;
using std::min;
using std::pair;
using std::vector;
// rectss[a] = [((l0, l1), (r0, r1))]
// mex(as[l, r)) = a for l0 <= l < l1, r0 <= r < r1
// \sum[0<=a<=n] |rectss[a]| <= 3 n + 1
// number of calls to add
// - extend existing interval at most once for each l
// - otherwise create new open rectangle
// number of open rectangle (l0 not determined)
// - decreases by at most 1 for each l
// - remains <= n + 1
vector<vector<pair<pair<int, int>, pair<int, int>>>> allRangeMex(const vector<int> &as) {
const int n = as.size();
// maintain range max
int segN;
for (segN = 1; segN < n + 1; segN <<= 1) {}
vector<int> seg(segN << 1, n);
auto segChange = [&](int a, int i) -> void {
for (seg[a += segN] = i; a >>= 1; ) seg[a] = max(seg[a << 1], seg[a << 1 | 1]);
};
// min a s.t. seg[a] >= i
auto segFind = [&](int i) -> int {
for (int a = 1; ; a = (seg[a << 1] >= i) ? (a << 1) : (a << 1 | 1)) if (a >= segN) return a - segN;
};
// l0 = 0 for open rectangle
vector<vector<pair<pair<int, int>, pair<int, int>>>> rectss(n + 1);
auto add = [&](int a, int l1, int r0, int r1) -> void {
if (rectss[a].size() && !rectss[a].back().first.first) {
rectss[a].back().first.first = l1;
if (r1 == rectss[a].back().second.first) r1 = rectss[a].back().second.second;
}
rectss[a].emplace_back(std::make_pair(0, l1), std::make_pair(r0, r1));
};
// as[n, n)
add(0, n + 1, n, n + 1);
for (int l = n; --l >= 0; ) {
if (0 <= as[l] && as[l] < n) {
segChange(as[l], l);
if (rectss[as[l]].size() && !rectss[as[l]].back().first.first) {
// erase interval r0 <= r < r1 (previously mex = as[l])
rectss[as[l]].back().first.first = l + 1;
const int r0 = rectss[as[l]].back().second.first;
const int r1 = rectss[as[l]].back().second.second;
for (int r = r0; r < r1; ) {
const int a = segFind(r);
const int rr = min(seg[segN + a] + 1, r1);
// adding interval, or extending existing interval at most once
add(a, l + 1, r, rr);
r = rr;
}
}
}
// as[l, l)
add(0, l + 1, l, l + 1);
}
return rectss;
}
////////////////////////////////////////////////////////////////////////////////
#include <assert.h>
#include <stdio.h>
#include <iostream>
#include <random>
#include <set>
using std::cerr;
using std::endl;
using std::mt19937_64;
using std::set;
void unittest() {
// 012013201
// r
// ^4444443200
// |444441110
// |44440000
// |4442000
// |333200
// |33110
// |3000
// |200
// |10
// |0
// --------->l
assert(allRangeMex({0, 1, 2, 0, 1, 3, 2, 0, 1}) ==
(vector<vector<pair<pair<int, int>, pair<int, int>>>>{
/*0*/ {{{9, 10}, {9, 10}}, {{8, 9}, {8, 10}}, {{7, 8}, {7, 8}}, {{6, 7}, {6, 8}}, {{5, 6}, {5, 8}}, {{4, 5}, {4, 8}}, {{3, 4}, {3, 4}}, {{2, 3}, {2, 4}}, {{1, 2}, {1, 4}}, {{0, 1}, {0, 1}}},
/*1*/ {{{5, 8}, {8, 9}}, {{2, 4}, {4, 5}}, {{0, 1}, {1, 2}}},
/*2*/ {{{7, 8}, {9, 10}}, {{3, 4}, {5, 7}}, {{0, 1}, {2, 3}}},
/*3*/ {{{6, 7}, {9, 10}}, {{2, 3}, {5, 6}}, {{1, 2}, {4, 6}}, {{0, 1}, {3, 6}}},
/*4*/ {{{5, 6}, {9, 10}}, {{4, 5}, {8, 10}}, {{3, 4}, {7, 10}}, {{0, 3}, {6, 10}}},
/*5*/ {},
/*6*/ {},
/*7*/ {},
/*8*/ {},
/*9*/ {},
}));
auto test = [&](const vector<int> &as) -> void {
const int n = as.size();
vector<vector<int>> mex(n + 1, vector<int>(n + 1, -1));
for (int l = 0; l <= n; ++l) {
set<int> app;
for (int r = l; r <= n; ++r) {
for (int &a = mex[l][r] = 0; app.count(a); ++a) {}
if (r < n) app.insert(as[r]);
}
}
const auto rectss = allRangeMex(as);
assert(rectss.size() == static_cast<size_t>(n + 1));
int numRects = 0;
for (int a = 0; a <= n; ++a) for (const auto &rect : rectss[a]) {
++numRects;
for (int l = rect.first.first; l < rect.first.second; ++l) {
for (int r = rect.second.first; r < rect.second.second; ++r) {
assert(l <= r);
assert(mex[l][r] == a);
}
}
}
assert(numRects <= 3 * n + 1);
};
for (int n = 0; n <= 8; ++n) {
// [0, 8)^n
for (int p = 0; p < 1 << (3 * n); ++p) {
vector<int> as(n);
for (int i = 0; i < n; ++i) as[i] = p >> (3 * i) & 7;
test(as);
}
cerr << "[unittest] PASSED n = " << n << endl;
}
mt19937_64 rng;
for (int n = 9; n <= 100; ++n) {
vector<int> as(n);
for (int i = 0; i < n; ++i) as[i] = rng() % (n + 1);
test(as);
}
}
// https://qoj.ac/contest/1454/problem/7969
void qoj7969() {
int N;
for (; ~scanf("%d", &N); ) {
vector<int> A(N);
for (int i = 0; i < N; ++i) {
scanf("%d", &A[i]);
}
const auto rectss = allRangeMex(A);
vector<vector<int>> addss(N + 1), remss(N + 1);
for (int a = 0; a <= N; ++a) for (const auto &rect : rectss[a]) {
addss[rect.second.first - (rect.first.second - 1)].push_back(a);
remss[(rect.second.second - 1) - rect.first.first].push_back(a);
}
vector<int> freq(N + 1, 0);
set<int> ex;
for (int a = 0; a <= N + 1; ++a) ex.insert(a);
vector<int> ans(N + 1);
for (int k = 0; k <= N; ++k) {
for (const int a : addss[k]) if (!freq[a]++) ex.erase(a);
ans[k] = *ex.begin();
for (const int a : remss[k]) if (!--freq[a]) ex.insert(a);
}
for (int k = 1; k <= N; ++k) {
if (k > 1) printf(" ");
printf("%d", ans[k]);
}
puts("");
}
}
int main() {
// unittest(); cerr << "PASSED unittest" << endl;
qoj7969();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 4088kb
input:
6 0 0 0 1 2 3
output:
2 3 4 0 0 0
result:
ok single line: '2 3 4 0 0 0'
Test #2:
score: 0
Accepted
time: 0ms
memory: 4108kb
input:
100 0 10 21 9 4 1 14 0 8 1 4 6 10 0 0 4 18 0 12 5 2 5 15 1 6 2 0 4 14 5 1 2 23 1 8 1 24 8 8 9 5 2 12 2 3 7 6 11 12 12 6 12 4 4 5 0 7 4 9 12 1 7 4 7 12 2 10 2 4 8 7 1 4 0 13 9 13 2 2 3 9 14 7 9 15 10 10 6 2 12 3 11 6 3 4 7 9 6 11 1
output:
2 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 2 2 2 2 2 2 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
result:
ok single line: '2 2 3 4 4 4 4 4 4 4 4 4 4 4 4 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
Test #3:
score: 0
Accepted
time: 20ms
memory: 18868kb
input:
100000 127 145 474 139 36 135 75 670 76 433 206 214 283 56 214 440 147 280 244 190 181 565 31 550 261 93 526 404 125 390 17 552 5 364 53 337 52 506 277 279 15 248 46 61 826 69 166 297 171 289 150 175 111 151 317 342 166 13 199 152 308 19 156 347 205 166 45 115 177 235 422 425 109 4 658 232 347 370 4...
output:
2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...
result:
ok single line: '2 2 3 3 3 3 3 3 3 3 4 4 4 4 4 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
Test #4:
score: 0
Accepted
time: 32ms
memory: 18984kb
input:
100000 360 34 204 156 183 404 21 3 20 122 170 193 347 144 51 464 94 265 190 88 284 437 538 392 661 397 839 208 83 191 42 16 194 515 374 53 617 502 307 504 348 175 219 63 2 130 289 223 135 440 284 189 104 142 87 117 316 218 301 14 87 405 293 489 763 197 678 196 173 96 257 17 190 525 243 161 220 178 8...
output:
2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 ...
result:
ok single line: '2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
Test #5:
score: 0
Accepted
time: 27ms
memory: 18928kb
input:
100000 322 408 29 271 168 161 265 412 230 177 325 60 331 226 298 143 343 83 274 706 47 234 287 46 329 248 174 351 235 38 172 171 251 355 274 307 468 11 222 309 666 137 18 440 1209 7 103 354 496 336 183 602 240 316 442 253 32 486 308 18 115 125 110 65 268 502 148 793 91 759 313 269 31 63 250 90 143 6...
output:
2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 ...
result:
ok single line: '2 2 3 3 3 3 3 3 3 4 4 4 4 4 4 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
Test #6:
score: 0
Accepted
time: 41ms
memory: 23680kb
input:
100000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...
result:
ok single line: '0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
Test #7:
score: 0
Accepted
time: 67ms
memory: 27616kb
input:
100000 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100...
output:
2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...
result:
ok single line: '2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0'
Test #8:
score: 0
Accepted
time: 38ms
memory: 25424kb
input:
100000 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0...
output:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...
result:
ok single line: '2 3 4 5 6 7 8 9 10 11 12 13 14...0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'