http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809

给定一串平衡的序列,要求交换两个位置之后,问其是否还平衡。

首先要注意到交换的是两个位置,这两个位置没有大小之分,所以要判断是否swap他们,保持相对大小

然后如果我们把'('当成是1,把')'当成是-1,那么序列平衡,那么前缀和就是0了。

然后考虑下交换的时候,如果就交换相同的字符,那么肯定是Yes了,如果是')' 和 '(',那么也是Yes

因为本来序列就是平衡的,现在这样交换,只不过是把相对位置改了,比如()(),交换2和3,其中和2匹配的1,还是可以看成和交换后的那个字符匹配。

考虑交换的是'(' 和 ')'

因为本来在a位置(字符'(')的时候,前缀和是+1了的,那么现在换了个')'过去,所以[a, b - 1]这段区间的前缀和要-2

[b, lenstr]这段要+2

当然可以更新后,判断前缀和是否为0,+ 中途不能出现负数。

然后注意到只有-2那段区间才会产生负数,所以查询[a, b - 1](注意相对大小)的最小值,如果小于2就NO了

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#define root 1, n, 1
#define lson L, mid, cur << 1
#define rson mid + 1, R, cur << 1 | 1
int n, q;
const int maxn = 1e5 + ;
int mi[maxn << ];
int add[maxn << ];
char str[maxn];
int pre_sum[maxn];
void pushUp(int cur) {
mi[cur] = min(mi[cur << ], mi[cur << | ]);
}
void build(int L, int R, int cur) {
if (L == R) {
mi[cur] = pre_sum[L];
return;
}
int mid = (L + R) >> ;
build(lson);
build(rson);
pushUp(cur);
}
int query(int begin, int end, int L, int R, int cur) {
if (L >= begin && R <= end) { //子图
return mi[cur];
}
int mid = (L + R) >> ;
int lans = inf;
int rans = inf;
if (begin <= mid) {
lans = query(begin, end, lson);
}
if (end > mid) rans = query(begin, end, rson);
return min(lans, rans);
}
void work() {
cin >> str + ;
for (int i = ; str[i]; ++i) {
if (str[i] == '(') {
pre_sum[i] = pre_sum[i - ] + ;
} else pre_sum[i] = pre_sum[i - ] - ;
}
build(root);
for (int i = ; i <= q; ++i) {
int a, b;
cin >> a >> b;
if (a > b) swap(a, b);
if (str[a] == str[b] || str[a] == ')' && str[b] == '(') {
cout << "Yes" << endl;
} else {
int MI = query(a, b - , root);
// cout << MI << endl;
if (MI < ) {
cout << "No" << endl;
} else cout << "Yes" << endl;
}
}
} int main() {
#ifdef local
freopen("data.txt","r",stdin);
#endif
IOS;
while (cin >> n >> q) work();
return ;
}

湖南省2016省赛题。1809: Parenthesis 线段树的更多相关文章

  1. 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并

    题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...

  2. Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~

                                                                              1100 - Again Array Queries ...

  3. Hitcon 2016 Pwn赛题学习

    PS:这是我很久以前写的,大概是去年刚结束Hitcon2016时写的.写完之后就丢在硬盘里没管了,最近翻出来才想起来写过这个,索性发出来 0x0 前言 Hitcon个人感觉是高质量的比赛,相比国内的C ...

  4. 2016多校8th 1008【线段树-神题】

    题意: T N M N个数 M个操作 一个数组A, 有3个操作 1 l r x,a[l]-a[r]都+x 2 l r,a[i]=sqrt(a[i]),l<=i<=r 3 l r,求和,a[ ...

  5. Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)

    题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...

  6. 2017 ICPC西安区域赛 A - XOR (线段树并线性基)

    链接:https://nanti.jisuanke.com/t/A1607 题面:   Consider an array AA with n elements . Each of its eleme ...

  7. bzoj3307 雨天的尾巴题解及改题过程(线段树合并+lca+树上差分)

    题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式 第一行数字N,M接下 ...

  8. Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法

    目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门  Portal  原题目描述在最下面.  简单的 ...

  9. 2019牛客多校第八场 F题 Flowers 计算几何+线段树

    2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...

随机推荐

  1. hibernate入门(-)

    1.struts2的支持 在web.xml中配置struts2的支持 <?xml version="1.0" encoding="UTF-8"?> ...

  2. I.MX6 Surfaceflinger 机制

    /**************************************************************************** * I.MX6 Surfaceflinger ...

  3. codevs 2102 石子归并2

    传送门 2102 石子归并 2  时间限制: 10 s  空间限制: 256000 KB  题目等级 : 黄金 Gold   题目描述 Description 在一个园形操场的四周摆放N堆石子,现要将 ...

  4. C++之static类成员,static类成员函数

    0.static修饰类中成员,表示类的共享数据 1.static类成员 在C++primer里面说过,static类成员不像普通的类数据成员,static类数据成员独立于一切类对象处在.static类 ...

  5. c++之拷贝构造函数详解

    C++中经常使用一个常量或变量初始化另一个变量,例如: double x=5.0: double y=x; 使用类创建对象时,构造函数被自动调用以完成对象的初始化,那么能否象简单变量的初始化一样,直接 ...

  6. Snmp在Windows下的实现----WinSNMP编程原理

    在Windows 下实现SNMP协议的编程,可以采用Winsock接口,在161,162端口通过udp传送信息.在Windows 2000中,Microsoft已经封装了SNMP协议的实现,提供了一套 ...

  7. sql之函数及流程控制

    date_format函数

  8. app UI测试之UIAutomator

    执行UIAutomator测试步骤 1.新建Java项目,导入android.jar和uiautomator.jar包,继承UiAutomatorTestCase 2.生成编译文件 android c ...

  9. html css鼠标样式,鼠标形状

    css鼠标手型cursor中hand与pointer Example:CSS鼠标手型效果 <a href="#" style="cursor:hand"& ...

  10. windows ping得通,连接不上网

    这是被电脑安装的软件拦截的现象,我们只需要使用.   netsh winsock reset   重启电脑即可