Cats on the Upgrade (easy version)

15 篇文章 0 订阅
2 篇文章 0 订阅

This is the easy version of the problem. The only difference between the easy and the hard versions are removal queries, they are present only in the hard version.

"Interplanetary Software, Inc." together with "Robots of Cydonia, Ltd." has developed and released robot cats. These electronic pets can meow, catch mice and entertain the owner in various ways.

The developers from "Interplanetary Software, Inc." have recently decided to release a software update for these robots. After the update, the cats must solve the problems about bracket sequences. One of the problems is described below.

First, we need to learn a bit of bracket sequence theory. Consider the strings that contain characters "(", ")" and ".". Call a string regular bracket sequence (RBS), if it can be transformed to an empty string by one or more operations of removing either single "." characters, or a continuous substring "()". For instance, the string "(()(.))" is an RBS, as it can be transformed to an empty string with the following sequence of removals:

"(()(.))" →→ "(()())" →→ "(())" →→ "()" →→ "".

We got an empty string, so the initial string was an RBS. At the same time, the string ")(" is not an RBS, as it is not possible to apply such removal operations to it.

An RBS is simple if this RBS is not empty, doesn't start with ".", and doesn't end with ".".

Denote the substring of the string ss as its sequential subsegment. In particular, s[l…r]=slsl+1…srs[l…r]=slsl+1…sr, where sisi is the ii-th character of the string ss.

Now, move on to the problem statement itself. You are given a string ss, initially consisting of characters "(" and ")". You need to answer the queries of the following kind.

Given two indices, ll and rr (1≤l<r≤n1≤l<r≤n), and it's guaranteed that the substring s[l…r]s[l…r] is a simple RBS. You need to find the number of substrings in s[l…r]s[l…r] such that they are simple RBS. In other words, find the number of index pairs ii, jj such that l≤i<j≤rl≤i<j≤r and s[i…j]s[i…j] is a simple RBS.

You are an employee in "Interplanetary Software, Inc." and you were given the task to teach the cats to solve the problem above, after the update.

Note that the "." character cannot appear in the string in this version of the problem. It is only needed for the hard version.

Input

The first line contains two integers nn and qq (2≤n≤3⋅1052≤n≤3⋅105, 1≤q≤3⋅1051≤q≤3⋅105), the length of the string, and the number of queries.

The second line contains the string ss, consisting of nn characters "(" and ")".

Each of the following qq lines contains three integers tt, ll and rr (t=2t=2, 1≤l<r≤n1≤l<r≤n), the queries you need to answer. It is guaranteed that all the queries are valid and correspond to the problem statements. Note that tt is unused and always equal to two in this problem. It is required for the hard version of the problem.

Output

For each query, print a single integer in a separate line, the number of substrings that are simple RBS. The answers must be printed in the same order as the queries are specified in the input.

Example

input

Copy

9 4
)(()())()
2 3 6
2 2 7
2 8 9
2 2 9

output

Copy

3
4
1
6

Note

Consider the example test case.

The answer to the first query is 33, as there are three suitable substrings: s[3…6]s[3…6], s[3…4]s[3…4] and s[5…6]s[5…6].

The answer to the second query is 44. The substrings are s[3…6]s[3…6], s[3…4]s[3…4], s[5…6]s[5…6] and s[2…7]s[2…7].

The answer to the third query is 11. The substring is s[8…9]s[8…9].

The answer to the fourth query is 66. The substrings are s[3…6]s[3…6], s[3…4]s[3…4], s[5…6]s[5…6], s[2…7]s[2…7], s[8…9]s[8…9] and s[2…9]s[2…9].

 

思路:会发现同级别得括号是1加到n的和,不同级别之间是加法。

记录同级别:与它相邻数的个数+1.

前缀和:只要传入同类数,便能计算集合数。如果要去掉可以减去去掉前的数的大小×剩余个数。假设有5个同级别括号,去掉前3个,四和五都要减去3,就是剩余集合数。

字符串:想要字符串从1开始,要定义成char类型,不能定义成string。直接输入字符串,比单个字符输入要块的多(字符多的时候),用%s,不用%c。%s,s+1直接从1开始(3e5级别时,单个字符输入已经爆炸了)。

字符串长度(数量)未知时才会用string,知道长度直接定义char字符数组。

#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
#include <map>
#include <stack>
#include <cstdio>

using namespace std;

typedef long long ll;
const int N=3e5+10;
int n,q;
char s[N];
//mp<int,vector<int>>mp;
stack<int>sta;
int p[N];
ll sum[N];

int main()
{
    //cin>>n>>q;
    scanf("%d %d",&n,&q);
    //for(int i=1;i<=n;i++)cin>>s[i];
    //cin>>s;
    scanf("%s",s+1);
    for(int i=1;i<=n;i++)
    {
        if(s[i]=='(')sta.push(i);
        else if(sta.size())
        {
            p[i]=p[sta.top()-1]+1;//p[i]计算的是与它并列的括号数量
            sta.pop();
        }
        sum[i]=sum[i-1]+p[i];//前缀和相当于组合数,前面有3(i)块的组合数。与组合数的加法(相/反)由外向内
    }
    while(q--)
    {
        int t,l,r;
        //cin>>t>>l>>r;
        scanf("%d %d %d",&t,&l,&r);
        printf("%lld\n",sum[r]-sum[l-1]-p[l-1]*(p[r]-p[l-1]));//得到剩余个数*减小的大小
        //cout<<sum[r]-sum[l-1]-p[l-1]*(p[r]-p[l-1])<<endl;
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值