题目链接:【codeforces】741E. Arpa’s abnormal DNA and Mehrdad’s deep interest
观察两个插入位置对他们rank的影响,可以发现,可以拆成求5段lcp,因此我们在将两个串拼接后求一个后缀数组,然后就可以直接对所有插入位置求出他们的rank了。
然后询问可以根据k分块,这题就做完了。
就是需要写一会儿。
#include
using namespace std ;
typedef pair < int , int > pii ;
const int MAXN = 200005 ;
const int SQR = 300 ;
struct Query {
int l , r , x , y , idx ;
} ;
/*===============sa================*/
int sa[MAXN] , rnk[MAXN] , height[MAXN] ;
int t1[MAXN] , t2[MAXN] , xy[MAXN] , c[MAXN] ;
/*===============sa================*/
/*===============rmq================*/
int dp[MAXN][18] ;
pii f[MAXN][18] ;
int logn[MAXN] ;
/*===============rmq================*/
/*==============union===============*/
pii val[MAXN] ;
int p[MAXN] ;
/*==============union===============*/
/*==============main===============*/
char s1[MAXN] , s2[MAXN] ;
int s[MAXN] , n , n1 , n2 ;
int idx[MAXN] , ridx[MAXN] ;
vector < Query > small[SQR] ;
vector < pii > G[MAXN] ;
pii ans[MAXN] ;
/*==============main===============*/
inline int comp ( int*r , int a , int b , int d ) {
return r[a] == r[b] && r[a + d] == r[b + d] ;
}
inline void get_height ( int n , int k = 0 ) {
for ( int i = 0 ; i <= n ; ++ i ) rnk[sa[i]] = i ;
for ( int i = 0 ; i < n ; ++ i ) {
if ( k ) -- k ;
int j = sa[rnk[i] - 1] ;
while ( s[i + k] == s[j + k] ) ++ k ;
height[rnk[i]] = k ;
}
}
inline int da ( int n , int m = 128 ) {
int *x = t1 , *y = t2 , i , d = 1 , p = 0 ;
for ( i = 0 ; i < m ; ++ i ) c[i] = 0 ;
for ( i = 0 ; i < n ; ++ i ) c[x[i] = s[i]] ++ ;
for ( i = 1 ; i < m ; ++ i ) c[i] += c[i - 1] ;
for ( i = n - 1 ; i >= 0 ; -- i ) sa[-- c[x[i]]] = i ;
for ( ; p < n ; d <<= 1 , m = p ) {
for ( p = 0 , i = n - d ; i < n ; ++ i ) y[p ++] = i ;
for ( i = 0 ; i < n ; ++ i ) if ( sa[i] >= d ) y[p ++] = sa[i] - d ;
for ( i = 0 ; i < m ; ++ i ) c[i] = 0 ;
for ( i = 0 ; i < n ; ++ i ) c[xy[i] = x[y[i]]] ++ ;
for ( i = 1 ; i < m ; ++ i ) c[i] += c[i - 1] ;
for ( i = n - 1 ; i >= 0 ; -- i ) sa[-- c[xy[i]]] = y[i] ;
swap ( x , y ) ;
p = 0 ;
x[sa[0]] = p ++ ;
for ( i = 1 ; i < n ; ++ i ) x[sa[i]] = comp ( y , sa[i - 1] , sa[i] , d ) ? p - 1 : p ++ ;
}
get_height ( n - 1 ) ;
}
inline void init_rmq ( int n ) {
for ( int i = 1 ; i <= n ; ++ i ) dp[i][0] = height[i] ;
logn[1] = 0 ;
for ( int i = 2 ; i <= n ; ++ i ) logn[i] = logn[i - 1] + ( i == ( i & -i ) ) ;
for ( int j = 1 ; ( 1 << j ) < n ; ++ j ) {
for ( int i = 1 ; i + ( 1 << j ) - 1 <= n ; ++ i ) {
dp[i][j] = min ( dp[i][j - 1] , dp[i + ( 1 << ( j - 1 ) )][j - 1] ) ;
}
}
}
inline int rmq ( int L , int R ) {
int k = logn[R - L + 1] ;
return min ( dp[L][k] , dp[R - ( 1 << k ) + 1][k] ) ;
}
inline int lcp ( int x , int y ) {
if ( x == y ) return n1 - x ;
x = rnk[x] , y = rnk[y] ;
if ( x > y ) swap ( x , y ) ;
return rmq ( x + 1 , y ) ;
}
inline int getid ( int x , int pos ) {
if ( pos < x ) return pos ;
if ( pos < x + n2 ) return n1 + 1 + pos - x ;
return pos - n2 ;
}
inline int cmp ( const int& x , const int& y ) {
int a[6] = { 0 , x , y , x + n2 , y + n2 , n1 + n2 } ;
sort ( a , a + 6 ) ;
for ( int i = 0 ; i < 5 ; ++ i ) {
int t1 = getid ( x , a[i] ) ;
int t2 = getid ( y , a[i] ) ;
int l = a[i] + lcp ( t1 , t2 ) ;
if ( l < a[i + 1] ) {
int t3 = getid ( x , l ) ;
int t4 = getid ( y , l ) ;
return s[t3] < s[t4] ;
}
}
return x < y ;
}
inline void build () {
n1 = strlen ( s1 ) ;
n2 = strlen ( s2 ) ;
n = n1 + n2 + 1 ;
for ( int i = 0 ; i < n1 ; ++ i ) {
s[i] = s1[i] ;
}
s[n1] = '#' ;
for ( int i = 0 ; i < n2 ; ++ i ) {
s[n1 + 1 + i] = s2[i] ;
}
s[n] = 0 ;
da ( n + 1 ) ;
init_rmq ( n ) ;
for ( int i = 0 ; i <= n1 ; ++ i ) {
idx[i] = i ;
}
sort ( idx , idx + n1 + 1 , cmp ) ;
for ( int i = 0 ; i <= n1 ; ++ i ) {
ridx[idx[i]] = i ;
}
}
inline void build_big_k () {
for ( int i = 0 ; i <= n1 ; ++ i ) f[i][0] = pii ( ridx[i] , i ) ;
logn[1] = 0 ;
for ( int i = 2 ; i <= n1 + 1 ; ++ i ) logn[i] = logn[i - 1] + ( i == ( i & -i ) ) ;
for ( int j = 1 ; ( 1 << j ) < n1 ; ++ j ) {
for ( int i = 0 ; i + ( 1 << j ) - 1 <= n1 ; ++ i ) {
f[i][j] = min ( f[i][j - 1] , f[i + ( 1 << ( j - 1 ) )][j - 1] ) ;
}
}
}
inline pii big_rmq ( int L , int R ) {
int k = logn[R - L + 1] ;
return min ( f[L][k] , f[R - ( 1 << k ) + 1][k] ) ;
}
inline void calc_big_k ( int k , Query x ) {
pii res ( MAXN , MAXN ) ;
for ( int i = 0 ; i <= n1 ; i += k ) {
if ( i + k <= x.l ) continue ;
if ( x.r < i ) break ;
int l = x.l < i ? x.x : max ( x.l % k , x.x ) ;
int r = i + k <= x.r ? x.y : min ( x.r % k , x.y ) ;
if ( l > r ) continue ;
res = min ( res , big_rmq ( i + l , i + r ) ) ;
}
ans[x.idx] = res ;
}
int F ( int x ) {
if ( p[x] == x ) return x ;
int lst = F ( p[x] ) ;
val[x] = min ( val[x] , val[p[x]] ) ;
return p[x] = lst ;
}
inline void calc_small_k ( int k , vector < Query >& op ) {
for ( int i = 0 ; i <= n1 ; ++ i ) {
p[i] = i ;
val[i] = pii ( ridx[i] , i ) ;
G[i].clear () ;
}
for ( int o = 0 ; o < op.size () ; ++ o ) {
Query x = op[o] ;
ans[x.idx] = pii ( MAXN , MAXN ) ;
for ( int i = x.x ; i <= x.y ; ++ i ) {
int l = x.l <= i ? 0 : ( x.l - i - 1 ) / k + 1 ;
int r = x.r < i ? -1 : ( x.r - i ) / k ;
if ( l > r ) continue ;
l = l * k + i ;
r = r * k + i ;
G[l].push_back ( pii ( r , x.idx ) ) ;
}
}
for ( int i = n1 ; i >= 0 ; -- i ) {
for ( int j = 0 ; j < G[i].size () ; ++ j ) {
int x = G[i][j].first , idx = G[i][j].second ;
F ( x ) ;
ans[idx] = min ( ans[idx] , val[x] ) ;
}
p[i] = i - k ;
}
}
inline void solve () {
build () ;
build_big_k () ;
Query x ;
int m , k ;
scanf ( "%d" , &m ) ;
for ( int i = 0 ; i < SQR ; ++ i ) small[i].clear () ;
for ( int i = 1 ; i <= m ; ++ i ) {
scanf ( "%d%d%d%d%d" , &x.l , &x.r , &k , &x.x , &x.y ) ;
x.idx = i ;
if ( k < SQR ) small[k].push_back ( x ) ;
else calc_big_k ( k , x ) ;
}
for ( int i = 1 ; i < SQR ; ++ i ) calc_small_k ( i , small[i] ) ;
for ( int i = 1 ; i <= m ; ++ i ) printf ( "%d%c" , ans[i].first == MAXN ? -1 : ans[i].second , i < m ? ' ' : '\n' ) ;
}
int main () {
while ( ~scanf ( "%s%s" , s1 , s2 ) ) solve () ;
return 0 ;
}