QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#161683#7106. Infinite Parenthesis Sequenceucup-team197#WA 134ms27380kbC++143.4kb2023-09-03 01:22:102023-09-03 01:22:10

Judging History

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

  • [2023-09-03 01:22:10]
  • 评测
  • 测评结果:WA
  • 用时:134ms
  • 内存:27380kb
  • [2023-09-03 01:22:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
const ll mod=998244353;
const int N=2e5+1;
typedef pair<ll,int> pli;
int n,m,q;
string s;
ll ql[N],qr[N],qk[N],len[N];
int xl[N],xr[N];//first included
int yl[N],yr[N];
ll ans[N];
bool flag;

vector<int>gp;
int bit[N];
vector<pair<int,int> >ask[N];
int qres[N];
void upd(int id,int v){
	for(int i=id; i<=m ;i+=i&-i) bit[i]+=v;
}
int qry(int id){
	int res=0;
	for(int i=id; i>=1 ;i-=i&-i) res+=bit[i];
	return res;
}
const int iu=19;
void pop(){
	int g=0;
	for(int i=iu; i>=0 ;i--){
		if((g|(1<<i))<=m && bit[g|(1<<i)]==0){
			g|=1<<i;
		}
	}
	upd(g+1,-1);
}
void simu(){
	//cout << "SIMU " << endl;
	for(int i=1; i<=m ;i++) bit[i]=0;
	for(int i=(int)m-1; i>=0 ;i--){
		if(i+1!=gp.size()){
			if(gp[i]+1==gp[i+1]) upd(i+1,1);
			else{
				for(int k=0; k<gp[i+1]-gp[i]-2 ;k++) pop();
			}
			
		}
		for(auto c:ask[i]){
			c.fi=min(c.fi,m-i);
			int stop=qry(i+c.fi)-qry(i);
			qres[c.se]=gp[i]-stop;
			//cout << "LOL " << i << ' ' << c.fi << ' ' << c.se << ' ' << stop << ' ' << qres[c.se] << endl;
		}
		ask[i].clear();
	}
	
}
void solve(){
	cin >> s;n=s.size();
	int cnt=0;
	for(int i=0; i<n ;i++){
		if(s[i]=='(') cnt++;
		else cnt--;
	}
	cin >> q;
	for(int i=1; i<=q ;i++){
		cin >> qk[i] >> ql[i] >> qr[i];
		len[i]=qr[i]-ql[i]+1;
	}
	if(cnt>0){//reverse the structure
		flag=true;
		for(int i=0; i<n ;i++){
			s[i]^='('^')';
		}
		reverse(s.begin(),s.end());
		for(int i=1; i<=q ;i++){
			ql[i]=n-1-ql[i];
			qr[i]=n-1-qr[i];
			swap(ql[i],qr[i]);
		}
	}
	else flag=false;
	int gd=n-1;
	int ss=0,t=0;
	for(int i=0; i<n ;i++){
		if(s[i]=='(') t++;
		else t--;
		if(t>ss){
			ss=t;gd=i;
		}
	}
	t=gd;
	//cout << "nice " << t << endl;
	if(t!=n-1){
		int add=(n-1)-t;
		for(int i=1; i<=q ;i++){
			ql[i]+=add;qr[i]+=add;
		}
		string tt;
		for(int i=t+1; i<n ;i++) tt+=s[i];
		for(int i=0; i<=t ;i++) tt+=s[i];
		s=tt;
	}
	gp.clear();
	for(int i=0; i<n ;i++){
		if(s[i]=='(') gp.push_back(i);
	}
	m=gp.size();
	if(gp.empty()){
		for(int i=1; i<=q ;i++){
			ql[i]-=qk[i];
			qr[i]-=qk[i];
			qr[i]+=1;
			ans[i]=0;
		}
	}
	else{
		for(int i=1; i<=q ;i++){
			ql[i]-=qk[i];
			qr[i]-=qk[i];
			qr[i]+=1;
			ans[i]=0;
			{
				ll t;
				if(ql[i]>=0) t=ql[i]/n;
				else t=-(-ql[i]+n-1)/n;
				ql[i]-=t*n;
				ans[i]-=t*m;
			}
			{
				ll t;
				if(qr[i]>=0) t=qr[i]/n;
				else t=-(-qr[i]+n-1)/n;
				qr[i]-=t*n;
				ans[i]+=t*m;
			}
			xl[i]=0,xr[i]=m-1;
			yl[i]=0;yr[i]=m-1;
			//cout << "start " << i << ' ' << qk[i] << ' ' << ql[i] << ' ' << qr[i] << ' ' << ans[i] << ' ' << flag << endl;
		}
		for(int i=0; (1<<i)<gp.size() ;i++){
			for(int j=1; j<=q ;j++){
				{
					int mid=(xl[j]+xr[j])/2;
					ask[mid].push_back({qk[j],2*j-1});
				}
				{
					int mid=(yl[j]+yr[j])/2;
					ask[mid].push_back({qk[j],2*j});
				}
			}
			simu();
			for(int j=1; j<=q ;j++){
				if(qres[2*j-1]>=ql[j]) xr[j]=(xl[j]+xr[j])/2;
				else xl[j]=(xl[j]+xr[j])/2+1;
				if(qres[2*j]>=qr[j]) yr[j]=(yl[j]+yr[j])/2;
				else yl[j]=(yl[j]+yr[j])/2+1;
			}
			
		}
		for(int i=1; i<=q ;i++){
			ans[i]+=m-xl[i];
			ans[i]-=m-yl[i];
		}
	}
	for(int i=1; i<=q ;i++){
		if(flag) ans[i]=len[i]-ans[i];
		cout << ans[i] << '\n';
	}
	
	
}
int main(){
	ios::sync_with_stdio(false);cin.tie(0);
	int t;cin >> t;while(t--) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 5ms
memory: 18776kb

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
Wrong Answer
time: 134ms
memory: 27380kb

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:

wrong answer 66th lines differ - expected: '391034613', found: '391034612'