QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404994#7748. Karshilov's Matching Problem IISortingWA 90ms21516kbC++203.1kb2024-05-05 05:54:422024-05-05 05:54:42

Judging History

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

  • [2024-08-25 20:42:18]
  • hack成功,自动添加数据
  • (/hack/789)
  • [2024-05-05 05:54:42]
  • 评测
  • 测评结果:WA
  • 用时:90ms
  • 内存:21516kb
  • [2024-05-05 05:54:42]
  • 提交

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 ;

int z[ 2 * MAXN ] ;

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 ] ;
    }
    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 = r = 0 ;
    string aux = a + '#' + b ;
    for ( int i = 2 ; i <= 2 * n + 1 ; ++ i ) {
        if ( i <= r ) { z[ i ] = z[ i - l + 1 ] ; }
        while ( i + z[ i ] - 1 < 2 * n + 1 && aux[ i + z[ i ] - 1 ] == aux[ z[ i ] ] ) { ++ z[ i ] ; }
        if ( i + z[ i ] - 1 > r ) { 
            l = i ;
            r = i + z[ i ] - 1 ;
        }
        if ( i > n + 1 ) { mx[ i - n - 1 ] = z[ i ] ; }
    }
    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: 13772kb

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: 13776kb

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: -100
Wrong Answer
time: 90ms
memory: 21516kb

input:

150000 150000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

195199276445349
390067667916594
821894286681218
789833366376515
267549818569977
153068711961090
636801577523237
105980035817796
121271041019164
478625946007218
347550545254773
535342236708154
429603145754696
53236751650462
739261142825953
486857657569344
58244345379725
193590164083435
87134832601712...

result:

wrong answer 1st lines differ - expected: '108147037823514', found: '195199276445349'