QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#565185#8475. Palindrome Stringsucup-team1231#AC ✓1812ms277720kbC++145.0kb2024-09-15 20:32:152024-09-15 20:32:17

Judging History

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

  • [2024-09-15 20:32:17]
  • 评测
  • 测评结果:AC
  • 用时:1812ms
  • 内存:277720kb
  • [2024-09-15 20:32:15]
  • 提交

answer

#pragma GCC optimize("Ofast","unroll-all-loops","fast-math")
#include <bits/stdc++.h>
using namespace std;
#define SZ 4000999
int N,q;
typedef pair<int,int> pii;
#define sz(u) (int((u).size()))
#define x first
#define fi first
#define se second
#define y second
const int L=24;
typedef vector<int> vi;
string S,t[100099],rt[100099];
#define all(x) (x).begin(),(x).end()
void inp(string&s) {
    static char buf[1000099];
    scanf("%s",buf); s=buf;
}
char o[SZ]; int on=0;
#define s o
int sa[SZ],rk[SZ],su[SZ],ork[SZ*2],&n=on;
bool diff(int a,int b,int q) {
    return ork[a]!=ork[b]||ork[a+q]!=ork[b+q];
}
#define U 25
int mi[U][2000999],*h=mi[0],lg2[SZ];
int qmin(int l,int r) {
    assert(r>=l&&l>=0&&r<n);
    int s=lg2[r-l+1];
    return min(mi[s][l],mi[s][r-(1<<s)+1]);
}
pii getrg(int u,int k) {
    // lcp(?,u)>=k
    u = rk[u];
    int l,r;
    {
    int L=u,R=n-1;
    while(L<R) {
        int M=(L+R+1)>>1;
        if(qmin(u,M-1)>=k) L=M;
        else R=M-1;
    }
    r=R;
    }
    {
    int L=0,R=u;
    while(L<R) {
        int M=(L+R)>>1;
        if(qmin(M,u-1)>=k) R=M;
        else L=M+1;
    }
    l=R;
    }
    return pii(l,r);
}
void build() {
    int G=max(n,200);
    for(int i=0;i<=G;++i) su[i]=0;
    for(int i=0;i<n;++i) ++su[rk[i]=s[i]];
    for(int i=1;i<=G;++i) su[i]+=su[i-1];
    for(int i=0;i<n;++i) sa[--su[rk[i]]]=i;
    for(int g=1;g<=n;g<<=1) {
    // cerr<<"++++"<<g<<"\n";
        for(int i=0;i<=G;++i) su[i]=0;
        // cerr<<"SDS\n";
        static int ts[SZ]; int tn=0;
        for(int i=n-1;i>=0;--i) {
            if(sa[i]>=g) ts[++tn]=sa[i]-g;
            ork[i]=rk[i];
        }
        // cerr<<"ASDS\n";
        for(int i=n-g;i<n;++i) ts[++tn]=i;
        for(int i=1;i<=tn;++i) ++su[rk[ts[i]]];
        for(int i=1;i<=G;++i) su[i]+=su[i-1];
        for(int i=1;i<=tn;++i)
            sa[--su[rk[ts[i]]]]=ts[i];
        int t=0;
        for(int i=0;i<n;++i) {
            if(i&&diff(sa[i-1],sa[i],g)) ++t;
            rk[sa[i]]=t;
        }
        // cerr<<"SS\n";
    }
    // cerr<<"++++\n";
    for(int i=0;i<n;++i) rk[sa[i]]=i;
    int g=0;
    for(int i=0;i<n;++i) {
        g=max(g-1,0);
        if(rk[i]==n-1) {
            h[rk[i]]=0;
            continue;
        }
        int A=i,B=sa[rk[i]+1];
        while(s[A+g]==s[B+g]) ++g;
        h[rk[i]]=g;
    }
    for(int i=1;i<=n;++i) {
        while((1<<lg2[i])<=i) ++lg2[i];
        --lg2[i];
    }
    for(int i=1;i<U;++i)
        for(int u=0;u+(1<<i)<=n;++u) {
            mi[i][u]=min(mi[i-1][u],mi[i-1][u+(1<<(i-1))]);
        }
    // for(int i=0;i<n;++i)cerr<<s[i];
    // cerr<<"\n";
    // for(int i=0;i<n;++i)cerr<<sa[i]<<",";
    // cerr<<"\n";
    // for(int i=0;i<n;++i)cerr<<rk[i]<<",";
    // cerr<<"\n";
    // for(int i=0;i<n;++i)cerr<<h[i]<<",";
    // cerr<<"\n";
}
#undef U
array<vi,2> manacher(const string&s) {
    int n=s.size();
    array<vi,2> p={vi(n+1),vi(n)};
    for(int z=0;z<2;++z)
        for(int i=0,l=0,r=0;i<n;++i) {
            int t=r-i+!z;
            if(i<r) p[z][i]=min(t,p[z][l+t]);
            int L=i-p[z][i],R=i+p[z][i]-!z;
            while(L>=1&&R+1<n&&s[L-1]==s[R+1])
                p[z][i]++, L--,R++;
            if(R>r) l=L,r=R;
        }
    return p;
}
int qz[SZ],bg[SZ];
typedef long long ll;
ll val[2][SZ];
int main() {
    scanf("%d%d",&N,&q); inp(S);
    {
    assert(N==S.size());
    auto m=manacher(S);
    for(int i=0;i<=N;++i) {
        //[i,i+U]
        int U=m[0][i];
        ++qz[i]; --qz[i+U];
    }
    for(int i=0;i<N;++i) {
        //[i,i+U]
        int U=m[1][i];
        ++qz[i]; --qz[i+U+1];
    }
    for(int i=1;i<=N;++i) qz[i]+=qz[i-1];
    }
    for(int i=1;i<=q;++i) {
        inp(t[i]); rt[i]=t[i];
        reverse(all(rt[i]));
        bg[i]=on;
        for(auto c:rt[i]) o[on++]=c;
    }
    int U=on;
    // cerr<<"A\n";
    for(auto c:S) o[on++]=c;
    // cerr<<"B\n";
    build();
    // cerr<<"B\n";
    for(int i=0;i<N;++i) {
        val[0][rk[i+U]]=(i?qz[i-1]:0)+1;
        val[1][rk[i+U]]=1;
    }
    for(int s=0;s<2;++s)
    for(int i=1;i<n;++i) val[s][i]+=val[s][i-1];
    for(int i=1;i<=q;++i) {
        ll CASE1 = 0;
        {
        pii rg=getrg(bg[i],sz(t[i]));
        CASE1+=val[0][rg.se];
        if(rg.fi) CASE1-=val[0][rg.fi-1];
        // cerr<<rg.fi<<"~"<<rg.se<<"+\n";
        }
        // cerr<<":"<<CASE1<<"\n";
        vector<int> L;
        {
        auto m=manacher(t[i]);
        int N=sz(t[i]);
        for(int i=1;i<N;++i) {
            //[i,i+U]
            int U=m[0][i];
            if(i+U-1==N-1&&U>0) L.push_back(i-1-(U-1));
        }
        for(int i=0;i<N;++i) {
            //[i,i+U]
            int U=m[1][i];
            if(i+U==N-1) L.push_back(i-U);
        }
        }
        for(auto s:L) if(s) {
            pii rg=getrg(bg[i]+sz(t[i])-s,s);
            CASE1+=val[1][rg.se];
            if(rg.fi) CASE1-=val[1][rg.fi-1];
        }
        printf("%lld\n",CASE1);
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 36840kb

input:

8 3
icpccamp
p
c
pc

output:

4
7
4

result:

ok 3 number(s): "4 7 4"

Test #2:

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

input:

10 3
bbbabbbbbb
baaaa
abb
bb

output:

10
4
31

result:

ok 3 number(s): "10 4 31"

Test #3:

score: 0
Accepted
time: 0ms
memory: 38612kb

input:

10 4
baababaaba
aaaaa
a
ab
aa

output:

8
18
17
11

result:

ok 4 number(s): "8 18 17 11"

Test #4:

score: 0
Accepted
time: 9ms
memory: 59464kb

input:

10000 1000
aabababbbaaabbbabbbbbbbbbaaabaabbbabbaababbbaaabbaabbbbbabababbbbbbbabbabaabaababbabaaababbbbaaabaababaaaaabbbbbbabbbababaababbbbabbbbabbabbbbbbabbaababbbbbbabaabababbbaabaaabbbbaaaabaaababbaaabbbbabababbaaabababababaababaabbaaabbaabababbbaabbbabaabbbbbbbbaabbaaabbbaabbaabbbabaabababbbbbb...

output:

300
18
197
198
181
21
6
5
21440
9
283
23680
8422
56163
35120
56163
35120
56163
35120
35120
56163
56163
56163
56163
56163
56163
56163
35120
35120
56163
56163
35120
56163
35120
35120
35120
35120
35120
56163
35120
35120
35120
56163
56163
35120
56163
35120
56163
35120
35120
56163
56163
56163
56163
35120...

result:

ok 1000 numbers

Test #5:

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

input:

20000 2000
bbbaaaaaababbbaababbaaaababaaabbaabbbbaaabbbaaababaaaaabbbbbbbaaaaabaaabbabababbbbbbbbaabaabbbbabaaabbbababaaabbabaabbbabbbbaaaababaabbbabbaaaababababbbbaabaababbbbbbbbabbaaaaaaaaaabbbbbaaaaaabbababbbaabbaabaaaabbabababaaabaabbabaabaaaabbbbbababaababbbbbababaaabaabbbbaaabaaababaabaaabbbab...

output:

1212
6
834
6
114
62
62
2309
89183
0
100016
90112
169223
169657
169657
169657
169223
169657
169223
169223
169657
169223
169223
169657
169657
169657
169657
169223
169223
169657
169657
169657
169223
169657
169657
169223
169657
169223
169657
169657
169657
169657
169223
169223
169657
169223
169657
169657...

result:

ok 2000 numbers

Test #6:

score: 0
Accepted
time: 19ms
memory: 64148kb

input:

30000 3000
aaaabbbaaaabaaaabaabaaaaaabaabbabaaaaaaaabbaabbaaaabbbbbababbbbbaabbabbabbbaaaababaaaabbbababaaabaabbbaabaababaabbababaaabbaaababbbaaaaaababaabbbaaabbabbbbaababbababaaaaaaaabbbaaaaabbabbbabbabbbaabbaabaaaaabbbbaabbbabbaabaabbaaaaaaaabbabbbbabaababbbabaaaabbbbbbbaabbbaabaaaaababbbbaabaabaa...

output:

90
10172
41
272796
99
20374
47982
64339
3142
198
859
167521
48
93
30100
399
93
20190
64339
3146
185181
2841
48
211
30004
859
11
46357
768
64339
210045
5076
3792
162
63
203
9
34220
137
3
399
2709
289073
4308
26270
289073
209
64339
64339
10520
38330
2755
10925
97
2748
26138
3320
3228
3154
3825
43
1725...

result:

ok 3000 numbers

Test #7:

score: 0
Accepted
time: 19ms
memory: 75448kb

input:

60000 6000
abaaabbbaaabbbbaabbbbbaaabaaaaabaaabaaaababaabbbbabbbaaabababbbababaababbbbaaabbaaaaaaabaaaabaaaaaaababaababaabbaaababaababaaabbbabbabaaaabbaabbbababbbbaabababbbbaabababaaababbbababaabbaaaaaabbbbbbbbabaaaabbbbaabaabaaabaababbaabbaabaabbbbbabbabbbbabbabbbbaaabbbbaaabbbababaabbabbbaaaabbbaa...

output:

306
296
289
289
247
6
459
373
14834
2363
443
14565
14220
4181
2130
0
0
1081983
1052603
84292
111982
111982
1081983
1081983
111982
1081983
1081983
1081983
111982
1081983
1081983
1081983
1081983
1081983
1081983
1081983
1081983
111982
111982
1081983
111982
1081983
1081983
111982
1081983
1081983
1081983...

result:

ok 6000 numbers

Test #8:

score: 0
Accepted
time: 40ms
memory: 84828kb

input:

100000 10000
babbabbbbabbbbbbaabaaabbabbabbaaabaaaaaabaabbbbbbababbaaaaabbabbbababaaaaaabbababbaababbabbbbabaabbaabbbabbbbbbabbaaabbaaaaabbabaababbabaaabaabbbabbaaabaaabababbbbbaabaabbbbabbaabbbbbaabaaabbbbbbbbabbbbbbaabaabbbbaaaabaabbbbbbbbbbaabbabaabbaabbaaaaaabbbaaabbabbbaaabababbaabaabaaaababaaa...

output:

5
32
37
318
8
481
226276
478914
50487
717062
490267
490267
490267
490267
490267
490267
490267
717062
717062
717062
490267
717062
717062
717062
717062
717062
490267
717062
490267
717062
490267
490267
490267
490267
490267
717062
490267
717062
717062
717062
490267
717062
717062
717062
717062
490267
490...

result:

ok 10000 numbers

Test #9:

score: 0
Accepted
time: 208ms
memory: 112304kb

input:

200000 20000
bbaababaabaababbaaaaabbabaabbbbbbaaaaabbbbaabbaabbbaaaaaaabbabbbababbbaabababbbbabbabbbabbaabaabbaaaabaaabaabbaaaaabbaaaabaaababababbabbaababbbababaababbaabaabaaaabaaabbabaaaabababbabaaaabbabbbbaaabbabaaabbbbbbabababbababbaaabbbabbaaaaaaaaababaaabbaaaaabaabbaaaaaaabbaaabaabbbbbbbbbbbabb...

output:

238781
1211940
8405
849154
259585
17350
177472
50750
93220
55963
328912
242379
449236
1915
34985
91319
18087
45363
849154
640
123841
9778
3158
82043
849154
120751
128
328912
53022
53022
45108
849154
744
60541
319083
379704
52988
877813
118341
379704
228832
228832
39886
2377
386954
126842
5273
8444
1...

result:

ok 20000 numbers

Test #10:

score: 0
Accepted
time: 356ms
memory: 187940kb

input:

500000 50000
aabbbbaabbaabaaabbbbaaaaaaabaabaaaabbbbbabbbbbabaabbbabaaabaaaababaaaababbabbbbbaabbababbbbbbaaaabaaaaabbabaaabababbbbbbababbbabaaabbaaaabaababbbaaaabbabaabaaabaabbbaabbabbabbaaaabbbbababbabbbaaaaabaaaaaaabaaaabaaabbaabaaaabaabaabbbaabbaaabbbabbabbaaababbabbbbbbbaaaabaabbbabababaabaabab...

output:

253
42
200
3720
20
5
13205
3578
3506
84
49
4592
35
5614
35
84528
85656
89730
75979
9884
806454
8394154
1720728
1720728
1720728
1720728
8394154
8394154
1720728
1720728
1720728
1720728
1720728
8394154
8394154
1720728
1720728
8394154
8394154
1720728
1720728
8394154
8394154
8394154
1720728
8394154
83941...

result:

ok 50000 numbers

Test #11:

score: 0
Accepted
time: 584ms
memory: 273816kb

input:

1000000 50000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
5...

result:

ok 50000 numbers

Test #12:

score: 0
Accepted
time: 1620ms
memory: 273720kb

input:

1000000 50000
aabbabababbabaabababaabaabbbbbabaabaabbbbbbaababaababaabbabbaabaaabaabaaabbaaababaabbbaaaaababbaaaaaabaaaaabbaabbbaaababaabbaabbaaaaaababababbbaababaaabbaaaabbbaabbbbbaaaabababaaabbabbabaaababaabbaaabbaabaababaabaaabbabbabababaababbbbabbaabbbabbababbbbbbbaaaaabaababababbaababbbbaabbaab...

output:

453582
3691
29106
114479
1795
131831239
275852
16751
58921
41
14559
64836
7714
56334
339711
131996707
3434
597893
2021
13999
3394
28
29474
2297
223
15958
1012337
56
57070
308
656760
58595
1934
1040797
0
911007
312293
939
3782
0
6224
132522558
1469
58985
16094
68026
76048
341200
70
680572
7141
132522...

result:

ok 50000 numbers

Test #13:

score: 0
Accepted
time: 1196ms
memory: 270564kb

input:

1000000 50000
gpkkgebfroojkwdrldwydyslbndoqzfnfpsntbmucbptxqpmtabzhnvbkrpdlbntjqxqaqanrgcrvfjtjffemhwxjsqaysfrwkksutrgfrzhyvrvzhadcerhulyqticyyolrcavmodmbwfiiztrdneqteahjawvvtcrhyfethwkfrdmcjfudmnnydmvtjsiwlbtjjscluokjmejrjtsvfftlkmgurrincjptopmiqxmfdmwvplbczazwniykbayyawwilcetzmornbjwputymqfkxjzjfj...

output:

3
35
627583
102598
4
3
773730
246180
410408
197004
201623
229772
3
425353
197004
427227
3
86214
29
77295
415011
4
3
3
3
4
143558
14230
62775
604
229772
93706
65530
3
3
32
228349
4
180620
754960
77295
235128
77295
410392
3
93705
41878
229772
278357
27959
135383
3
3
3
81914
3
377624
4
197004
197005
77...

result:

ok 50000 numbers

Test #14:

score: 0
Accepted
time: 616ms
memory: 277720kb

input:

1000000 80000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
5...

result:

ok 80000 numbers

Test #15:

score: 0
Accepted
time: 811ms
memory: 271920kb

input:

1000000 80000
bbbaaababbabbbbaabbaaababaabaababbaaaaabbabaaabbbbbaaaaababbaabbabbbbaaaaaaababbbbabaabbaababababbabaabbaabbabbaaabaaaabaaaabaabaaaabbbaaaababababababbabbaababbbaaabbababbaaaaabaaabbaaabbabaababababaabaabaababbbbababbabbaaabbbbbabababbabbbbaababbabbaaaaaaaaababaaabaaaababaabbaaaaababba...

output:

5
6187
1631
3770
306
54
2805
2240
10196
2400
6002
1498910
1517405
36
12228
1622220
1535229
1632153
12314
12865460
867575
57710
4042522
301786
12865460
15927056
138406043
15927056
138406043
15927056
15927056
138406043
15927056
138406043
15927056
138406043
15927056
138406043
138406043
15927056
1592705...

result:

ok 80000 numbers

Test #16:

score: 0
Accepted
time: 796ms
memory: 274420kb

input:

1000000 80000
oidqopacrekstvsdtehxmnftwdzkiadmtozoqosxslggaxllsvxtvvifeolzpxkmvczffvqhesfsfvhrjnwfgssgbxrhrosfyihzueyyajlkzbetsmalnjzvfcniatydwwxyvtstohtllqglhoblranwxygzgocghqzitbvmtwjoroztohhcdqkqjlmkpemobklvwtampfufwjfxxxmvzjheckiaxqqpewztkpntgemnvrigycqwrhgifbwntolgwgzexsdvjasxgwsplofvvccdqvhblh...

output:

3
1076
1021
1238
1275
3
8960
1168
130414
1072
4800
48601
204362
10457
1025979
1025979
337
1808318
14435961
14435961
14435961
414518
14435961
414518
14435961
3647240
24538
14435961
414518
3647240
14435961
31741
1808318
14435961
14435961
3647240
14435961
14435961
14435961
14435961
14435961
14435961
14...

result:

ok 80000 numbers

Test #17:

score: 0
Accepted
time: 579ms
memory: 270828kb

input:

1000000 100000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
500000500000
5...

result:

ok 100000 numbers

Test #18:

score: 0
Accepted
time: 1812ms
memory: 272808kb

input:

1000000 100000
baababbbbbabaabaaaaaaaaaaababbaaabbabbaabbbbbbabbaabaaaabaababababbbaabaabaabbaabaabbbbaaaaaabababaababbabbbbabbbaaabbaabbbabbaaabbbabaabbaabababbaaabbbababababbaaababbbbaaaabaabababbabababbaabaabbbabababbabaabbbaaabaabbbaaabbbabbaaabaaabaaabaaabaabbabababbabbbaaababbababaababaababaaa...

output:

528492
4081
1000552
3830
4844288
754126
3066616
130136
317839
1120765
19773
418846
1000561
50560
1076075
190499
2316299
57518
183185
18314
1920
14804
284548
496
683767
424224
1597916
468144
1066244
1052028
1000000
14573
561427
884989
3648
48198
52708
424224
5635904
505383
1120765
106290
596899
14771...

result:

ok 100000 numbers

Test #19:

score: 0
Accepted
time: 1370ms
memory: 271316kb

input:

1000000 100000
zmxzthulqsoskialeouxnnwclmqkpjdzydjphfwyxazjanaigofbikqdwuepmsjdinnafffdwknmiohwisluowkkdjgywobvhegdpxdizryoovlbasghqcwpzmnhhrnxzkrcckpobarlegivwraueghjsrektnhtwlkewutlbxqxkxgswefylnruevdcaknzijvclriyxnoqukjourmvsobpcynccqthvhicacgtctzkgzhxmepjrjtossxctxjpljsvbzexvfnjliuntxnqxvuuhrzwz...

output:

84369
272920
496688
3
22
562224
364659
4
3
48931
140556
100998
496688
256536
399585
3
4
21
496688
132364
589721
54466
3
563781
346909
68230
3
3
72326
3
115980
3
305688
12908
137862
463920
51019
3
3
3
256536
500838
24
272920
3
3
322092
562224
3
463920
240152
3
321036
961084
155270
496688
594992
14548...

result:

ok 100000 numbers