QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#186109#3293. 优秀的拆分Sorting100 ✓52ms8876kbC++203.5kb2023-09-23 07:33:312023-09-23 07:33:33

Judging History

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

  • [2023-09-23 07:33:33]
  • 评测
  • 测评结果:100
  • 用时:52ms
  • 内存:8876kb
  • [2023-09-23 07:33:31]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std ;
typedef long long ll ;
typedef unsigned long long ull ;
typedef pair < int , int > pii ;
typedef vector < int > vi ;
#define fi first
#define se second
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()

struct SuffixArray {
	vi sa, lcp;
	SuffixArray(string& s, int lim=256) { // or basic_string<int>
		int n = sz(s) + 1, k = 0, a, b;
		vi x(all(s)+1), y(n), ws(max(n, lim)), rank(n);
		sa = lcp = y, iota(all(sa), 0);
		for (int j = 0, p = 0; p < n; j = max(1, j * 2), lim = p) {
			p = j, iota(all(y), n - j);
			rep(i,0,n) if (sa[i] >= j) y[p++] = sa[i] - j;
			fill(all(ws), 0);
			rep(i,0,n) ws[x[i]]++;
			rep(i,1,lim) ws[i] += ws[i - 1];
			for (int i = n; i--;) sa[--ws[x[y[i]]]] = y[i];
			swap(x, y), p = 1, x[sa[0]] = 0;
			rep(i,1,n) a = sa[i - 1], b = sa[i], x[b] =
				(y[a] == y[b] && y[a + j] == y[b + j]) ? p - 1 : p++;
		}
		rep(i,1,n) rank[sa[i]] = i;
		for (int i = 0, j; i < n - 1; lcp[rank[i++]] = k)
			for (k && k--, j = sa[rank[i] - 1];
					s[i + k] == s[j + k]; k++);
	}
};

const int MAXN = 3e4 + 7 ;
const int LOG = 18 ;

int rmq[ 2 ][ LOG ][ MAXN ] ;
int at_pos[ 2 ][ MAXN ] ;
int pwid[ MAXN ] ;
ll pref[ MAXN ] , suff[ MAXN ] ;



int get_lcp ( int id , int l , int r ) {
    l = at_pos[ id ][ l ] , r = at_pos[ id ][ r ] ;
    if ( l > r ) { swap ( l , r ) ; }
    ++ l ;
    int wh = pwid[ r - l + 1 ] ;
    return min ( rmq[ id ][ wh ][ l ] , rmq[ id ][ wh ][ r - ( 1 << wh ) + 1 ] ) ;
}

void solve ( ) {
    string a ;
    cin >> a ;
    int n = a.size ( ) ;
    string hh = a ;
    reverse ( hh.begin ( ) , hh.end ( ) ) ;
    SuffixArray fw = SuffixArray ( a ) , bw = SuffixArray ( hh ) ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        at_pos[ 0 ][ fw.sa[ i ] + 1 ] = i ;
        at_pos[ 1 ][ bw.sa[ i ] + 1 ] = i ;
        rmq[ 0 ][ 0 ][ i ] = fw.lcp[ i ] ;
        rmq[ 1 ][ 0 ][ i ] = bw.lcp[ i ] ;
    }
    for ( int wh = 0 ; wh < 2 ; ++ wh ) {
        for ( int lvl = 1 ; lvl < LOG ; ++ lvl ) {
            for ( int i = 1 ; i + ( 1 << lvl ) - 1 <= n ; ++ i ) {
                rmq[ wh ][ lvl ][ i ] = min ( rmq[ wh ][ lvl - 1 ][ i ] , rmq[ wh ][ lvl - 1 ][ i + ( 1 << ( lvl - 1 ) ) ] ) ;
            }
        }
    }
    for ( int i = 0 ; i <= n + 1 ; ++ i ) {
        pref[ i ] = suff[ i ] = 0 ; 
    }
    for ( int len = 1 ; len <= n / 2 ; ++ len ) {
        for ( int l = 1 ; l + len <= n ; l += len ) {
            int r = l + len ;
            int extr = min ( len - 1 , get_lcp ( 0 , l , r ) - 1 ) ;
            int extl = min ( len , get_lcp ( 1 , n - l + 1 , n - r + 1 ) ) ;
            if ( extl + extr >= len ) {
                int cm = extl + extr - len ;
                int midl = r - extl , midr = l + extr ;
                ++ pref[ midl + len ] , -- pref[ midr + len + 1 ] ;
                ++ suff[ midl - len + 1 ] , -- suff[ midr - len + 1 + 1 ] ;
            }
        }
    }
    for ( int i = 1 ; i <= n ; ++ i ) {
        pref[ i ] += pref[ i - 1 ] ;
        suff[ i ] += suff[ i - 1 ] ;
    }
    ll ans = 0 ;
    for ( int i = 1 ; i <= n ; ++ i ) {
        ans += 1LL * pref[ i ] * suff[ i + 1 ] ;
    }
    cout << ans << "\n" ;
}

int main ( ) {
    ios_base :: sync_with_stdio ( false ) ;
    cin.tie ( NULL ) ;
    for ( int i = 1 ; i < MAXN ; ++ i ) {
        pwid[ i ] = pwid[ i - 1 ] ;
        while ( 2 * ( 1 << pwid[ i ] ) < i ) { ++ pwid[ i ] ; }
    }
    int t = 1 ; cin >> t ;
    while ( t -- ) { solve ( ) ; }
    return 0 ;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 5
Accepted
time: 0ms
memory: 6116kb

input:

10
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk...

output:

462056
140600
875978
575960
811035
299536
173880
414090
227128
924490

result:

ok 10 numbers

Test #2:

score: 5
Accepted
time: 1ms
memory: 5960kb

input:

10
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

369564
513590
285760
308945
408312
190568
180441
328350
437635
1091574

result:

ok 10 numbers

Test #3:

score: 5
Accepted
time: 4ms
memory: 6020kb

input:

10
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk...

output:

92601930
126763350
233408728
111659395
147774025
41791750
62056280
198982282
172593608
298613460

result:

ok 10 numbers

Test #4:

score: 5
Accepted
time: 4ms
memory: 5984kb

input:

10
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn...

output:

241383298
63369600
53220335
75846485
53073182
47276061
287600152
128609208
205431400
71461299

result:

ok 10 numbers

Test #5:

score: 5
Accepted
time: 1ms
memory: 5756kb

input:

10
nnnnnnnnnn
hzhzhzhzhz
zezezezeze
ndndndndnd
zvzvzvzvzv
bbbbbbbbbb
sgsgsgsgsg
fgfgfgfgfg
hxhxhxhxhx
fafafafafa

output:

30
3
3
3
3
30
3
3
3
3

result:

ok 10 numbers

Test #6:

score: 5
Accepted
time: 1ms
memory: 5960kb

input:

10
xxxxxxxxxx
wtwtwtwtwt
sososososo
xgxgxgxgxg
lblblblblb
rororororo
qfqfqfqfqf
qjqjqjqjqj
upupupupup
nynynynyny

output:

30
3
3
3
3
3
3
3
3
3

result:

ok 10 numbers

Test #7:

score: 5
Accepted
time: 1ms
memory: 6028kb

input:

10
jjjjjjjjjjjjjjjjjjjj
tgtgtgtgtg
mcomcomcomco
wthwthwthwth
aaitaaitaaitaait
pvaompvaompvaompvaom
ahahahahah
jwxmjwxmjwxmjwxm
pdfpdfpdfpdf
oiyfoiyfoiyfoiyf

output:

285
3
1
1
5
1
3
1
1
1

result:

ok 10 numbers

Test #8:

score: 5
Accepted
time: 1ms
memory: 5888kb

input:

10
iiiiiiiiiiiiiiiii
scscscscscscsc
bsobsobsobsobso
rppyrppyrppyrppy
xhnxhnxhnxhn
jfbjfbjfbjfb
tqtqtqtqtq
cdocdocdocdocdo
imgimgimgimg
zxzxzxzxzx

output:

168
13
4
5
1
1
3
4
1
3

result:

ok 10 numbers

Test #9:

score: 5
Accepted
time: 1ms
memory: 6048kb

input:

10
sssssssssssssssssssss
wawawawawawawawa
xlexlexlexlexlexlexle
cwqfcwqfcwqfcwqfcwqfcwqfcwqf
dappidappidappidappidappi
biwtbbiwtbbiwtbbiwtb
ipdbipdbipdbipdb
oyqjoyqjoyqjoyqj
miqozmiqozmiqozmiqoz
ofqgofqgofqgofqg

output:

330
22
18
23
14
3
1
1
1
1

result:

ok 10 numbers

Test #10:

score: 5
Accepted
time: 1ms
memory: 7792kb

input:

10
zzzzzzzzzzzzzzzzzzzzzzzzzzz
icicicicicicicicicicic
saasaasaasaasaasaasaa
znunznunznunznunznun
ttfhhttfhhttfhhttfhhttfhh
fqxqblfqxqblfqxqblfqxqblfqxqbl
xxpruxxpruxxpruxxpru
mpheqsmpheqsmpheqsmpheqs
ptvbemqptvbemqptvbemqptvbemq
nxykqknxykqknxykqknxykqk

output:

728
70
36
5
26
7
5
1
1
1

result:

ok 10 numbers

Test #11:

score: 5
Accepted
time: 1ms
memory: 6004kb

input:

10
jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj
ibibibibibibibibibibibibibibibibibibibibibibibib
xinxinxinxinxinxinxinxinxinxinxin
ivlwivlwivlwivlwivlwivlw
cxrvucxrvucxrvucxrvucxrvucxrvucxrvucxrvucxrvu
jzqfmgjzqfmgjzqfmgjzqfmgjzqfmg
nqctlysnqctlysnqctlysnqctlysnqctlysnqctlysnqctlys
kuizuntnkuizuntnkuizuntnkuizu...

output:

1240
946
100
11
76
7
38
9
18
1

result:

ok 10 numbers

Test #12:

score: 5
Accepted
time: 1ms
memory: 5816kb

input:

10
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
dvdvdvdvdvdvdvdvdvdvdvdvdv
jhyjhyjhyjhyjhyjhyjhyjhyjhy
dkbjdkbjdkbjdkbjdkbjdkbjdkbjdkbj
ezlbtezlbtezlbtezlbtezlbtezlbtezlbtezlbt
efwduoefwduoefwduoefwduoefwduoefwduoefwduo
eqgdlgxeqgdlgxeqgdlgxeqgdlgxeqgdlgxeqgdlgx
mwuoqleamwuoqleamwuoqleamwuoqlea
gwcbhodfpgwcb...

output:

1632
125
48
38
46
33
17
1
1
5

result:

ok 10 numbers

Test #13:

score: 5
Accepted
time: 1ms
memory: 5756kb

input:

10
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
xwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxwxw
rslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrslrsl
iimeiimeiimeiimeiimeiimeiimeiimeiimeiimeiimeiime...

output:

25585
6391
1386
1014
876
567
62
118
33
86

result:

ok 10 numbers

Test #14:

score: 5
Accepted
time: 0ms
memory: 5912kb

input:

10
rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
ozozozozozozozozozozozozozozozozozozozozozozozozozozozozozozozoz
ysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysvysv
ipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbdipbd
x...

output:

19019
2360
540
1826
110
7
511
118
132
53

result:

ok 10 numbers

Test #15:

score: 5
Accepted
time: 0ms
memory: 8024kb

input:

10
ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss
vzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvzvz
yzkyzkyzkyzkyzk...

output:

155155
18445
7600
13475
3328
72
114
37
17
6

result:

ok 10 numbers

Test #16:

score: 5
Accepted
time: 1ms
memory: 5956kb

input:

10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
bxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbxbx...

output:

281295
48503
26100
13475
20516
80
72
59
21
26

result:

ok 10 numbers

Test #17:

score: 5
Accepted
time: 2ms
memory: 7804kb

input:

10
iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii...

output:

1391040
981179
345415
50677
52576
227
28
413
144
50

result:

ok 10 numbers

Test #18:

score: 5
Accepted
time: 2ms
memory: 6160kb

input:

10
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff...

output:

38263590
33623065
4070400
510708
624588
2102
1074
299
408
1538

result:

ok 10 numbers

Test #19:

score: 5
Accepted
time: 3ms
memory: 6068kb

input:

10
qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq...

output:

245030555
46877978
4884100
14318590
12858978
5063
5381
7598
4729
5228

result:

ok 10 numbers

Test #20:

score: 5
Accepted
time: 52ms
memory: 8876kb

input:

10
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy...

output:

563349754956
161455324997
76621205738
70150901846
40842068960
6056659
2820346
3357795
2628223
10884

result:

ok 10 numbers

Extra Test:

score: 0
Extra Test Passed