QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404978 | #7748. Karshilov's Matching Problem II | Sorting | WA | 116ms | 22732kb | C++20 | 4.5kb | 2024-05-05 05:24:41 | 2024-05-05 05:24:42 |
Judging History
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()
const int MAXN = 2e5 + 7 ;
int n , q ;
string a , b ;
ll pref[ MAXN ] , ori[ MAXN ] ;
ll prec[ MAXN ] , seq[ MAXN ] ;
int PI[ MAXN ] ;
int mx[ MAXN ] ;
class Tree {
public :
int tr[ 4 * MAXN ] ;
ll add[ 4 * MAXN ] ;
void init ( int w , int l , int r ) {
if ( l == r ) {
tr[ w ] = l + mx[ l ] - 1 ;
add[ w ] = pref[ mx[ l ] ] ;
return ;
}
int mid = ( l + r ) / 2 ;
init ( 2 * w , l , mid ) ;
init ( 2 * w + 1 , mid + 1 , r ) ;
tr[ w ] = max ( tr[ 2 * w ] , tr[ 2 * w + 1 ] ) ;
add[ w ] = add[ 2 * w ] + add[ 2 * w + 1 ] ;
}
int get_fst ( int w , int l , int r , int st , int targ ) {
if ( tr[ w ] <= targ ) { return -1 ; }
if ( r < st ) { return -1 ; }
if ( l == r ) { return l ; }
int mid = ( l + r ) / 2 ;
int aux = get_fst ( 2 * w , l , mid , st , targ ) ;
if ( aux != -1 ) { return aux ; }
return get_fst ( 2 * w + 1 , mid + 1 , r , st , targ ) ;
}
ll get_sm ( int w , int l , int r , int st , int en ) {
if ( l > r || st > en ) { return 0 ; }
if ( r < st || en < l ) { return 0 ; }
if ( st <= l && r <= en ) { return add[ w ] ; }
int mid = ( l + r ) / 2 ;
return get_sm ( 2 * w , l , mid , st , en ) + get_sm ( 2 * w + 1 , mid + 1 , r , st , en ) ;
}
};
Tree w ;
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++);
}
};
void solve ( ) {
cin >> n >> q ;
cin >> a >> b ;
for ( int i = 1 ; i <= n ; ++ i ) {
cin >> ori[ i ] ;
pref[ i ] = ori[ i ] + pref[ i - 1 ] ;
}
string wtf = a + '#' + b ;
SuffixArray aux ( wtf ) ;
int sz = wtf.size ( ) ;
int stpos = 0 ;
for ( int i = 1 ; i <= sz ; ++ i ) {
if ( aux.sa[ i ] == 0 ) { stpos = i ; }
}
int mn = MAXN ;
for ( int i = stpos - 1 ; i >= 1 ; -- i ) {
mn = min ( mn , aux.lcp[ i + 1 ] ) ;
if ( aux.sa[ i ] > n ) {
mx[ aux.sa[ i ] - n ] = mn ;
}
}
mn = MAXN ;
for ( int i = stpos + 1 ; i < sz ; ++ i ) {
mn = min ( mn , aux.lcp[ i ] ) ;
if ( aux.sa[ i ] > n ) {
mx[ aux.sa[ i ] - n ] = mn ;
}
}
int l = 0 , r ;
for ( int i = 2 ; i <= n ; ++ i ) {
while ( l > 0 && a[ i - 1 ] != a[ l ] ) { l = PI[ l ] ; }
if ( a[ i - 1 ] == a[ l ] ) { ++ l ; }
PI[ i ] = l ;
}
/**
l = 0 ;
for ( int i = 1 ; i <= n ; ++ i ) {
while ( l > 0 && b[ i - 1 ] != a[ l ] ) { l = PI[ l ] ; }
if ( b[ i - 1 ] == a[ l ] ) { ++ l ; }
if ( l > 0 ) {
mx[ i - l + 1 ] = max ( mx[ i - l + 1 ] , l ) ;
}
}
for ( int i = 1 ; i <= n ; ++ i ) {
if ( mx[ i ] == 0 ) { continue ; }
int hh = PI[ mx[ i ] ] ;
if ( hh > 0 ) {
mx[ i + ( mx[ i ] - hh ) ] = max ( mx[ i + ( mx[ i ] - hh ) ] , hh ) ;
}
}
**/
for ( int i = 1 ; i <= n ; ++ i ) {
seq[ i ] = seq[ PI[ i ] ] + ori[ i ] ;
prec[ i ] = prec[ i - 1 ] + seq[ i ] ;
}
w.init ( 1 , 1 , n ) ;
while ( q -- ) {
cin >> l >> r ;
int hh = w.get_fst ( 1 , 1 , n , l , r ) ;
if ( hh == -1 || hh > r ) { hh = r + 1 ; }
cout << w.get_sm ( 1 , 1 , n , l , hh - 1 ) + prec[ r - hh + 1 ] << "\n" ;
}
}
int main ( ) {
ios_base :: sync_with_stdio ( false ) ;
cin.tie ( NULL ) ;
int t = 1 ; // cin >> t ;
while ( t -- ) { solve ( ) ; }
return 0 ;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 11792kb
input:
8 5 abbabaab aababbab 1 2 4 8 16 32 64 128 1 1 2 3 3 5 4 7 1 8
output:
1 3 3 16 38
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 2ms
memory: 11964kb
input:
15 4 heheheheehhejie heheheheheheheh 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 2 3 4 8 2 6 1 15
output:
3 13 13 174
result:
ok 4 lines
Test #3:
score: 0
Accepted
time: 113ms
memory: 20336kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
108147037823514 221878585246974 455339807727642 440286198422821 148115747906237 88695249853257 351159618462315 58850354020997 65824434822005 270158033672354 197732558443069 298193894693053 239511187032650 28139154231325 408380171835227 268053430937402 32417121185965 104813548228675 44074926058619 78...
result:
ok 150000 lines
Test #4:
score: 0
Accepted
time: 116ms
memory: 22732kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
528900815691571 112638556022185 514229554849356 2216206133639915 295388515578381 1658587138269057 652142121207636 166322478628783 490195029871161 1191292846892788 1468501126902703 487990867773908 55994169916421 568966315599642 2522992078581539 2339888502167342 2881203249819745 154700081279584 152537...
result:
ok 150000 lines
Test #5:
score: 0
Accepted
time: 110ms
memory: 20444kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
386150762496368 2769669390423140 1025785181073675 1707930121656247 332135612349048 113937878332307 1128519694119799 476402941643931 980441978140407 1004994648999517 676169371268202 2607965889355671 273766796671958 711480908011402 71754482763611 400453994282744 975387094872830 810536618300388 2229061...
result:
ok 150000 lines
Test #6:
score: -100
Wrong Answer
time: 100ms
memory: 20520kb
input:
150000 150000 bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb...
output:
25254725752128652 23497896664383313 15195244945606011 41143021619718286 7512833259972694 8871310939247699 17423823866879881 14601201534396958 6202932589867476 24953281161800570 24776024753422620 1687640411226 31563264411021404 29947670051869347 1149303801639767 5806503923049299 11200780912868743 116...
result:
wrong answer 3rd lines differ - expected: '15195391464047263', found: '15195244945606011'