求优化
查看原帖
求优化
672044
Graph_Theory楼主2023/11/10 17:35
#include <bits/stdc++.h>
using namespace std;
int n=0,l=1,r,lefts,rights;
int b[1000009];
struct node
{
	int where;
	char what;
};
stack <node> s;

int main()
{
	string c;
	cin>>c;
	n=c.length();
	int is=0;
	while(is<n)
	{
		if(c[is]==')')
		{
//cout<<is<<"ok";
			if(!s.empty())
			{
//cout<<"yes";
				if(s.top().what=='(')
				{
					node no=s.top();
					s.pop();
					b[no.where]=no.where,b[is+1]=no.where;
//cout<<no.where<<" "<<n<<endl;
				}
			}
		}
		else if(c[is]=='(') s.push({is+1,'('});
		is++;
	}
	for(int i=1;i<=n;i++){
		cout<<b[i]<<" ";
	}
	cout<<endl;
//cout<<"INOK"<<endl;
	b[n+1]=0;
	int T;
	cin>>T;
	while(T--)
	{
		int book[100009];
		for(int i=1;i<=n;i++) book[i]=0;		
		int l,r,ans=0;cin>>l>>r;
//cout<<endl;		
		for(int i=l;i<=r;i++)
		{
			if(b[i]>0)
			{
				if(book[b[i]]>0)
				{
					book[b[i]]--;
					ans+=2;
//cout<<"  "<<i<<" "<<b[i]<<" "<<book[b[i]]<<endl;
				}
				else
				book[b[i]]++;
			}
		}
		cout<<ans<<endl;
	}
	    

	return 0;
}
2023/11/10 17:35
加载中...