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 ...
随机推荐
- .net 加密错误:填充无效,无法移除
今天用System.Security.Cryptography加密.使用了AesManaged,报错:填充无效,无法移除.分析是解密失败,密文损坏,或者KEY,IV不正确. using (AesMan ...
- java工厂类与反射机制
java 简单工厂类 2012-04-22 15:44:07| 分类: java | 标签:java工厂类 简单工厂类 |举报|字号 订阅 简单工厂模式需要由以下角色组成: 接口 ...
- 自定义不等高的cell-(storyboard)
对比自定义等高cell,需要几个额外的步骤(iOS8开始才支持) 添加子控件和contentView之间的间距约束 设置tableViewCell的真实行高和估算行高 // 告诉tableView所有 ...
- DOS下导入dmp文件到Oracle数据库
imp usename/password@SID full=y file= d:\data\xxxx.dmp ignore=y imp system/manager file=bible_db lo ...
- ExecutorService 的理解与使用
ExecutorService 的理解与使用 http://my.oschina.net/bairrfhoinn/blog/177639 Java线程池ExecutorService http:// ...
- OpenProcess() 函数
方法名称:OpenProcess 位置:Kernel32.dll OpenProcess 函数用来打开一个已存在的进程对象,并返回进程的句柄. 使用外部函数首先需导入命名空间: System.Runt ...
- 多个不同的表合并到一个datatable中,repeater在绑定datatable
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI ...
- python threading 模块来实现多线程
以多线程的方式向标准输出打印日志 #!/usr/bin/python import time import threading class PrintThread(threading.Thread): ...
- USB2.0的基本学习
SB2.0是在1.0的基础上于2000年提出来的,在1.0的基础上曾加了480Mbps的数据传输率.USB2.0具有以下的优点: 1.每个USB系统中有一个主机,通过级联的方式连接多个外部设备,最多可 ...
- Delphi XE6 通过JavaScript API调用百度地图
参考昨天的内容,有朋友还是问如何调用百度地图,也是,谁让咱都在国内呢,没办法,你懂的. 首先去申请个Key,然后看一下百度JavaScript的第一个例子:http://developer.baidu ...