spoj BRCKTS - Brackets 线段树
题目链接
给一个括号序列, 两种操作。 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法。
线段树, 我们开两个数组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 线段树的更多相关文章
- CF380C. Sereja and Brackets[线段树 区间合并]
C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...
- 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 ...
- SPOJ - HORRIBLE 【线段树】
思路 线段树 区间更新 模板题 注意数据范围 AC代码 #include <cstdio> #include <cstring> #include <ctype.h> ...
- Light Switching(SPOJ LITE)—— 线段树成段更新异或值
题目连接:http://www.spoj.com/problems/LITE/en/. 题意:有若干个灯泡,每次对一段操作,这一段原先是亮的,就关了:原先是关着的,就打开.询问某一段的打开的灯泡的个数 ...
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- 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 ...
- 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 ...
- SPOJ 2916 Can you answer these queries V(线段树-分类讨论)
题目链接:http://www.spoj.com/problems/GSS5/ 题意:给出一个数列.每次查询最大子段和Sum[i,j],其中i和j满足x1<=i<=y1,x2<=j& ...
- 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 ...
随机推荐
- java调用Command命令
----------- import java.io.BufferedReader; import java.io.InputStreamReader; /** * 此类用来执行Command命令 * ...
- Android 开发中的View事件监听机制
在开发过程中,我们常常根据实际的需要绘制自己的应用组件,那么定制自己的监听事件,及相应的处理方法是必要的.我们都知道Android中,事件的监听是基于回调机制的,比如常用的OnClick事件,你了解它 ...
- 逗号" , "表达式
概述 逗号操作符 对它的每个操作对象求值(从左至右),然后返回最后一个操作对象的值. 语法 expr1, expr2, expr3... 参数 expr1, expr2, expr3... 任一表达式 ...
- 关于Eclipse的编码配置和字体大小设置
编码设置 1.工作空间编码:Window-->Preferences-->General-->Workspace 2.工程文件编码:项目-->Properties-->R ...
- php之文件基础操作
在php中对文件的基础操作非常的简单,php提供的函数粗略的用了一遍. file_get_contents():可以获取文件的内容获取一个网络资源的内容,这是php给我封装的一个比较快捷的读取文件的内 ...
- QueryString和BASE64
加号(+)是BASE64编码的一部分,而加号在QueryString中被当成是空格.因此,当一个含有BASE64编码的字符串直接作为URL的一部分时,如果其中含有加号,则使用QueryString读取 ...
- jQuery学习-事件之绑定事件(七)
今天来说说事件中的handlers方法中的一个片段 1 matches[ sel ] = handleObj.needsContext ? 2 jQuery( sel, this ).in ...
- Invalidate(TRUE)与Invalidate(FALSE)区别(前者会发送WM_ERASEBKGND消息全部刷新,然后使用WM_PAINT消息绘制,而后者只发送WM_PAINT消息)
使用Invalidate(TRUE)函数时,它会向消息队列中添加了WM_ERASEBKGND和WM_PAINT两个消息. 使用Invalidate(FALSE)函数时,它只会向消息队列中添加了WM_P ...
- C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0
原文 C#中通过Process启动的外部第三方程序MainWindowHandle句柄为0 前几天遇到了一个在C#的winform程序中,启动一个第三方jar程序,并修改jar运行窗体的标题的问题. ...
- ArcGIS学习推荐基础教程摘录
###########-------------------摘录一--------------------------########### ***************************** ...