题目链接

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



线段树, 我们开两个数组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. WindowsForm 记事本 对话框

    textbox:     属性:         text:文本         selectedtext:获取或设置选中文本         canundo:是否能够撤销     方法:       ...

  2. js中添加事件 attachEvent 与 addEventListener

    给元素添加事件时,使用js进行实现时产生了疑惑,有关事件浏览器兼容的问题,在此记录如下. <!DOCTYPE html> <html> <head> <met ...

  3. 在Win7的IIS上搭建FTP服务及用户授权

    FTP服务 FTP是文件传输协议(File Transfer Protocol)的简称,该协议属于应用层协议(端口号通常为21),用于Internet上的双向文件传输(即文件的上传和下载).在网络上有 ...

  4. 泛型编程中的Concept, Model和Policy

    A crude explanation Concept A set of requirements on a type, e.g. a RandomAccessible concept require ...

  5. com.sun.jdi.InvocationException occurred invoking method.

    文章来源于网络, 自己也遇到同样的问题,也是采用这样的方式解决的.原文链接http://zuiyanwangyue.iteye.com/blog/470638 在 HibernateDaoSuppor ...

  6. Android 技巧记录

    1.取消EditText自动获取焦点 在项目中,一进入一个页面, EditText默认就会自动获取焦点,弹出输入法界面,很不友好.那么如何取消这个默认行为呢? 解决之道:在EditText的父级控件中 ...

  7. AsyncTask 不能与Thread.sleep()同时使用解决方案

    public class MainActivity extends Activity { private ImageView iv_ads; String urrstrString = "h ...

  8. phpcms v9 万能字段使用

    <input type="text" name="info[down]" id="down" value="{FIELD_V ...

  9. yum 安装软件时报Public key for * is not installed

    这个是由于没有导入rpm签名信息引起的 解决方案: rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

  10. JVM内存分配和回收

    本文内容来自<Java编程思想(第四版)>第二章<一切都是对象>和第五章<初始化与清理>.作为一个使用了好几年的Javaer,再次看编程思想的前面章节(不要问我为什 ...