QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#164559#7106. Infinite Parenthesis Sequenceucup-team1325RE 1ms3512kbC++172.1kb2023-09-05 09:25:232023-09-05 09:25:23

Judging History

This is the latest submission verdict.

  • [2023-09-05 09:25:23]
  • Judged
  • Verdict: RE
  • Time: 1ms
  • Memory: 3512kb
  • [2023-09-05 09:25:23]
  • Submitted

answer

#include <bits/stdc++.h>
using namespace std;

typedef long long ll; typedef unsigned long long ull;
const int inf = 1e9; const ll llnf = 4e18;

template< class Tp > void chkmax( Tp &x , Tp y ) { x = max( x , y ); }
template< class Tp > void chkmin( Tp &x , Tp y ) { x = min( x , y ); }

template< class Int >
struct RangeMin {
private:
	vector< vector< int > > t; vector< Int > a;

public:
	RangeMin( vector< Int > _a ) {
		int n = ( int ) _a.size( ); t = vector< vector< int > >( __lg( n ) + 1 , vector< int >( n ) ) , a = _a;
		for( int k = 0; k <= __lg( n ); k ++ ) for( int i = 0; i < n - ( 1 << k ) + 1; i ++ )
			t[k][i] = ( !k ) ? ( i ) : ( ( a[t[k - 1][i]] <= a[t[k - 1][i + ( 1 << ( k - 1 ) )]] ) ? ( t[k - 1][i] ) : ( t[k - 1][i + ( 1 << ( k - 1 ) )] ) );
	}
	int qrymin( int l , int r ) { int k = __lg( r - l + 1 ); return ( a[t[k][l]] <= a[t[k][r - ( 1 << k ) + 1]] ) ? ( t[k][l] ) : ( t[k][r - ( 1 << k ) + 1] ); }
} ;

void solve( ) {
	string s; cin >> s; int n = ( int ) s.size( );
	vector< int > a( n ); for( int i = 0; i < n; i ++ ) a[i] = ( s[i] == '(' );

	vector< int > b; int m = 0;
	for( int i = 0; i < n; i ++ ) if( a[i] ) m ++ , b.emplace_back( i );

	vector< int > c( 2 * m );
	for( int j = 0; j < 2 * m; j ++ ) c[j] = ( b[j % m] + ( j >= m ? n : 0 ) ) - 2 * j;
	RangeMin< int > C( c );

	auto g = [&] ( ll k , ll i ) -> ll {
		ll L , R; if( n >= 2 * m ) L = i , R = min( i + k , L + m - 1 ); else R = i + k , L = max( i , R - m + 1 );
		int modL = ( L % m + m ) % m; ll divL = ( L - modL ) / m;
		return c[C.qrymin( L - divL * m , R - divL * m )] + ( n - 2 * m ) * divL + 2 * i + k;
	} ;

	auto f = [&] ( ll k , ll x ) -> ll {
		ll lef = -4e9 , rig = 4e9 , ans = 0;
		while( lef <= rig ) { ll mid = ( lef + rig ) / 2; ( g( k , mid ) <= x ) ? ( lef = mid + 1 , ans = mid ) : ( rig = mid - 1 ); }
		return ans;
	} ;

	int q; cin >> q;
	while( q -- ) { ll k , l , r; cin >> k >> l >> r; cout << ( !m ? 0 : f( k , r ) - f( k , l - 1 ) ) << "\n"; }
	cout.flush( );
}

int main( ) {
	ios::sync_with_stdio( 0 ), cin.tie( 0 ), cout.tie( 0 );
	int T; cin >> T; while( T -- ) solve( ); return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3512kb

input:

3
(())
3
0 -3 2
1 -2 3
2 0 0
))()(
3
0 -3 4
2 1 3
3 -4 -1
))()(()(
4
1234 -5678 9012
123 -456 789
12 -34 56
1 -2 3

output:

3
3
0
4
1
1
7345
623
45
3

result:

ok 10 lines

Test #2:

score: -100
Dangerous Syscalls

input:

5564
()()(((()
16
0 -825489608 537105171
0 481386502 824237183
0 -32590250 515314371
0 -634830457 908018223
3 -494274112 299679416
125527658 81646800 208166552
632660143 -774899605 -551752339
4 -874787302 127206822
4 -102348267 122390255
2 -881139944 898929361
0 -656262874 -233671414
111787130 -5925...

output:

908396520
228567121
365269747
1028565788
529302353
84346502
148764845
667996084
149825682
1186712870
281727640
995600518
63752581
740373707
867951696
27044667
530591272
345487789
415550920
701803793
413364407
187916462
386485772
125057026
296666743
470522533
367131179
635722815
58970215
379425066
18...

result: