【SPOJ61】Brackets(线段树)
题意:给出一个括号序列,要求维护两种操作:
1.将第x位上的括号取反
2.查询当前整个括号序列是否匹配
n<=3e4
思路:线段树维护区间内没有匹配的左右括号数量
pushup时t[p].r=t[rs].r+t[ls].r-min(t[ls].l,t[rs].r)
不知道这个式子怎么推出来的,但在四种情况下它都成立
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
#define fi first
#define se second
#define MP make_pair
#define N 210000
#define M 7010
#define eps 1e-8
#define pi acos(-1)
#define oo 1e9
#define MOD 10007 struct node
{
int l,r;
}t[N];
char a[N];
int n; void pushup(int p)
{
int ls=p<<;
int rs=ls+;
t[p].l=t[ls].l+t[rs].l-min(t[ls].l,t[rs].r);
t[p].r=t[rs].r+t[ls].r-min(t[ls].l,t[rs].r);
} void build(int l,int r,int p)
{
t[p].l=t[p].r=;
if(l==r)
{
if(a[l]=='(') t[p].l=;
else t[p].r=;
return;
}
int mid=(l+r)>>;
build(l,mid,p<<);
build(mid+,r,p<<|);
pushup(p);
} void update(int l,int r,int x,int p)
{
if(l==r)
{
t[p].l=-t[p].l;
t[p].r=-t[p].r;
return;
}
int mid=(l+r)>>;
if(x<=mid) update(l,mid,x,p<<);
else update(mid+,r,x,p<<|);
pushup(p);
} int main()
{
//freopen("spoj61.in","r",stdin);
//freopen("spoj61.out","w",stdout);
int cas=;
while(scanf("%d",&n)!=EOF)
{
cas++;
printf("Test %d:\n",cas);
scanf("%s",a+);
int m;
scanf("%d",&m);
build(,n,);
for(int i=;i<=m;i++)
{
int x;
scanf("%d",&x);
if(x==)
{
if(t[].l==&&t[].r==) printf("YES\n");
else printf("NO\n");
}
else update(,n,x,);
}
}
return ;
}
【SPOJ61】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 BRCKTS - Brackets 线段树
题目链接 给一个括号序列, 两种操作. 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法. 线段树, 我们开两个数组lf, rg. 表示某个区间里面, 右边的左括号个数 ...
- CodeForces-380C:Sereja and Brackets(线段树与括号序列)
Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...
- Sereja and Brackets CodeForces - 380C (线段树+分治思路)
Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...
- (线段树 && 字符串的处理)codeforces -- 570C
链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/J Description Daniel has a s ...
- Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟
E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works o ...
- Codeforces Round #603 (Div. 2) E. Editor 线段树
E. Editor The development of a text editor is a hard problem. You need to implement an extra module ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
随机推荐
- advanced regression to predict housing prices
https://docs.google.com/presentation/d/e/2PACX-1vQGlXP6QZH0ATzXYwnrXinJcCn00fxCOoEczPAXU-n3hAPLUfMfi ...
- I am too vegetable to all kill the 51nod problems on level 2 and 3.
51nod level 2:50/51 剩的一个题是切比雪夫距离转曼哈顿距离,现学的,bzoj3710过了,51nod上全wa了,很迷,可能有坑⑧. level 3:62/68 之前有的题有思路但是不 ...
- 1207: [HNOI2004]打鼹鼠
Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4154 Solved: 1997[Submit][Status][Discuss] Descript ...
- Java中json前后端日期传递处理
这里推荐2种方式 依赖包 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifa ...
- PyCharm 2018.1 软件汉化
下载汉化包 链接: https://pan.baidu.com/s/1buLFINImW_3cNzP8HsB4cA 密码: fqpu 安装汉化包 找到pycharm安装目录 直接把刚刚下载的汉化包复制 ...
- Python9-MySQL数据库安装及基本操作-day42
MySQL 单机程序(自己DB) 单机程序(共用DB)MySQL:用于管理文件的一个软件 -服务端软件 -socket服务端 -本地文件操作 -解析指令[SQL语句] -客户端软件(各种各样) -so ...
- 动态规划:HDU1712-ACboy needs your help(分组背包问题)
ACboy needs your help Time Limit : 1000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Othe ...
- CSU 1997-Seating Arrangement
Seating Arrangement Mr. Teacher老师班上一共有n个同学,编号为1到n. 在上课的时候Mr. Teacher要求同学们从左至右按1, 2, -, n的顺序坐成一排,这样每个 ...
- 《鸟哥的Linux私房菜》学习笔记(0)——磁盘与文件系统管理
一.Linux的登陆流程 login: 用户名:每个用户名都有一个用户ID(用户标识符),计算机处理的就是用户ID(数字)而不是用户名(字符),. 认证机制:Authentication,比如密码或者 ...
- Python 性能分析工具简介
Table of Contents 1. 性能分析和调优工具简介 1.1. Context Manager 1.2. Decorator 1.3. 系统自带的time命令 1.4. python ti ...