QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#599349 | #7876. Cyclic Substrings | Hirro | WA | 8ms | 31896kb | C++20 | 3.3kb | 2024-09-29 03:38:04 | 2024-09-29 03:38:04 |
Judging History
answer
#include<bits/stdc++.h>
#define pll pair<int,int>
#define dep(x) tr[x].dep
using namespace std;
const int N = 3e6 + 5;
const int LOG = 23;
int tot = 2, root[2] = { 2,1 };
struct node {
int val, dep,son[10],tg ;
}tr[N * 2];
int fa[N * 2][LOG+1], pos[N * 4], r[N * 4], lg[N * 2 + 1], ci[31];
int t[N * 4], tn = 0, n;
const int P = 998244353;
string s;
int insert(int f, int ty, int val) {
int& x = tr[f].son[ty];
if (x == 0) {
x = ++tot;
fa[x][0] = f;
for (int i = 1; i <= LOG; i++)fa[x][i] = fa[fa[x][i - 1]][i - 1];
dep(x) = dep(f) + 2;
}
tr[x].val += val;
return x;
}
int ans = 0;
int up(int x, int dis) {
dis = max(dis, 0);
for (int j = lg[dis]; j >= 0; j = lg[dis]) {
dis -= ci[j];
x = fa[x][j];
}
return x;
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
freopen("1.in", "r", stdin);
cin >> n >> s;
t[tn++] = 10;
for (auto& v : s) {
t[tn++] = v - '0';
t[tn++] = 10;
}
for (auto& v : s) {
t[tn++] = v - '0';
t[tn++] = 10;
}
fa[1][0] = 1, fa[2][0] = 2;
dep(1) = -1;
for (int i = 1; i <= N * 2; i++)lg[i] = lg[i - 1] + (1 << lg[i - 1] == i);
for (int i = 0; i <= N * 2; i++)lg[i]--;
for (int i = 1; i <= LOG; i++)fa[1][i] = fa[fa[1][i - 1]][i - 1];
for (int i = 1; i <= LOG; i++)fa[2][i] = fa[fa[1][i - 1]][i - 1];
for (int i = 0; i <= 30; i++)ci[i] = 1 << i;
int mid = 0;
for (int i = 0; i <= 2 * n; i++) {
int rt = root[i % 2];
if (mid + r[mid] > i) {
int nx = 2 * mid - i;
r[i] = min(mid + r[mid] - i, r[nx]);
int L = i + r[i], R = i + r[nx];
L += L % 2, R += R % 2;
pos[i] = up(pos[nx], (R - L) / 2);
tr[rt].tg--;
tr[pos[i]].tg++;
}
if (!pos[i])pos[i] = t[i] == 10 ? pos[i] = rt : insert(rt, t[i], 1);
while (i + r[i] + 1 < tn && i - r[i] - 1 >= 0 && t[i + r[i] + 1] == t[i - r[i] - 1]) {
r[i]++;
if (t[i + r[i]] != 10)pos[i] = insert(pos[i], t[i + r[i]], 1);
}
if (i + r[i] > mid + r[mid])mid = i;
}
for (int i = 2 * n + 1; i < tn; i++) {
int rt = root[i % 2];
int nx = 2 * mid - i;
r[i] = max(r[i - 2 * n], min(mid + r[mid] - i, r[nx]));
if (min(mid + r[mid] - i, r[nx]) < r[i - 2 * n])nx = i - 2 * n;
int L = i - r[nx], R = i - r[i];
L -= L % 2, R -= R % 2;
pos[i] = up(pos[nx], (R - L) / 2);
if (i - r[i] < 2 * n) {
L = i - r[nx], R = 2 * n;
L -= L % 2, R -= R % 2;
int x = up(pos[nx], (R - L) / 2);
tr[x].tg--;
tr[pos[i]].tg++;
}
while (i + r[i] + 1 < tn && i - r[i] - 1 >= 0 && t[i + r[i] + 1] == t[i - r[i] - 1]) {
r[i]++;
if (t[i + r[i]] != 10)pos[i] = insert(pos[i], t[i + r[i]], i - r[i] < 2 * n);
}
if (i + r[i] > mid + r[mid])mid = i;
}
for (int u = tot; u >= 1; u--) {
int f = fa[u][0];
tr[f].tg = (tr[f].tg + tr[u].tg) % P;
tr[u].val = (tr[u].val + tr[u].tg) % P;
if (dep(u) <= n)ans = (ans + 1ll * tr[u].val * tr[u].val % P * dep(u) % P) % P;
}
cout << (ans % P + P) % P << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 8ms
memory: 31896kb
input:
5 01010
output:
0
result:
wrong answer 1st numbers differ - expected: '39', found: '0'