题目链接

给一个括号序列, 两种操作。 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法。



线段树, 我们开两个数组lf, rg。 表示某个区间里面, 右边的左括号个数, 和左边的右括号个数。 ))(( 这个序列lf和rg就都是2. (())这样的话都是0.

如果合法, 那么lf, rg都等于0。

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <complex>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef complex <double> cmx;
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 30005;
int lf[maxn<<2], rg[maxn<<2];
string s;
void pushUp(int rt) {
int tmp = min(lf[rt<<1], rg[rt<<1|1]); //因为左边的左括号和右边的右括号也会组成合法的序列
lf[rt] = lf[rt<<1] + lf[rt<<1|1]-tmp; //所以这里需要减去新构成的合法的括号个数。
rg[rt] = rg[rt<<1] + rg[rt<<1|1]-tmp;
}
void build(int l, int r, int rt) {
if(l == r) {
if(s[l-1] == '(')
lf[rt] = 1;
else
rg[rt] = 1;
return ;
}
int m = l+r>>1;
build(lson);
build(rson);
pushUp(rt);
}
void update(int p, int l, int r, int rt) {
if(l == r) {
lf[rt] ^= 1;
rg[rt] ^= 1;
return ;
}
int m = l+r>>1;
if(p <= m)
update(p, lson);
else
update(p, rson);
pushUp(rt);
}
int main()
{
int n, m, x, cnt = 1;
while(cin>>n) {
cin>>s;
mem(lf);
mem(rg);
build(1, n, 1);
cin>>m;
printf("Test %d:\n", cnt++);
while(m--) {
scanf("%d", &x);
if(x) {
update(x, 1, n, 1);
} else {
if(lf[1]||rg[1]) {
puts("NO");
} else {
puts("YES");
}
}
}
}
return 0;
}

spoj BRCKTS - Brackets 线段树的更多相关文章

  1. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. Codeforces Round #223 (Div. 2) E. Sereja and Brackets 线段树区间合并

    题目链接:http://codeforces.com/contest/381/problem/E  E. Sereja and Brackets time limit per test 1 secon ...

  3. SPOJ - HORRIBLE 【线段树】

    思路 线段树 区间更新 模板题 注意数据范围 AC代码 #include <cstdio> #include <cstring> #include <ctype.h> ...

  4. Light Switching(SPOJ LITE)—— 线段树成段更新异或值

    题目连接:http://www.spoj.com/problems/LITE/en/. 题意:有若干个灯泡,每次对一段操作,这一段原先是亮的,就关了:原先是关着的,就打开.询问某一段的打开的灯泡的个数 ...

  5. CodeForces-380C:Sereja and Brackets(线段树与括号序列)

    Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...

  6. Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)

    You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defi ...

  7. SPOJ COT3 Combat on a tree(Trie树、线段树的合并)

    题目链接:http://www.spoj.com/problems/COT3/ Alice and Bob are playing a game on a tree of n nodes.Each n ...

  8. SPOJ 2916 Can you answer these queries V(线段树-分类讨论)

    题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...

  9. SPOJ 1557. Can you answer these queries II 线段树

    Can you answer these queries II Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 https://www.spoj.com/pr ...

随机推荐

  1. radio与checkbox

    最近一直在学习Javascript与asp.net MVC4,每天都在跟着书学习.这样总感觉自己看的很抽象,没有点实际的意义.而且,每次看的东西很容易忘记,所以打算在这里记录自己的学习笔记. Java ...

  2. 反编译C#的dll文件并修改,再重新生成dll

    1.把dll文件导入到ildasm工具中,ildasm是由微软提供的.net程序反编译工具,位于“C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A ...

  3. zendStudio安装Xdebug项目断点调试

    1,首先安装xdebug插件 传送门 2,配置php.ini文件如下: [XDebug] xdebug.profiler_append = xdebug.profiler_enable = xdebu ...

  4. 使用html,JavaScript,ajax写一个小型实例

    //1.创建受捐单位数组 var arrOrgData = [     { "Id": 1, "OrgName": "红十字会" },    ...

  5. jquery读取本地文件

    <html> <head> <script type="text/javascript" src="/jquery/jquery.js&qu ...

  6. Python之路第四天,基础(4)-装饰器,迭代器,生成器

    装饰器 装饰器(decorator)是一种高级Python语法.装饰器可以对一个函数.方法或者类进行加工.在Python中,我们有多种方法对函数和类进行加工,比如在Python闭包中,我们见到函数对象 ...

  7. leetcode 算法刷题(一)

    今天开始刷Leetcode上面的算法题.我会更新我刷题过程中提交的代码(成功和不成功的都有)和比较好的解法 第二题 Add Two Numbers 题目的意思:输入两个链表,这两个链表都是倒序的数字, ...

  8. 从缓冲上看阻塞与非阻塞socket在发送接收上的区别

    最近在网络上看到一些帖子以及回复,同时又搜索了一些网络上关于阻塞非阻塞区别的描述,发现很多人在描述两者的发送接收时操作返回以及缓冲区处理的区别时有不同程度的误解.所以我想写一篇文章来纠正错误,并作为记 ...

  9. javascript时间处理方法收集

    首先收集到的是一个给某一个时间对象增加一段时间的方法, 例如2026-05-11增加一个月的时间,增加后时间为2026-05-11, 代码如下: function DateAdd(interval,n ...

  10. SQL Server 获取服务器信息

    最近做了一个小工具,里面涉及到一些取SQL Server 服务器信息的一些东西,找了好久,找到一个不错的,贴出来分享. 系统函数 SERVERPROPERTY ( propertyname ) 包含要 ...