QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#599220#7876. Cyclic SubstringsHirroWA 984ms1038512kbC++203.5kb2024-09-29 02:27:142024-09-29 02:27:14

Judging History

你现在查看的是最新测评结果

  • [2024-09-29 02:27:14]
  • 评测
  • 测评结果:WA
  • 用时:984ms
  • 内存:1038512kb
  • [2024-09-29 02:27:14]
  • 提交

answer

#include<bits/stdc++.h>
#define pll pair<int,int>
using namespace std;
const int N = 3e6 + 5;
const int LOG = 23;
int tot = 2, root[2] = { 2,1 };
struct node {
    int val, tg, son[10];
}tr[N * 2];
int fa[N * 2][LOG+1], pos[N * 4], r[N * 4], dep[N * 2], 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];
        tr[x].val += val;
        dep[x] = dep[f] + 2;
    }
    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] = 1;
    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];
        if (mid + r[mid] > i) {
            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++;
            }
        }
        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]], 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: 100
Accepted
time: 10ms
memory: 30088kb

input:

5
01010

output:

39

result:

ok 1 number(s): "39"

Test #2:

score: 0
Accepted
time: 10ms
memory: 31084kb

input:

8
66776677

output:

192

result:

ok 1 number(s): "192"

Test #3:

score: 0
Accepted
time: 12ms
memory: 30360kb

input:

1
1

output:

1

result:

ok 1 number(s): "1"

Test #4:

score: 0
Accepted
time: 12ms
memory: 29976kb

input:

2
22

output:

12

result:

ok 1 number(s): "12"

Test #5:

score: 0
Accepted
time: 12ms
memory: 29564kb

input:

2
21

output:

2

result:

ok 1 number(s): "2"

Test #6:

score: 0
Accepted
time: 8ms
memory: 30480kb

input:

3
233

output:

10

result:

ok 1 number(s): "10"

Test #7:

score: 0
Accepted
time: 7ms
memory: 29496kb

input:

3
666

output:

54

result:

ok 1 number(s): "54"

Test #8:

score: 0
Accepted
time: 291ms
memory: 365616kb

input:

1000000
3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333...

output:

496166704

result:

ok 1 number(s): "496166704"

Test #9:

score: 0
Accepted
time: 984ms
memory: 1038512kb

input:

3000000
2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222...

output:

890701718

result:

ok 1 number(s): "890701718"

Test #10:

score: -100
Wrong Answer
time: 488ms
memory: 565436kb

input:

3000000
9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999...

output:

122581918

result:

wrong answer 1st numbers differ - expected: '224009870', found: '122581918'