【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 ...
随机推荐
- 【数学 随机 技巧】cf364D. Ghd
随机化选讲的例题 John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positi ...
- python简单实用gunicorn部署
linux 安装 pyuthon 安装 pip install gunicorn manage.py 文件 from app import create_app app = create_app( ...
- goaccess实现实时监控
一.实现后台实时监控 goaccess -p /usr/local/etc/goaccess/goaccess.conf /var/log/nginx/access.log -a -o /usr/sh ...
- Python_三级目录
程序要求: 1. 使用字典存储 1. 可以一层一层的进入到所有层2. 可以在每层返回上一层3. 可以在任意层退出 三级目录写了两个版本,第一个版本是刚看完字典写出来的,代码很多冗余,很多重复. men ...
- tcl之string操作-match/map/大小写转换
- Python9-MySQL-Homework-day43
表结构 SET NAMES utf8; SET FOREIGN_KEY_CHECKS = 0; -- ---------------------------- -- Table structure f ...
- XX公司在线笔试题编程题之一
题目: #include <iostream> #include <vector> #include <string> #include <list> ...
- WebApi 跨域
http://www.cnblogs.com/lori/p/3557111.html http://bbs.csdn.net/topics/391020576
- leetcode 【 Find Peak Element 】python 实现
题目: A peak element is an element that is greater than its neighbors. Given an input array where num[ ...
- xml文件的生成
关于android中自定义xml文件的生成,请看示例代码(主要来源于黑马教程): import java.io.File; import java.io.FileNotFoundException; ...