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 ...
随机推荐
- 关于oracle卸载没有卸载完全的问题
1.关闭oracle所有的服务.可以在windows的服务管理器中关闭: 2.打开注册表:regedit 打开路径: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlS ...
- [Linked List]Remove Duplicates from Sorted List
Total Accepted: 90247 Total Submissions: 254602 Difficulty: Easy Given a sorted linked list, delete ...
- xshell4无法使用小键盘问题解决
今天新安装了一个Xshell4.0的客户端,登录linux服务器发现无法使用小键盘进行输入,后来把终端里的——终端类型 更改成linux后重新打开标签登录服务器,发现可正常使用.默认的终端类型为:xt ...
- 点击超链接执行js代码实现确认操作
如题,本次是要实现点击超链接实现执行js代码,并确认是否删除数据库数据,采用php. 首先链接数据库,查询数据库数据: 1: <?php 2: $dbms='mysql'; //数据库类型 ,对 ...
- ctype.h 字符分类与转换
函数及说明 1 int isalnum(int c)该函数检查传递的字符是否是字母数字. 2 int isalpha(int c)该函数是否传递的字符是字母. 3 int iscntrl(int ...
- ORM和Core
.net core中有哪些被抛弃的类 1.DataTable DataRow SqlDataAdapter DataRow DataColumn DataColumn 虽然这些类不是我ORM核心功能 ...
- BadgeView新提示开源工具类
BadgeView是使用某个图标作为新功能的提醒,类似于收到短息后短信图标的右上方有信息数目或者其他的显示性提示.BadgeView很好的实现了这个功能,而且进行了拓展,可自定义位置和提示图标. 工具 ...
- 几个关于JPEGLIB库的博客
1.http://blog.csdn.net/huxiangyang4/archive/2010/07/12/5728888.aspx 我认为是最好的 2.http://blog.csdn.net/a ...
- poj 1401---求N!末尾0的个数,2的个数一定比5多,观察得来,0的产生即为2*5,去找这个阶乘一行里面5的个数即可
#include<stdio.h> #include<stdlib.h> int main() { int T,N; while(scanf("%d",&a ...
- Spring流程
Spring Web Flow是Spring框架的子项目,作用是让程序按规定流程运行. 1 安装配置Spring Web Flow 虽然Spring Web Flow是Spring框架的子项目,但它并 ...