QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#676738 | #9482. Count Pseudo-Palindromes | ucup-team4435 | TL | 0ms | 3820kb | C++20 | 3.3kb | 2024-10-25 23:51:15 | 2024-10-25 23:51:15 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define len(a) int((a).size())
#ifdef LOCAL
#include "debug.h"
#else
#define dbg(...)
#define dprint(...)
#define debug if constexpr (false)
#define draw_tree(...)
#define draw_array(...)
#endif // LOCAL
template<typename Fun>
struct y_combinator {
const Fun fun;
explicit y_combinator(const Fun&& fun) : fun(std::forward<const Fun>(fun)) {}
template<typename... Args>
auto operator()(Args&&... args) const {
return fun(std::ref(*this), std::forward<Args>(args)...);
}
};
vector<int> solve(int n, vector<int> a, int rot) {
vector<int> last(2 * n, -1), mate(2 * n, -1);
for (int i = 0; i < 2 * n; i++) {
if (last[a[i]] == -1) {
last[a[i]] = i;
} else {
mate[last[a[i]]] = i;
mate[i] = last[a[i]];
}
}
vector<int> ans(2 * n);
y_combinator([&](auto dfs, int l, int r) -> void {
if (r - l <= 1) {
return;
}
int m = (l + r + rot) / 2;
dfs(l, m);
dfs(m, r);
vector<int> f(r - m + 1);
set<int> lims;
for (int i = m; i < r; i++) {
if (i < mate[i]) {
f[i - m]++;
f[min(r, mate[i]) - m]--;
} else if (mate[i] < m) {
lims.insert(i);
}
}
for (int i = 1; i < len(f); i++) {
f[i] += f[i - 1];
}
vector<int> psum(r - m + 1);
for (int i = 0; i < r - m; i++) {
psum[i + 1] = psum[i] + (f[i] == 0);
}
auto query = [&](int left, int right) {
left = max(left, m);
right = min(right, r);
if (right <= left) {
return 0;
}
return psum[right - m] - psum[left - m];
};
set<pair<int, int>> open;
for (int i = m - 1; i >= l; i--) {
if (i < mate[i]) {
if (mate[i] < m) {
open.erase({r, mate[i]});
} else {
open.emplace(min(r, mate[i]), i);
lims.erase(mate[i]);
}
} else if (mate[i] < i) {
open.emplace(r, i);
}
if (open.empty()) {
continue;
}
auto [to, index] = *open.rbegin();
int from = m;
if (len(open) > 1) {
from = next(open.rbegin())->first;
}
if (!lims.empty()) {
to = min(to, *lims.begin());
}
ans[index] += query(from, to);
}
})(0, 2 * n);
return ans;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<int> a(2 * n);
for (auto &x : a) {
cin >> x;
x--;
}
auto ans = solve(n, a, 0);
dbg(ans);
reverse(all(a));
auto ans2 = solve(n, a, 1);
for (int i = 0; i < len(ans); i++) {
ans[i] += ans2.rbegin()[i];
}
for (auto x : ans) {
cout << x + 1 << ' ';
}
cout << '\n';
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3820kb
input:
2 1 1 2 2
output:
1 2 2 1
result:
ok 4 tokens
Test #2:
score: 0
Accepted
time: 0ms
memory: 3556kb
input:
3 2 1 2 3 1 3
output:
1 2 2 2 2 1
result:
ok 6 tokens
Test #3:
score: 0
Accepted
time: 0ms
memory: 3592kb
input:
4 1 2 4 3 4 1 3 2
output:
1 2 1 2 1 3 1 1
result:
ok 8 tokens
Test #4:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
1 1 1
output:
1 1
result:
ok 2 tokens
Test #5:
score: -100
Time Limit Exceeded
input:
500000 233733 151347 468353 495903 234861 297169 312993 2734 287872 359375 79017 285205 219439 37290 409190 194953 306816 472906 123794 121028 66509 62235 385879 300188 485875 72413 167304 333428 33910 220100 151575 22575 206653 82054 111518 34032 48702 198940 6262 222529 170641 1735 38943 235003 11...