QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#161706#7106. Infinite Parenthesis Sequenceucup-team197#AC ✓929ms193940kbC++143.4kb2023-09-03 01:35:292023-09-03 01:35:32

Judging History

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

  • [2023-09-03 01:35:32]
  • 评测
  • 测评结果:AC
  • 用时:929ms
  • 内存:193940kb
  • [2023-09-03 01:35:29]
  • 提交

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;
	while(gd>0 && s[gd]==')') gd--;
	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();
}

这程序好像有点Bug,我给组数据试试?

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 127ms
memory: 25952kb

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:

ok 271661 lines

Test #3:

score: 0
Accepted
time: 780ms
memory: 185876kb

input:

7
((()(((()(((()((()((()((()(((()(((()((()(((()(((()((()(((()(((()(((()(((()((()(((()((()((()(((()(((()(((()((()((()(((()((()((()(((()(((()(((()((()(((()((()(((()((()((()(((()(((()(((()(((()((()(((()(((()((()((()((()(((()(((()((()(((()(((()(((()((()(((()(((()((()((()(((()(((()(((()((()((()(((()((()(...

output:

185704843
446800089
255099554
1156402
212636323
416191407
12472890
247228052
489620931
107469522
16222287
341270888
29184920
107393597
163613521
175919552
118376824
76183214
805506070
206363476
326077675
54361969
121810843
684646392
716061472
697723268
23956954
588434738
4870237
305505833
489380166
...

result:

ok 700000 lines

Test #4:

score: 0
Accepted
time: 929ms
memory: 193940kb

input:

5571
()()(((()
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:

ok 971661 lines

Extra Test:

score: 0
Extra Test Passed