A14820 | Cats on the Upgrade (hard version)
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
This is the hard 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:
"(()(.))" $\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 following queries:
1. Given two indices, $l$ and $r$ ( $1 \le l < r \le n$ ). It's guaranteed that the $l$ -th character is equal to "(", the $r$ -th character is equal to ")", and the characters between them are equal to ".". Then the $l$ -th and the $r$ -th characters must be set to ".".
2. 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.
"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:
"(()(.))" $\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 following queries:
1. Given two indices, $l$ and $r$ ( $1 \le l < r \le n$ ). It's guaranteed that the $l$ -th character is equal to "(", the $r$ -th character is equal to ")", and the characters between them are equal to ".". Then the $l$ -th and the $r$ -th characters must be set to ".".
2. 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.
输入格式
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 \in \{1, 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.
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 \in \{1, 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.
输出格式
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 8 )(()())() 2 3 6 2 2 7 1 3 4 2 2 7 2 2 9 1 5 6 1 2 7 2 8 9
输出 #1
3 4 2 4 1
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]$ .
After the third query, the string becomes ")(..())()".
The answer to the fourth query is $2$ . The substrings are $s[5\dots6]$ and $s[2\dots7]$ . Note that $s[3\dots6]$ is not a simple RBS anymore, as it starts with ".".
The answer to the fifth query is $4$ . The substrings are $s[5\dots6]$ , $s[2\dots7]$ , $s[8\dots9]$ and $s[2\dots9]$ .
After the sixth query, the string becomes ")(....)()".
After the seventh query, the string becomes ")......()".
The answer to the eighth query is $1$ . The substring is $s[8\dots9]$ .
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]$ .
After the third query, the string becomes ")(..())()".
The answer to the fourth query is $2$ . The substrings are $s[5\dots6]$ and $s[2\dots7]$ . Note that $s[3\dots6]$ is not a simple RBS anymore, as it starts with ".".
The answer to the fifth query is $4$ . The substrings are $s[5\dots6]$ , $s[2\dots7]$ , $s[8\dots9]$ and $s[2\dots9]$ .
After the sixth query, the string becomes ")(....)()".
After the seventh query, the string becomes ")......()".
The answer to the eighth query is $1$ . The substring is $s[8\dots9]$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted