309108: CF1625E1. Cats on the Upgrade (easy version)

Memory Limit:256 MB Time Limit:6 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Cats on the Upgrade (easy version)

题意翻译

本题和 CF1625E2 的唯一区别在于,本题没有修改操作。 给定一个长度为 $n$ 的仅包含 `(`、`)` 的字符串 $s$。给出如下定义: - 定义某字符串是一个简单的**合法的括号序列**(以下简称 **RBS**),当且仅当我们可以通过若干次移除一个 `.` 或者一个连续的子串 `()` 的操作将这个字符串删空,且这个字符串不以 `.` 作为开头或者结尾。 - 定义 $s[l\dots r]$ 为字符串 $s$ 从第 $l$ 个字符开始到第 $r$ 个字符结束的子串。 一共有 $q$ 次询问,每次询问给定两个整数 $l,r$,你需要回答有多少个 $s[l\dots r]$ 的子串是简单的 RBS。换句话说,请求出满足 $l\leqslant i\leqslant j\leqslant r$ 且 $s[i\dots j]$ 是简单的 RBS 的 $(i,j)$ 的对数。**保证 $s[l\dots r]$ 是一个简单的 RBS**。 数据范围: - $2\leqslant n\leqslant 3\times 10^5$,$1\leqslant q\leqslant 3\times 10^5$。 - $1\leqslant l<r\leqslant n$。 Translated by Eason_AC 2022.1.19

题目描述

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. ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF1625E1/ad865cfdf37d1aee9e7ac138cc3da0f92a8cbe90.png)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: "(()(.))" $ \rightarrow $ "(()())" $ \rightarrow $ "(())" $ \rightarrow $ "()" $ \rightarrow $ "". 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 $ s $ as its sequential subsegment. In particular, $ s[l\dots r] = s_ls_{l+1}\dots s_r $ , where $ s_i $ is the $ i $ -th character of the string $ s $ . Now, move on to the problem statement itself. You are given a string $ s $ , initially consisting of characters "(" and ")". You need to answer the queries of the following kind. Given two indices, $ l $ and $ r $ ( $ 1 \le l < r \le n $ ), and it's guaranteed that the substring $ s[l\dots r] $ is a simple RBS. You need to find the number of substrings in $ s[l\dots r] $ such that they are simple RBS. In other words, find the number of index pairs $ i $ , $ j $ such that $ l \le i < j \le r $ and $ s[i\dots 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.

输入输出格式

输入格式


The first line contains two integers $ n $ and $ q $ ( $ 2 \le n \le 3\cdot10^5 $ , $ 1 \le q \le 3\cdot10^5 $ ), the length of the string, and the number of queries. The second line contains the string $ s $ , consisting of $ n $ characters "(" and ")". Each of the following $ q $ lines contains three integers $ t $ , $ l $ and $ r $ ( $ t = 2 $ , $ 1 \le l < r \le n $ ), the queries you need to answer. It is guaranteed that all the queries are valid and correspond to the problem statements. Note that $ t $ is unused and always equal to two in this problem. It is required for the hard version of the problem.

输出格式


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.

输入输出样例

输入样例 #1

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

输出样例 #1

3
4
1
6

说明

Consider the example test case. The answer to the first query is $ 3 $ , as there are three suitable substrings: $ s[3\dots6] $ , $ s[3\dots4] $ and $ s[5\dots6] $ . The answer to the second query is $ 4 $ . The substrings are $ s[3\dots6] $ , $ s[3\dots4] $ , $ s[5\dots6] $ and $ s[2\dots7] $ . The answer to the third query is $ 1 $ . The substring is $ s[8\dots9] $ . The answer to the fourth query is $ 6 $ . The substrings are $ s[3\dots6] $ , $ s[3\dots4] $ , $ s[5\dots6] $ , $ s[2\dots7] $ , $ s[8\dots9] $ and $ s[2\dots9] $ .

Input

题意翻译

本题和 CF1625E2 的唯一区别在于,本题没有修改操作。 给定一个长度为 $n$ 的仅包含 `(`、`)` 的字符串 $s$。给出如下定义: - 定义某字符串是一个简单的**合法的括号序列**(以下简称 **RBS**),当且仅当我们可以通过若干次移除一个 `.` 或者一个连续的子串 `()` 的操作将这个字符串删空,且这个字符串不以 `.` 作为开头或者结尾。 - 定义 $s[l\dots r]$ 为字符串 $s$ 从第 $l$ 个字符开始到第 $r$ 个字符结束的子串。 一共有 $q$ 次询问,每次询问给定两个整数 $l,r$,你需要回答有多少个 $s[l\dots r]$ 的子串是简单的 RBS。换句话说,请求出满足 $l\leqslant i\leqslant j\leqslant r$ 且 $s[i\dots j]$ 是简单的 RBS 的 $(i,j)$ 的对数。**保证 $s[l\dots r]$ 是一个简单的 RBS**。 数据范围: - $2\leqslant n\leqslant 3\times 10^5$,$1\leqslant q\leqslant 3\times 10^5$。 - $1\leqslant l<r\leqslant n$。 Translated by Eason_AC 2022.1.19

加入题单

上一题 下一题 算法标签: