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

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. 牛客小白月赛5 F 圆(circle) 【欧拉定理】

    题目连接: https://www.nowcoder.com/acm/contest/135/F 签到题来了,送你们一个Python秒的题. Apojacsleam来到了OI大陆,经过了连年征战,成为 ...

  2. Spring Framework(框架)整体架构 变迁

    Spring Framework(框架)整体架构 2018年04月24日 11:16:41 阅读数:1444 标签: Spring框架架构 更多 个人分类: Spring框架   版权声明:本文为博主 ...

  3. redis Connection refused 远程连接错误

    redis 远程连接时报错:  Exception in thread "main" redis.clients.jedis.exceptions.JedisConnectionE ...

  4. 传送流(TS)的基础知识

    数字电视的TS包和TS流的组成和功能 综合考虑几下几个因素: (1)包的长度不能过短,否则包头开销所占比例过大, 导致传输效率下降 (2)包的长度不能过长,否则在丢失同步的情况下恢复同步的 周期过长, ...

  5. 图论:HDU2544-最短路(最全、最经典的最短路入门及小结)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  6. POJ3436------ACM Computer Factory

    题目链接 ACM Computer Factory Description As you know, all the computers used for ACM contests must be i ...

  7. Spring.Net初认识——竹子整理

    留个脚印,过两天总结. 看到知乎上有人对于DI|IOC 的解释,满不错,收藏下先 作者:OneNoodle链接:http://www.zhihu.com/question/23277575/answe ...

  8. js 实现5秒倒计时后跳转页面

    <script type="text/javascript"> function countDown(secs, surl) { var jumpTo = docume ...

  9. loj2035 「SDOI2016」征途

    学了斜率优化这题就能一气呵成地做出来啦qwqqwq #include <iostream> #include <cstdio> using namespace std; typ ...

  10. sql注入过滤了#,--+怎么办

    题目是NCTF2018的web题目 第一段是错误的思路,第二段是晚上有思考后发现的直接看第二段吧. ① ?id=1'会直接出来报错提示. 猜测使用单引号保护id. 另外一打空格就提示you hacke ...