Codeforces 380C. Sereja and Brackets(模拟啊)
来源:互联网 发布:乐高编程机器人 编辑:程序博客网 时间:2024/05/16 08:07
题目链接:http://codeforces.com/problemset/problem/380/C
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consisting of characters "(" and ")".
Sereja needs to answer m queries, each of them is described by two integers li, ri (1 ≤ li ≤ ri ≤ n). The answer to the i-th query is the length of the maximum correct bracket subsequence of sequence sli, sli + 1, ..., sri. Help Sereja answer all queries.
You can find the definitions for a subsequence and a correct bracket sequence in the notes.
The first line contains a sequence of characters s1, s2, ..., sn (1 ≤ n ≤ 106) without any spaces. Each character is either a "(" or a ")". The second line contains integer m (1 ≤ m ≤ 105) — the number of queries. Each of the next m lines contains a pair of integers. The i-th line contains integers li, ri (1 ≤ li ≤ ri ≤ n) — the description of the i-th query.
Print the answer to each question on a single line. Print the answers in the order they go in the input.
())(())(())(71 12 31 21 128 125 112 10
00210466
A subsequence of length |x| of string s = s1s2... s|s| (where |s| is the length of string s) is string x = sk1sk2... sk|x|(1 ≤ k1 < k2 < ... < k|x| ≤ |s|).
A correct bracket sequence is a bracket sequence that can be transformed into a correct aryphmetic expression by inserting characters "1" and "+" between the characters of the string. For example, bracket sequences "()()", "(())" are correct (the resulting expressions "(1)+(1)", "((1+1)+1)"), and ")(" and "(" are not.
For the third query required sequence will be «()».
For the fourth query required sequence will be «()(())(())».
题意:
给出一个含‘ ( ’ ,‘ ) ' 的字符串,
然后给出一个询问区间,去这个区间里完整匹配的()的数目!一个()的答案为2!
PS:
我们可以用栈把每个’ ( ‘的下标存起来!然后再扫一遍’ )‘!用树状数组维护一下即可!
代码如下:
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <iostream>#include <stack>using namespace std;#define maxn 1000017#define maxm 100017using namespace std;struct node{ int l, r; int id;} q[maxm];char s[maxn];int c[maxn];int ans[maxm];stack<int> st;int len;int Lowbit(int x) // 2^k{ return x&(-x);}void update(int i, int x)//i点增量为x{ while(i <= len) { c[i] += x; i += Lowbit(i); }}int sum(int x)//区间求和 [1,x]{ int ans = 0; while(x) { ans += c[x]; x -= Lowbit(x); } return ans;}bool cmp(node a, node b){ return a.r < b.r;}int main(){ int m; while(~scanf("%s",s)) { int pos = 1; memset(c, 0, sizeof(c)); while(!st.empty()) { st.pop(); } len = strlen(s); scanf("%d",&m); for(int i = 0; i < m; i++) { scanf("%d%d",&q[i].l,&q[i].r); q[i].id = i; } sort(q, q+m, cmp); for(int i = 0; i < m; i++) { for(int j = pos; j <= q[i].r; j++) { if(s[j-1] == '(') { st.push(j); } else if(s[j-1] == ')') { if(!st.empty()) { int tt = st.top(); update(tt, 2); st.pop(); } } } pos = q[i].r+1; ans[q[i].id] = sum(q[i].r) - sum(q[i].l-1); } for (int i = 0; i < m; i++) { printf("%d\n", ans[i]); } } return 0;}/*())(())(())(71 11 22 32 105 111 128 12*/
- Codeforces 380C. Sereja and Brackets(模拟啊)
- Codeforces 380 C. Sereja and Brackets
- Codeforces 380C Sereja and Brackets
- Codeforces 380C Sereja and Brackets(线段树)
- Codeforces Round 223 380C Sereja and Brackets 树状数组
- Codeforces 380C - Sereja and Brackets (线段树括号匹配)
- Codeforces 380C. Sereja and Brackets【线段树】
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- Codeforces Round #223 (Div. 1) C. Sereja and Brackets
- Codeforces 381C Sereja and Brackets【二分+思维】好题~
- CodeForces - 368C Sereja and Algorithm (找规律&模拟)
- codeforces 381E Sereja and Brackets(线段树)
- Codeforces 381E Sereja and Brackets(线段树)
- Codeforces 380A Sereja and Prefixes(模拟)
- CF 380C - Sereja and Brackets(线段树)
- Coderforce 380 C. Sereja and Brackets(线段树)
- C. Sereja and Brackets----线段树
- codeforces Round #223 (Div.1) C. Sereja and Brackets【线段树】
- HDU 2473-Junk-Mail Filter-并查集删除
- HDU 1061 Rightmost Digit
- sscanf用法详解
- Tcp通信中服务器处理客户端意外断开 / 10054
- poj3662 二分+spfa
- Codeforces 380C. Sereja and Brackets(模拟啊)
- jquery 元素控制(追加元素/追加内容)介绍及应用
- 新浪微博新兵训练营系列课程——平台RPC框架介绍
- 导航栏和状态栏的颜色设置
- 在界面条件检索时设置默认时间
- linux cp命令批量复制不提示方法
- iOS中设置view的边界
- Java IO完全总结
- 一行代码比五百行架构说明更有价值