题意:给出一个括号序列,要求维护两种操作:

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(线段树)的更多相关文章

  1. CF380C. Sereja and Brackets[线段树 区间合并]

    C. Sereja and Brackets time limit per test 1 second memory limit per test 256 megabytes input standa ...

  2. 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 ...

  3. spoj BRCKTS - Brackets 线段树

    题目链接 给一个括号序列, 两种操作. 一种将某个位置的括号变反(左变右, 右变左), 第二种是询问这个括号序列是否合法. 线段树, 我们开两个数组lf, rg. 表示某个区间里面, 右边的左括号个数 ...

  4. CodeForces-380C:Sereja and Brackets(线段树与括号序列)

    Sereja has a bracket sequence s1, s2, ..., sn, or, in other words, a string s of length n, consistin ...

  5. Sereja and Brackets CodeForces - 380C (线段树+分治思路)

    Sereja and Brackets 题目链接: CodeForces - 380C Sereja has a bracket sequence s1, s2, ..., *s**n, or, in ...

  6. (线段树 && 字符串的处理)codeforces -- 570C

    链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87813#problem/J Description Daniel has a s ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

随机推荐

  1. redhat linux6.5升级openssh

    1.下载最新的openssh包 http://www.openssh.com/portable.html#http 2.升级openssh之前要先打开服务器telnet,通过telnet登录服务器,因 ...

  2. Q&A - ABTesting是啥?

    举个简单的例子,当你有一个日IP过千的网站,而你的网站首页几百年没有更改了,这个时候你想启用新的网页,而你有害怕新的页面用户不一定就非常喜欢,那么这个时候你就需要进行A/B测试了.测试的方法是将老页面 ...

  3. 安装配置mysql图文步骤以及配置mysql的环境变量的步骤

    MySQL下载地址:http://dev.mysql.com/downloads/installer/ 我的数据库是5.5.21这个版本的.其实可以一直点击next,直到出现第14张图,从这里开始要注 ...

  4. Fight Against Traffic -简单dijkstra算法使用

    题目链接 http://codeforces.com/contest/954/problem/D 题目大意 n m s t 分别为点的个数, 边的个数,以及两个特殊的点 要求s与t间的距离在新增一条边 ...

  5. Linux入门-第九周

    1.判断UID是否大于等于500,如果为真就显示为普通用户,如果为假就显示为系统或管理用户 AWK简介:awk是一个强大的文本分析工具,相对于grep的查找,sed的编辑,awk在其对数据分析并生成报 ...

  6. 笔记-python tutorial-9.classes

    笔记-python tutorial-9.classes 1.      Classes 1.1.    scopes and namespaces namespace: A namespace is ...

  7. 教你如何在 Javascript 文件里使用 .Net MVC Razor 语法

    摘录 文章主要是介绍了通过一个第三方类库RazorJS,实现Javascript 文件里使用 .Net MVC Razor 语法,很巧妙,推荐给大家 相信大家都试过在一个 View 里嵌套使用 jav ...

  8. 「微信小程序免费辅导教程」24,基础内容组件icon的使用探索与7月26日微信公众平台的更新解读

  9. 产生指定时间区间序列、按指定单位变化时间 python实现

    示例1:给定起始日期和结束日期,如何得到中间的时间序列 import datetime def dateRange(beginDate, endDate): dates = [] dt = datet ...

  10. luogu2093 [国家集训队]JZPFAR

    题面不符?-- #include <algorithm> #include <iostream> #include <cstdio> using namespace ...