QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#673618 | #9482. Count Pseudo-Palindromes | hos_lyric | RE | 1ms | 4048kb | C++14 | 7.6kb | 2024-10-25 02:20:24 | 2024-10-25 02:20:24 |
Judging History
answer
#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
using namespace std;
using Int = long long;
template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")
////////////////////////////////////////////////////////////////////////////////
// 2^61 - 1 = 2'305'843'009'213'693'951
struct ModLong61 {
static constexpr unsigned long long M = (1ULL << 61) - 1;
unsigned long long x;
constexpr ModLong61() : x(0ULL) {}
constexpr ModLong61(unsigned x_) : x(x_) {}
constexpr ModLong61(unsigned long long x_) : x(x_ % M) {}
constexpr ModLong61(int x_) : x((x_ < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
constexpr ModLong61(long long x_) : x(((x_ %= static_cast<long long>(M)) < 0) ? (x_ + static_cast<long long>(M)) : x_) {}
ModLong61 &operator+=(const ModLong61 &a) { x = ((x += a.x) >= M) ? (x - M) : x; return *this; }
ModLong61 &operator-=(const ModLong61 &a) { x = ((x -= a.x) >= M) ? (x + M) : x; return *this; }
ModLong61 &operator*=(const ModLong61 &a) {
const unsigned __int128 y = static_cast<unsigned __int128>(x) * a.x;
x = (y >> 61) + (y & M);
x = (x >= M) ? (x - M) : x;
return *this;
}
ModLong61 &operator/=(const ModLong61 &a) { return (*this *= a.inv()); }
ModLong61 pow(long long e) const {
if (e < 0) return inv().pow(-e);
ModLong61 a = *this, b = 1ULL; for (; e; e >>= 1) { if (e & 1) b *= a; a *= a; } return b;
}
ModLong61 inv() const {
unsigned long long a = M, b = x; long long y = 0, z = 1;
for (; b; ) { const unsigned long long q = a / b; const unsigned long long c = a - q * b; a = b; b = c; const long long w = y - static_cast<long long>(q) * z; y = z; z = w; }
assert(a == 1ULL); return ModLong61(y);
}
ModLong61 operator+() const { return *this; }
ModLong61 operator-() const { ModLong61 a; a.x = x ? (M - x) : 0ULL; return a; }
ModLong61 operator+(const ModLong61 &a) const { return (ModLong61(*this) += a); }
ModLong61 operator-(const ModLong61 &a) const { return (ModLong61(*this) -= a); }
ModLong61 operator*(const ModLong61 &a) const { return (ModLong61(*this) *= a); }
ModLong61 operator/(const ModLong61 &a) const { return (ModLong61(*this) /= a); }
template <class T> friend ModLong61 operator+(T a, const ModLong61 &b) { return (ModLong61(a) += b); }
template <class T> friend ModLong61 operator-(T a, const ModLong61 &b) { return (ModLong61(a) -= b); }
template <class T> friend ModLong61 operator*(T a, const ModLong61 &b) { return (ModLong61(a) *= b); }
template <class T> friend ModLong61 operator/(T a, const ModLong61 &b) { return (ModLong61(a) /= b); }
explicit operator bool() const { return x; }
bool operator==(const ModLong61 &a) const { return (x == a.x); }
bool operator!=(const ModLong61 &a) const { return (x != a.x); }
friend std::ostream &operator<<(std::ostream &os, const ModLong61 &a) { return os << a.x; }
};
////////////////////////////////////////////////////////////////////////////////
#include <chrono>
#ifdef LOCAL
mt19937_64 rng(58);
#else
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#endif
////////////////////////////////////////////////////////////////////////////////
struct Set {
// max{ceil(log_64(n)), 1}
int log64N, n;
vector<unsigned long long> a[6];
explicit Set(int n_ = 0) : n(n_) {
assert(n >= 0);
int m = n ? n : 1;
for (int d = 0; ; ++d) {
m = (m + 63) >> 6;
a[d].assign(m, 0);
if (m == 1) {
log64N = d + 1;
break;
}
}
}
bool empty() const {
return !a[log64N - 1][0];
}
bool contains(int x) const {
return (a[0][x >> 6] >> (x & 63)) & 1;
}
void insert(int x) {
for (int d = 0; d < log64N; ++d) {
const int q = x >> 6, r = x & 63;
a[d][q] |= 1ULL << r;
x = q;
}
}
void erase(int x) {
for (int d = 0; d < log64N; ++d) {
const int q = x >> 6, r = x & 63;
if ((a[d][q] &= ~(1ULL << r))) break;
x = q;
}
}
// max s.t. <= x (or -1)
int prev(int x) const {
if (x > n - 1) x = n - 1;
for (int d = 0; d <= log64N; ++d) {
if (x < 0) break;
const int q = x >> 6, r = x & 63;
const unsigned long long lower = a[d][q] << (63 - r);
if (lower) {
x -= __builtin_clzll(lower);
for (int e = d; --e >= 0; ) x = x << 6 | (63 - __builtin_clzll(a[e][x]));
return x;
}
x = q - 1;
}
return -1;
}
// min s.t. >= x (or n)
int next(int x) const {
if (x < 0) x = 0;
for (int d = 0; d < log64N; ++d) {
const int q = x >> 6, r = x & 63;
if (static_cast<unsigned>(q) >= a[d].size()) break;
const unsigned long long upper = a[d][q] >> r;
if (upper) {
x += __builtin_ctzll(upper);
for (int e = d; --e >= 0; ) x = x << 6 | __builtin_ctzll(a[e][x]);
return x;
}
x = q + 1;
}
return n;
}
};
/*
for each a, count [l, r) s.t. I[0][a] < l <= I[1][a] < r
fix l
[l, r) must contain a with min I[1][a] s.t. I[0][a] < l <= I[1][a]
==> a appears once in [l, r)
==> others should appear even times
*/
int N;
vector<int> A;
int main() {
for (; ~scanf("%d", &N); ) {
A.resize(2*N);
for (int i = 0; i < 2*N; ++i) {
scanf("%d", &A[i]);
--A[i];
}
vector<unsigned long long> rnds(N);
for (int a = 0; a < N; ++a) rnds[a] = rng();
vector<Int> ans(2*N, 0);
for (int dir = 0; dir < 2; ++dir) {
vector<vector<int>> I(N);
for (int i = 0; i < 2*N; ++i) I[A[i]].push_back(i);
vector<vector<int>> lss(2*N);
{
Set is(N);
for (int i = 0; i < 2*N; ++i) {
if (I[A[i]][0] == i) {
is.insert(I[A[i]][1]);
} else {
is.erase(i);
}
if (!is.empty()) lss[is.next(0)].push_back(i + 1);
}
}
vector<unsigned long long> pre(2*N + 1, 0);
for (int i = 0; i < 2*N; ++i) pre[i + 1] = pre[i] ^ rnds[A[i]];
map<unsigned long long, int> freq;
for (int i = 2*N; --i >= 0; ) {
++freq[pre[i + 1]];
for (const int l : lss[i]) {
auto it = freq.find(pre[l] ^ rnds[A[i]]);
if (it != freq.end()) {
// cerr<<"A = "<<A<<", i = "<<i<<", l = "<<l<<", *it = "<<*it<<endl;
ans[i] += it->second;
}
}
}
reverse(A.begin(), A.end());
reverse(ans.begin(), ans.end());
}
for (int i = 0; i < 2*N; ++i) {
if (i) printf(" ");
printf("%lld", ans[i]);
}
puts("");
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3780kb
input:
2 1 1 2 2
output:
1 2 2 1
result:
ok 4 tokens
Test #2:
score: 0
Accepted
time: 1ms
memory: 3804kb
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: 3764kb
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: 4048kb
input:
1 1 1
output:
1 1
result:
ok 2 tokens
Test #5:
score: -100
Runtime Error
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...