【CSU1809】Parenthesis(线段树 + 贪心 + 前缀和)

本文介绍了一道关于括号匹配的问题,并提供了一种利用线段树进行优化的有效解决方案。通过分析不同情况下括号序列的变化,文章详细解释了如何判断交换括号后序列的合法性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一个菜逼的成长。。

题目大意:
给你一个长度为n的匹配的括号序列,有q次询问,每次询问交换a,b的位置,问交换后的序列是否仍然匹配。

题解:
这种括号的题目有一种经典的套路就是遇到’(‘加一,遇到’)’减一,这样整个序列最后的前缀和一定是非负的。
贪心一下,判断一下四种情况:
1.2),交换的两个括号是一样的,自然是输出Yes;
3),如果左边是’)’右边是’(‘,更加保证前缀和非负,也是输出Yes;
4),如果左边是’(‘右边是’)’,处理前缀和时扫描到左边的位置时,交换前是加1,交换后变成了减1,这样来回少了2,所以从a位置往后到b-1的位置都要减2,所以判断[a,b-1]中是否有小于2的,有则输出No,否则输出Yes。
在b位置是加了2.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdlib>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <list>
#include <deque>
#include <cctype>
#include <bitset>
#include <cmath>
using namespace std;
#define ALL(v) (v).begin(),(v).end()
#define cl(a) memset(a,0,sizeof(a))
#define bp __builtin_popcount
#define pb push_back
#define fin freopen("D://in.txt","r",stdin)
#define fout freopen("D://out.txt","w",stdout)
#define lson t<<1,l,mid
#define rson t<<1|1,mid+1,r
#define seglen (node[t].r-node[t].l+1)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
template <typename T>
inline void read(T &x){
    T ans=0;
    char last=' ',ch=getchar();
    while(ch<'0' || ch>'9')last=ch,ch=getchar();
    while(ch>='0' && ch<='9')ans=ans*10+ch-'0',ch=getchar();
    if(last=='-')ans=-ans;
    x = ans;
}
inline bool DBread(double &num)
{
    char in;double Dec=0.1;
    bool IsN=false,IsD=false;
    in=getchar();
    if(in==EOF) return false;
    while(in!='-'&&in!='.'&&(in<'0'||in>'9'))
        in=getchar();
    if(in=='-'){IsN=true;num=0;}
    else if(in=='.'){IsD=true;num=0;}
    else num=in-'0';
    if(!IsD){
        while(in=getchar(),in>='0'&&in<='9'){
            num*=10;num+=in-'0';}
    }
    if(in!='.'){
        if(IsN) num=-num;
            return true;
    }else{
        while(in=getchar(),in>='0'&&in<='9'){
                num+=Dec*(in-'0');Dec*=0.1;
        }
    }
    if(IsN) num=-num;
    return true;
}
template <typename T>
inline void write(T a) {
    if(a < 0) { putchar('-'); a = -a; }
    if(a >= 10) write(a / 10);
    putchar(a % 10 + '0');
}
/******************head***********************/
const int maxn = 100000 + 10;
char str[maxn];
int sum[maxn],ans;
struct Node{
    int l,r,mn;
}node[maxn * 4];
void pushup(int t)
{
    node[t].mn = min(node[t<<1].mn,node[t<<1|1].mn);
}
void build(int t,int l,int r)
{
    node[t].l = l;
    node[t].r = r;
    node[t].mn = 0;
    if(l == r){
        node[t].mn = sum[l];
        return ;
    }
    int mid = ( l + r ) >> 1;
    build(lson);
    build(rson);
    pushup(t);
}
void Query(int t,int l,int r)
{
    if(node[t].l == l && node[t].r == r){
        ans = min(ans,node[t].mn);
        return ;
    }
    int mid = (node[t].l + node[t].r) >> 1;
    if(l > mid)Query(t<<1|1,l,r);
    else if(r <= mid)Query(t<<1,l,r);
    else {
        Query(t<<1,l,mid);
        Query(t<<1|1,mid+1,r);
    }
}
int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q)){
        scanf("%s",str+1);
        for( int i = 1; i <= n; i++ ){
            if(str[i] == '(')sum[i] = sum[i-1] + 1;
            else sum[i] = sum[i-1] - 1;
        }
        build(1,1,n);
        int l,r;
        while(q--){
            scanf("%d%d",&l,&r);
            if(l > r)swap(l,r);
            if(str[l] == str[r] || (str[l] == ')' && str[r] == '('))puts("Yes");
            else {
                ans = INF;
                Query(1,l,r-1);
                if(ans < 2)puts("No");
                else puts("Yes");
            }
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值