湖南省2016省赛题。1809: Parenthesis 线段树
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 线段树的更多相关文章
- 2017西安区域赛A / UVALive - 8512 线段树维护线性基合并
题意:给定\(a[1...n]\),\(Q\)次询问求\(A[L...R]\)的异或组合再或上\(K\)的最大值 本题是2017的西安区域赛A题,了解线性基之后你会发现这根本就是套路题.. 只要用线段 ...
- Light oj-1100 - Again Array Queries,又是这个题,上次那个题用的线段树,这题差点就陷坑里了,简单的抽屉原理加暴力就可以了,真是坑~~
1100 - Again Array Queries ...
- Hitcon 2016 Pwn赛题学习
PS:这是我很久以前写的,大概是去年刚结束Hitcon2016时写的.写完之后就丢在硬盘里没管了,最近翻出来才想起来写过这个,索性发出来 0x0 前言 Hitcon个人感觉是高质量的比赛,相比国内的C ...
- 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[ ...
- Codeforces Round #271 (Div. 2) E题 Pillars(线段树维护DP)
题目地址:http://codeforces.com/contest/474/problem/E 第一次遇到这样的用线段树来维护DP的题目.ASC中也遇到过,当时也非常自然的想到了线段树维护DP,可是 ...
- 2017 ICPC西安区域赛 A - XOR (线段树并线性基)
链接:https://nanti.jisuanke.com/t/A1607 题面: Consider an array AA with n elements . Each of its eleme ...
- bzoj3307 雨天的尾巴题解及改题过程(线段树合并+lca+树上差分)
题目描述 N个点,形成一个树状结构.有M次发放,每次选择两个点x,y对于x到y的路径上(含x,y)每个点发一袋Z类型的物品.完成所有发放后,每个点存放最多的是哪种物品. 输入格式 第一行数字N,M接下 ...
- Wannafly Winter Camp Day8(Div1,onsite) E题 Souls-like Game 线段树 矩阵乘法
目录 Catalog Solution: (有任何问题欢迎留言或私聊 && 欢迎交流讨论哦 Catalog @ Problem:传送门 Portal 原题目描述在最下面. 简单的 ...
- 2019牛客多校第八场 F题 Flowers 计算几何+线段树
2019牛客多校第八场 F题 Flowers 先枚举出三角形内部的点D. 下面所说的旋转没有指明逆时针还是顺时针则是指逆时针旋转. 固定内部点的答案的获取 anti(A)anti(A)anti(A)或 ...
随机推荐
- codeforces B. George and Round 解题报告
题目链接:http://codeforces.com/contest/387/problem/B 题目意思:给出1-n个问题,以及要满足是good rounde条件下这n个问题分别需要达到的compl ...
- 什么是AWS Lambda?——事件驱动的函数执行环境
AWS CTO Werner Vogels在AWS re:Invent 2014大会的第二场主题演讲上公布了两个新服务和一系列新的实例,两个新服务都相当令人瞩目:第一个宣布的新服务是Amazon EC ...
- RequireJS 配置理解
RequireJS 配置: 1.首先加载RequireJS文件 <script src="//cdn.bootcss.com/require.js/2.1.22/require.js& ...
- oracle下 启动subversion命令 及 oracle相关服务启动备忘
linux shell下 svnserve - d -r + 目录 例如:svnserve -d -r /svn 启动 svn服务. 访问svn://192.168.0.120/kjcg 测试. ...
- java-swing-JTextComponent
package com.http; import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Swi ...
- 如何使用Psyco为你的Python程序提速
psyco加速Python执行速度的方法:要求: 版本对照:File name Python versions Well-tested withpsyco-x.y-win32-py ...
- c语言中的# ## 可变参数宏 ...和_ _VA_ARGS_ _
1.#假如希望在字符串中包含宏参数,ANSI C允许这样作,在类函数宏的替换部分,#符号用作一个预处理运算符,它可以把语言符号转化程字符串.例如,如果x是一个宏参量,那么#x可以把参数名转化成相应的字 ...
- python数据分析笔记中panda(2)
1 将手机号码分开为运营商,地区和号码段 from pandas import read_csv; df = read_csv("H:\\pythonCode\\4.6\\data.csv& ...
- IDL(Interactive Data Language——交互式数据语言)
Interactive Data Language——交互式数据语言 目前,图像处理常用的ENVI就是用IDL开发的经典软件 广泛支持的平台: Microsoft Windows Open VMS S ...
- 洛谷 - P4450 - 双亲数 - 整除分块
https://www.luogu.org/fe/problem/P4450 应该不分块也可以. 求\(F(n,m,d)=\sum\limits_{i=1}^{n}\sum\limits_{j=1}^ ...