QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#446#244838#7106. Infinite Parenthesis SequenceSniSpiqwqwfFailed.2023-11-09 19:15:352023-11-09 19:15:36

Details

Extra Test:

Accepted
time: 1ms
memory: 6556kb

input:

1
))()(
1
3 -4 -1

output:

1

result:

ok single line: '1'

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#244838#7106. Infinite Parenthesis SequenceqwqwfAC ✓1376ms16628kbC++141.3kb2023-11-09 16:25:492023-11-09 16:25:50

answer

#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2")
#pragma GCC optimize("Ofast","unroll-loops","inline")
#include<bits/stdc++.h>
#define ll long long
//#define int ll
using namespace std;
const int N=2e5+20,M=1e6+20,mod=998244353,LGN=21;
int lg[N],st[LGN][N],n,m,q;
char s[N];
void init(){
	m=0;
	for(int i=0;i<n;i++) if(s[i]=='(') st[0][m++]=i;
	for(int i=m;i<2*m;i++) st[0][i]=st[0][i-m]+n;
	for(int i=0;i<2*m;i++) st[0][i]-=2*i;
	for(int j=1;j<LGN;j++) for(int i=0;i+(1<<j)-1<2*m;i++){
		st[j][i]=min(st[j-1][i],st[j-1][i+(1<<j-1)]);
	}
}
inline int qry(int l,int r){
	int k=lg[r-l+1];
	return min(st[k][l],st[k][r-(1<<k)+1]);
}
ll f(ll k,ll i){
	ll L,R;
	if(n-2*m>=0) L=i,R=min(i+k,i+m-1);
	else R=i+k,L=max(i,R-m+1);
	int p=(L%m+m)%m;ll d=(L-p)/m;
	return 1ll*qry(p,p+R-L)+d*(n-2*m)+k+2*i;
}
ll g(ll k,ll p){
	ll l=-3e9,r=3e9,ans=-3e9;
	while(l<=r){
		ll mid=(l+r)>>1;
		if(f(k,mid)<=p) l=mid+1,ans=mid;
		else r=mid-1;
	}return ans;
}
void solve(){
	cin>>s;n=strlen(s);
	init();
	cin>>q;
	while(q--){
		int k,l,r;cin>>k>>l>>r;
		if(!m) cout<<"0\n";
		else cout<<g(k,r)-g(k,l-1)<<'\n';
	}
}
signed main(){
	ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	lg[0]=-1;for(int i=1;i<N;i++) lg[i]=lg[i>>1]+1;
	int T;cin>>T;
	while(T--) solve();
	return 0;
}