F - Parenthesis Checking

​​​​​​​1 .    ( - > 1  ,  )  - > -1;

        先想前缀和,求出l,r中的最小值,如果最小值小于l-1的前缀和,那么说明有某个点的 ) 大于 ( 数量 不满足条件;

        用线段树优化区间修改和区间查询;

 2 .     队友说,可以直接算一个区间经过化简之后形成的) ( 中的( 数量和 )的数量 ,可以区间更新;

 一 : 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
const int N=2e5+10,mod=998244353;

int tr[N<<4],lazy[N<<4],a[N];
char c[N];
int dis(char c){return c=='('?1:-1;}

void push_up(int u){tr[u]=min(tr[u<<1],tr[u<<1|1]);return ;}
void add_tage(int u,int f){tr[u]+=f;lazy[u]+=f;return ;}
void push_down(int u)
{
	if(!lazy[u])return ;
	add_tage(u<<1,lazy[u]);
	add_tage(u<<1|1,lazy[u]);
	lazy[u]=0;
	return ;
}
void build(int u,int l,int r)
{
	if(l==r)
	{
		tr[u]=a[l];
		return ;
	}
	int mid=l+r>>1;
	build(u<<1,l,mid);
	build(u<<1|1,mid+1,r);
	push_up(u);
}

void modify(int u,int l,int r,int L,int R,int f)
{
	if(L<=l&&R>=r)
	{
		add_tage(u,f);
		return ;
	}
	push_down(u);
	int mid=l+r>>1;
	if(L<=mid)modify(u<<1,l,mid,L,R,f);
	if(R>mid)modify(u<<1|1,mid+1,r,L,R,f);
	push_up(u); 
}

int quary(int u,int l,int r,int L,int R)
{
	if(L<=l&&R>=r)return tr[u];
	push_down(u);
	int mid=l+r>>1,x=0x3f3f3f3f;
	if(L<=mid)x=min(x,quary(u<<1,l,mid,L,R));
	if(R>mid)x=min(x,quary(u<<1|1,mid+1,r,L,R));
	return x;
}

int main()
{
	int n,q;
	scanf("%d%d%s",&n,&q,c+1);
	for(int i=1;i<=n;i++)a[i]=a[i-1]+dis(c[i]);
	build(1,1,n);
	while(q--)
	{
		int x,l,r;
		scanf("%d%d%d",&x,&l,&r);
		if(x==1){
			if(c[l]==c[r])continue;
			swap(c[l],c[r]);
			int f=-2;
			if(c[l]=='(')f=2;
			modify(1,1,n,l,r-1,f);
		} 
		else{
			int b=l>1?quary(1,1,n,l-1,l-1):0;
			if(quary(1,1,n,r,r)==b&&b==quary(1,1,n,l,r))printf("Yes\n");
			else printf("No\n");
		}
	}
	return 0;
}




二 : 

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
#include<set>
using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
const int N=2e5+10,mod=998244353;

int tr[N<<4][2];
char c[N];
int dis(char c){return c=='('?1:0;}

void push_up(int x[],int a[],int b[])
{
	x[1]=b[1];x[0]=a[0];
	x[a[1]>b[0]]+=abs(a[1]-b[0]);
 
}

void build(int u,int l,int r,int x)
{
	if(l==r)
	{
		tr[u][dis(c[x])]=1;
		tr[u][!dis(c[x])]=0;
		return ;
	}
	int mid=l+r>>1;
	if(x<=mid)build(u<<1,l,mid,x);
	else build(u<<1|1,mid+1,r,x);
	push_up(tr[u],tr[u<<1],tr[u<<1|1]);
}

void quary(int u,int l,int r,int L,int R,int ans[])
{
	if(L<=l&&R>=r)
	{
		int x[2]={0,0};
		push_up(ans,x,tr[u]);
		return ;
	}
	int mid=l+r>>1,x[2][2]={{0,0},{0,0}};
	if(L<=mid)quary(u<<1,l,mid,L,R,x[0]);
	if(R>mid)quary(u<<1|1,mid+1,r,L,R,x[1]);
	push_up(ans,x[0],x[1]);
	return ;
}

int main()
{
	int n,q;
	scanf("%d%d%s",&n,&q,c+1);
	for(int i=1;i<=n;i++)build(1,1,n,i);

	while(q--)
	{
		int x,l,r;
		scanf("%d%d%d",&x,&l,&r);
		if(x==1){
			if(c[l]==c[r])continue;
			swap(c[l],c[r]);
			build(1,1,n,l);
			build(1,1,n,r);
		} 
		else{
			int a[2]={0,0};
			quary(1,1,n,l,r,a);
			if(!a[0]&&!a[1])printf("Yes\n");
			else printf("No\n");
		}
	}
	return 0;
}




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李昌荣。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值