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

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. Uva 长城守卫——1335 - Beijing Guards

    二分查找+一定的技巧 #include<iostream> using namespace std; +; int n,r[maxn],Left[maxn],Right[maxn];//因 ...

  2. Java - 网络

    要事为先,你如果想要在这个行业发展下去的话,实际上三角形的三个点在支撑着你发展,一个是技术.一个是管理(不是说管理别人,是管理你自己的时间,管理你自己的精力).还有一个就是沟通,注重这三点均衡的发展. ...

  3. mysql基础,数据表的类型

  4. k8s基于RBAC的访问控制(用户授权)

    kubernetes的API Server常用的授权插件有:   Node.ABAC.RBAC.Webhook我们重点说一下RBAC的访问控制逻辑RBAC(Role base access contr ...

  5. php 获取 今天、昨天、这周、上周、这月、上月、近30天

    <?php //今天 $today = date("Y-m-d"); //昨天 $yesterday = date("Y-m-d", strtotime( ...

  6. 为什么选择Redis

    1)Redis不仅仅支持简单的k/v类型的数据,同时还提供list,set,zset,hash等数据结构的存储.    2)Redis支持master-slave(主-从)模式应用    3)Redi ...

  7. 42.VUE学习之--组件之子组件使用$on与$emit事件触发父组件实现购物车功能

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. BFS:Open and Lock(一个数的逐位变化问题的搜索)

    解体心得: 1.关于定义四维数组的问题,在起初使用时,总是在运行时出错,找了很多方法,最后全部将BFS()部分函数写在主函数中,将四维数组定义在主函数中才解决了问题.运行成功后再次将四维数组定义为全局 ...

  9. HDU1272小希的迷宫

    小希的迷宫 上次Gardon的迷宫城堡小希玩了很久(见Problem B),现在她也想设计一个迷宫让Gardon来走.但是她设计迷宫的思路不一样,首先她认为所有的通道都应该是双向连通的,就是说如果有一 ...

  10. 扩展程序 - Google Chrome

    Adblock Plus 3.0.3 Adblock Plus 已被超过 1 亿台设备使用,是世界上最受欢迎的广告拦截软件. ID:cfhdojbkjhnklbpkdaibdccddilifddb 查 ...