QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#311973 | #7901. Basic Substring Structure | hos_lyric | RE | 0ms | 4020kb | C++14 | 9.7kb | 2024-01-23 06:30:33 | 2024-01-23 06:30:33 |
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")
////////////////////////////////////////////////////////////////////////////////
// SA-IS
// String: string, vector<int>, vector<long long>
// if sigma <= n, O(n) time, O(n) space
// if sigma > n, O(n log n) time, O(n) space
template <class String> vector<int> suffixArrayRec(const String &as) {
const int n = as.size();
if (n == 0) return {};
const auto minmaxA = minmax_element(as.begin(), as.end());
const auto minA = *minmaxA.first, maxA = *minmaxA.second;
if (static_cast<unsigned long long>(maxA) - minA >=
static_cast<unsigned long long>(n)) {
vector<int> us(n);
for (int u = 0; u < n; ++u) us[u] = u;
std::sort(us.begin(), us.end(), [&](int u, int v) -> bool {
return (as[u] < as[v]);
});
int b = 0;
vector<int> bs(n, 0);
for (int i = 1; i < n; ++i) {
if (as[us[i - 1]] != as[us[i]]) ++b;
bs[us[i]] = b;
}
return suffixArrayRec(bs);
}
const int sigma = maxA - minA + 1;
vector<int> pt(sigma + 1, 0), poss(sigma);
for (int u = 0; u < n; ++u) ++pt[as[u] - minA + 1];
for (int a = 0; a < sigma; ++a) pt[a + 1] += pt[a];
// cmp[u] := (as[u, n) < as[u + 1, n))
vector<bool> cmp(n);
cmp[n - 1] = false;
for (int u = n - 1; --u >= 0; ) {
cmp[u] = (as[u] != as[u + 1]) ? (as[u] < as[u + 1]) : cmp[u + 1];
}
// ><, nn - id (0 <= id < n)
int nn = 0;
vector<int> ids(n, 0);
int last = n;
vector<int> nxt(n, 0);
// put ><, from the tail of each bucket
vector<int> us(n, 0);
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int u = n - 1; --u >= 1; ) if (!cmp[u - 1] && cmp[u]) {
ids[u] = ++nn;
nxt[u] = last;
last = u;
us[--poss[as[u] - minA]] = u;
}
// follow > backwards, from the head of each bucket
memcpy(poss.data(), pt.data(), sigma * sizeof(int));
us[poss[as[n - 1] - minA]++] = n - 1;
for (int i = 0; i < n; ++i) {
const int u = us[i];
if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
}
// follow < backwards, from the tail of each bucket
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int i = n; --i >= 0; ) {
const int u = us[i];
if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
}
if (nn) {
int vsLen = 0;
vector<int> vs(nn);
for (const int u : us) if (ids[u]) vs[vsLen++] = u;
int b = 0;
vector<int> bs(nn, 0);
for (int j = 1; j < nn; ++j) {
// as[v1, w1] (< or =) as[v0, w0]
int v1 = vs[j - 1], v0 = vs[j];
const int w1 = nxt[v1], w0 = nxt[v0];
if (w1 - v1 != w0 - v0) {
++b;
} else {
for (; ; ++v1, ++v0) {
if (v1 == n) { ++b; break; }
if (as[v1] != as[v0]) { ++b; break; }
if (v1 == w1) break;
}
}
bs[nn - ids[vs[j]]] = b;
}
for (int u = 0; u < n; ++u) if (ids[u]) vs[nn - ids[u]] = u;
const auto sub = suffixArrayRec(bs);
// put ><, from the tail of each bucket
memset(us.data(), 0, n * sizeof(int));
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int j = nn; --j >= 0; ) {
const int u = vs[sub[j]];
us[--poss[as[u] - minA]] = u;
}
// follow > backwards, from the head of each bucket
memcpy(poss.data(), pt.data(), sigma * sizeof(int));
us[poss[as[n - 1] - minA]++] = n - 1;
for (int i = 0; i < n; ++i) {
const int u = us[i];
if (u && !cmp[u - 1]) us[poss[as[u - 1] - minA]++] = u - 1;
}
// follow < backwards, from the tail of each bucket
memcpy(poss.data(), pt.data() + 1, sigma * sizeof(int));
for (int i = n; --i >= 0; ) {
const int u = us[i];
if (u && cmp[u - 1]) us[--poss[as[u - 1] - minA]] = u - 1;
}
}
return us;
}
// us[i]: i-th suffix
// su[u]: index of as[u, n)
// hs[i]: longest common prefix of as[us[i - 1], n) and as[us[i], n)
struct SuffixArray {
int n;
bool rmq;
vector<int> us, su, hs, bsr;
SuffixArray() : n(0), rmq(false) {}
SuffixArray(const string &as, bool rmq_) : rmq(rmq_) { build(as); }
SuffixArray(const vector<int> &as, bool rmq_) : rmq(rmq_) { build(as); }
SuffixArray(const vector<long long> &as, bool rmq_) : rmq(rmq_) { build(as); }
template <class String> void build(const String &as) {
n = as.size();
us = suffixArrayRec(as);
su.resize(n + 1);
for (int i = 0; i < n; ++i) su[us[i]] = i;
su[n] = -1;
hs.assign(n, 0);
for (int h = 0, u = 0; u < n; ++u) if (su[u]) {
for (int v = us[su[u] - 1]; v + h < n && as[v + h] == as[u + h]; ++h) {}
hs[su[u]] = h;
if (h) --h;
}
if (rmq) {
const int logN = n ? (31 - __builtin_clz(n)) : 0;
hs.resize((logN + 1) * n - (1 << logN) + 1);
for (int e = 0; e < logN; ++e) {
int *hes = hs.data() + e * n;
for (int i = 0; i <= n - (1 << (e + 1)); ++i) {
hes[n + i] = min(hes[i], hes[i + (1 << e)]);
}
}
bsr.resize(n + 1);
bsr[0] = -1;
for (int i = 1; i <= n; ++i) bsr[i] = bsr[i >> 1] + 1;
}
}
// Returns longest common prefix of as[u, n) and as[v, n).
// 0 <= u, v <= n
// Assumes rmq.
inline int lcp(int u, int v) const {
if (u == v) return n - u;
int i = su[u], j = su[v];
if (i > j) swap(i, j);
const int e = bsr[j - i];
return min(hs[e * n + i + 1], hs[e * n + j + 1 - (1 << e)]);
}
};
////////////////////////////////////////////////////////////////////////////////
int N;
vector<int> S;
int main() {
for (int numCases; ~scanf("%d", &numCases); ) { for (int caseId = 1; caseId <= numCases; ++caseId) {
scanf("%d", &N);
S.resize(N);
for (int i = 0; i < N; ++i) {
scanf("%d", &S[i]);
}
const SuffixArray sa(S, true);
vector<int> zs(N);
Int base = 0;
for (int i = 0; i < N; ++i) {
base += zs[i] = sa.lcp(0, i);
}
// cerr<<"zs = "<<zs<<endl;
// change S[j]
vector<vector<Int>> addss(N + 1), remss(N + 1);
// j -> (new character, score)
vector<vector<pair<int, int>>> gainss(N);
for (int i = 1; i < N; ++i) {
// 0 <= j < zs[i]: zs[i] -> j
addss[0 ].push_back(0 - zs[i]);
remss[zs[i]].push_back(0 - zs[i]);
// max{zs[i],i} <= j < i+zs[i]: zs[i] -> j-i
addss[max(zs[i], i)].push_back(-i - zs[i]);
remss[i + zs[i] ].push_back(-i - zs[i]);
if (i + zs[i] < N) {
assert(S[zs[i]] != S[i + zs[i]]);
// change S[zs[i]] to S[i + zs[i]]
int l = zs[i] + 1 + sa.lcp(zs[i] + 1, i + zs[i] + 1);
if (zs[i] < i) {
gainss[zs[i]].emplace_back(S[i + zs[i]], l - zs[i]);
}
// change S[i + zs[i]] to S[zs[i]]
if (l >= i + zs[i]) {
l = i + zs[i];
if (S[zs[i]] == S[i + i + zs[i]]) {
l += 1 + sa.lcp(i + zs[i] + 1, i + i + zs[i] + 1);
}
}
// if(i==1)cerr<<"HELP "<<i+zs[i]<<" "<<S[zs[i]]<<": "<<zs[i]<<" -> "<<l<<endl;
// if(zs[i]==3||i+zs[i]==3)cerr<<"HELP "<<i+zs[i]<<" "<<S[zs[i]]<<": "<<zs[i]<<" -> "<<l<<endl;
gainss[i + zs[i]].emplace_back(S[zs[i]], l - zs[i]);
}
}
vector<Int> ans(N, 0);
Int sum0 = 0, sum1 = 0;
for (int j = 0; j < N; ++j) {
for (const Int x : addss[j]) { sum0 += x; sum1 += 1; }
for (const Int x : remss[j]) { sum0 -= x; sum1 -= 1; }
const Int baseHere = base + sum0 + sum1 * j;
ans[j] = baseHere;
auto &gains = gainss[j];
sort(gains.begin(), gains.end());
// cerr<<j<<": sum0 = "<<sum0<<", sum1 = "<<sum1<<", baseHere = "<<baseHere<<", gains = "<<gains<<endl;
for (int k = 0, l = 0; k < (int)gains.size(); k = l) {
Int score = baseHere;
for (; l < (int)gains.size() && gains[k].first == gains[l].first; ++l) {
score += gains[l].second;
}
// cerr<<" "<<S[j]<<" -> "<<gains[k].first<<": "<<score<<endl;
chmax(ans[j], score);
}
}
Int key = 0;
for (int j = 0; j < N; ++j) {
key += (ans[j] ^ (j + 1));
}
printf("%lld\n", key);
#ifdef LOCAL
vector<Int>brt(N,0);
for(int j=0;j<N;++j){
for(int x=1;x<=N;++x)if(S[j]!=x){
auto ss=S;ss[j]=x;
const SuffixArray saa(ss,true);
Int score=0;
for(int i=0;i<N;++i)score+=saa.lcp(0,i);
// if(j==3)cerr<<"brt "<<j<<": "<<S[j]<<" -> "<<x<<": "<<score<<endl;
chmax(brt[j],score);
}
}
cerr<<"brt = "<<brt<<endl;
cerr<<"ans = "<<ans<<endl;
assert(brt==ans);
#endif
}
#ifndef LOCAL
break;
#endif
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4020kb
input:
2 4 2 1 1 2 12 1 1 4 5 1 4 1 9 1 9 8 10
output:
15 217
result:
ok 2 lines
Test #2:
score: -100
Runtime Error
input:
10000 8 2 1 2 1 1 1 2 2 9 2 2 1 2 1 2 1 2 1 15 2 1 2 1 1 1 1 2 2 1 2 1 2 2 1 2 1 1 10 2 1 1 1 2 2 1 1 2 2 3 2 1 2 11 1 2 2 1 1 2 1 2 2 1 1 14 2 1 1 1 1 2 1 1 1 2 2 1 2 1 12 2 2 2 1 2 2 2 1 1 2 1 2 4 2 1 1 2 8 1 2 2 2 1 2 1 1 8 1 1 2 1 2 1 1 1 6 2 1 1 1 2 2 14 2 2 1 1 1 1 2 2 2 1 2 2 1 1 10 1 2 2 1 1...