QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#569450 | #9329. Good Subsegments | ucup-team004# | WA | 39ms | 19864kb | C++20 | 2.8kb | 2024-09-16 23:02:39 | 2024-09-16 23:02:39 |
Judging History
answer
#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
template <typename T>
struct Fenwick {
int n;
std::vector<T> a;
Fenwick(int n_ = 0) {
init(n_);
}
void init(int n_) {
n = n_;
a.assign(n, T{});
}
void add(int x, const T &v) {
for (int i = x + 1; i <= n; i += i & -i) {
a[i - 1] = a[i - 1] + v;
}
}
T sum(int x) {
T ans{};
for (int i = x; i > 0; i -= i & -i) {
ans = ans + a[i - 1];
}
return ans;
}
T rangeSum(int l, int r) {
return sum(r) - sum(l);
}
int select(const T &k) {
int x = 0;
T cur{};
for (int i = 1 << std::__lg(n); i; i /= 2) {
if (x + i <= n && cur + a[x + i - 1] <= k) {
x += i;
cur = cur + a[x - 1];
}
}
return x;
}
};
void solve() {
int n;
std::cin >> n;
std::vector<int> a(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i];
a[i]--;
}
std::vector<int> l(n), r(n);
for (int i = 0; i < n; i++) {
l[i] = (i > 0 && a[i] == a[i - 1] ? l[i - 1] : 0) + 1;
}
for (int i = n - 1; i >= 0; i--) {
r[i] = (i + 1 < n && a[i] == a[i + 1] ? r[i + 1] : 0) + 1;
}
std::vector<std::vector<std::array<int, 3>>> e(n);
for (int i = 0; i < n; i++) {
e[a[i]].push_back({l[i], i, 0});
e[a[i]].push_back({r[i], i, 1});
}
std::vector<int> ans(n / 2 + 1);
Fenwick<int> fenl(n);
Fenwick<int> fenr(n);
for (int x = 0; x < n; x++) {
std::sort(e[x].begin(), e[x].end(), std::greater());
for (auto [v, i, t] : e[x]) {
if (t == 0) {
int res = fenr.rangeSum(i + 1, n);
if (res > 0) {
ans[v] += res;
}
fenl.add(i, 1);
} else {
int res = fenl.sum(i);
if (res > 0) {
ans[v] += res;
}
fenr.add(i, 1);
}
}
for (auto [v, i, t] : e[x]) {
if (t == 0) {
fenl.add(i, -1);
} else {
fenr.add(i, -1);
}
}
}
for (int i = n / 2 - 1; i >= 1; i--) {
ans[i] += ans[i + 1];
}
for (int i = 1; i <= n / 2; i++) {
std::cout << ans[i] << " \n"[i == n / 2];
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int t;
std::cin >> t;
while (t--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3612kb
input:
4 10 1 2 2 2 2 2 3 2 2 2 6 1 1 1 2 1 1 9 2 2 1 1 1 2 2 1 1 10 3 2 3 2 4 2 10 10 10 10
output:
28 11 3 0 0 10 2 0 16 3 0 0 10 1 0 0 0
result:
ok 17 numbers
Test #2:
score: 0
Accepted
time: 0ms
memory: 3584kb
input:
4 3 3 3 3 8 3 3 3 3 2 2 2 2 3 3 3 3 5 3 3 3 3 3
output:
3 12 2 0 0 3 10 3
result:
ok 8 numbers
Test #3:
score: 0
Accepted
time: 14ms
memory: 3592kb
input:
15430 4 4 4 2 2 9 1 1 1 1 1 1 1 1 2 10 2 2 2 2 2 2 4 4 4 3 7 4 4 4 4 4 4 4 7 6 6 6 6 5 5 5 6 6 6 6 6 6 6 8 8 8 8 8 5 5 3 3 7 7 7 7 7 6 6 6 10 4 4 4 4 2 2 2 2 2 2 4 1 2 2 2 6 1 1 1 1 1 1 10 6 6 6 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 7 1 3 3 3 3 3 3 10 4 4 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 10 10 10 10 10 1...
output:
2 0 28 15 6 1 18 6 1 0 0 21 10 3 9 1 0 15 6 1 8 1 0 0 9 1 0 21 7 1 0 0 3 0 15 6 1 24 10 3 0 0 36 21 10 3 15 6 1 29 15 6 1 0 21 10 3 16 4 0 0 0 13 2 0 0 0 3 0 13 2 0 0 0 7 1 0 6 0 0 3 6 1 10 3 0 36 21 10 3 10 3 0 10 3 10 3 3 45 28 15 6 1 6 1 11 3 0 6 1 7 0 0 6 1 15 6 1 1 10 1 0 0 4 0 15 6 1 3 18 6 1 ...
result:
ok 46132 numbers
Test #4:
score: 0
Accepted
time: 14ms
memory: 3652kb
input:
1785 33 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 63 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 73 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 2...
output:
528 465 406 351 300 253 210 171 136 105 78 55 36 21 10 3 1953 1830 1711 1596 1485 1378 1275 1176 1081 990 903 820 741 666 595 528 465 406 351 300 253 210 171 136 105 78 55 36 21 10 3 2628 2485 2346 2211 2080 1953 1830 1711 1596 1485 1378 1275 1176 1081 990 903 820 741 666 595 528 465 406 351 300 253...
result:
ok 49554 numbers
Test #5:
score: 0
Accepted
time: 13ms
memory: 3940kb
input:
1822 96 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 61 21 9...
output:
4560 4371 4186 4005 3828 3655 3486 3321 3160 3003 2850 2701 2556 2415 2278 2145 2016 1891 1770 1653 1540 1431 1326 1225 1128 1035 946 861 780 703 630 561 496 435 378 325 276 231 190 153 120 91 66 45 28 15 6 1 210 171 136 105 78 55 36 21 10 3 2178 1996 1822 1656 1498 1348 1206 1072 946 828 718 616 52...
result:
ok 49546 numbers
Test #6:
score: 0
Accepted
time: 10ms
memory: 9672kb
input:
2 89720 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 18749 1874...
output:
1848963231 1848783803 1848604391 1848424995 1848245615 1848066251 1847886903 1847707571 1847528255 1847348955 1847169671 1846990403 1846811151 1846631915 1846452695 1846273491 1846094303 1845915131 1845735975 1845556835 1845377711 1845198603 1845019511 1844840435 1844661375 1844482331 1844303303 184...
result:
ok 50000 numbers
Test #7:
score: 0
Accepted
time: 17ms
memory: 8640kb
input:
2 73829 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 44095 4409...
output:
535244835 535097201 534949599 534802029 534654491 534506985 534359511 534212069 534064659 533917281 533769935 533622621 533475339 533328089 533180871 533033685 532886531 532739409 532592319 532445261 532298235 532151241 532004279 531857349 531710451 531563585 531416751 531269949 531123179 530976441 ...
result:
ok 49999 numbers
Test #8:
score: -100
Wrong Answer
time: 39ms
memory: 19864kb
input:
1 200000 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 128472 1284...
output:
1350246093 1349714087 1349182103 1348650141 1348118201 1347586283 1347054387 1346522513 1345990661 1345458831 1344927023 1344395237 1343863473 1343331731 1342800011 1342268313 1341736637 1341204983 1340673351 1340141741 1339610153 1339078587 1338547043 1338015521 1337484021 1336952543 1336421087 133...
result:
wrong answer 1st numbers differ - expected: '9940180685', found: '1350246093'