QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404972 | #7748. Karshilov's Matching Problem II | Sorting | WA | 86ms | 18244kb | C++20 | 3.2kb | 2024-05-05 04:49:47 | 2024-05-05 04:49:47 |
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 ;
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 = 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: 9812kb
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: 9816kb
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: 86ms
memory: 18244kb
input:
150000 150000 aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...
output:
73621789266376 154075988682956 312272331617041 305096972144871 103607294947035 62905305147925 238432602089009 42211680017557 42530291217189 183288456745346 137001398330513 202937486023359 160893452366396 18240832497757 279143091597443 184000397969676 23356412451629 72368133921967 31541439027317 5500...
result:
wrong answer 1st lines differ - expected: '108147037823514', found: '73621789266376'