hhuoj Mouse and Parenthesis 线段树求局部最小值 TWT Tokyo Olymipic 2COMBO -1 未完待续
来源:互联网 发布:张国荣同性恋 知乎 编辑:程序博客网 时间:2024/06/18 00:38
问题 E: Mouse and Parenthesis
时间限制: 1 Sec 内存限制: 128 MB提交: 31 解决: 7
[提交][状态][讨论版]
题目描述
Tom has m same balanced parenthesis sequence P=p1 p2…pn of length n.
This day Jerry comes into Tom's room and swaps one pair of parenthesis in every sequence.
Tom and Jerry both like balanced parenthesis sequence, so Jerry wants to know whether each P remains balanced after pai and pbi swapped.
1. S is empty;
2. or there exists balanced parenthesis sequence A,B such that S=AB;
3. or there exists balanced parenthesis sequence S' such that S=(S').
输入
The first line contains an integers T (T≤20), which indicates the number of test cases.
For each case:
The first line contains two integers n,m.The second line contains n characters p1 p2…pn.
The i-th of the last m lines contains 2 integers ai,bi (1≤ai,bi≤n,ai≠bi).
⋅ for 50% data, 1≤n≤50,1≤q≤1000.
⋅ for 100% data, 1≤n≤100000,1≤q≤100000.
输出
For every test case, you should output "Case #x:", where x indicates the case number and counts from 1. Then in ith line output 'Yes' if ith parenthesis sequence is balanced, otherwise 'No'.
样例输入
24 2(())1 32 32 1()1 2
样例输出
Case #1:NoYesCase #2:No
/* ━━━━━┒ ┓┏┓┏┓┃μ'sic foever!! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┃┃┃┃┃┃ ┻┻┻┻┻┻ */#include <iostream>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>using namespace std;const int maxn =1e5;int number[maxn+5];char chr[maxn+5];int a[maxn+5];struct unit{ int l; int r; int sum;}save[(maxn+5)*9];void build(int l,int r,int index){ save[index].l=l; save[index].r=r; if(l==r){ save[index].sum=a[l]; return; } int middle = (l+r)/2; build(l, middle, 2*index); build(middle+1, r, 2*index+1); save[index].sum=min(save[2*index].sum,save[2*index+1].sum);}int getSum(int l,int r,int index){ if(save[index].l==l&&save[index].r==r){ return save[index].sum; } int middle = (save[index].l+save[index].r)/2; if(middle<l){ return getSum(l, r, 2*index+1); }else if(middle>=r){ return getSum(l,r,2*index); }else{ return min(getSum(l, middle, 2*index),getSum(middle+1, r, 2*index+1)); }}int main(){ int t; scanf("%d",&t); int rnd=1; while(t--){ int n,m,i; string in; scanf("%d%d",&n,&m); scanf("%s",chr+1); printf("Case #%d:\n",rnd); memset(a,0,sizeof(a)); a[0]=0; for(int j=1;j<=n;j++){ a[j]=a[j-1]+(chr[j]=='('?1:-1); } build(1, n, 1); for(i=0;i<m;i++){ int p1,p2; scanf("%d%d",&p1,&p2); if(chr[p1]==chr[p2]){ printf("Yes\n"); continue; } int left=min(p1,p2),right=max(p1,p2); if(chr[left]==')'){ printf("Yes\n"); continue; } int ans=getSum(left, right-1, 1); if(ans>=2){ printf("Yes\n"); }else{ printf("No\n"); } } rnd++; } return 0;}
0 0
- hhuoj Mouse and Parenthesis 线段树求局部最小值 TWT Tokyo Olymipic 2COMBO -1 未完待续
- HHUOJ 1023 Mouse and Parenthesis(线段树)
- hhuoj Boxes and Balls TWT Tokyo Olympic 3 COMBO-1 数学题
- HHUOJ LCM and Walk 数学 逆推 gcd&lcm DFS TWT Tokyo Olympic 2Combo -1
- HDU1171 Big Event in HDU 01背包 母函数 TWT Tokyo Olympic 2COMBO-1 未完待续
- HDU3466 Proud Merchants 01背包 思考 TWT Tokyo Olympic 2Combo -2 未完待续
- hhoj Friendship of Mouse 水题 TWT Tokyo Olympic 1COMBO-2
- HHUOJ 1013: 冒险游戏(risk_ganme) 动态规划 TWT tokyo olympic 2combo -2
- hhu Kingdom of Black and White 暴力 TWT Tokyo Olympic 1COMBO -1
- POJ2503 Babelfish 二分 TWT Tokyo Olympic 1 COMBO-1
- HDU4007 Dave 暴力 TWT Tokyo Olympic 2combo-1
- Find the maximum 欧拉函数 高精度 BigInteger TWT Tokyo Olympic 2COMBO-1
- HHU Mouse and Parenthesis (线段树+括号匹配)
- Contest1002 - HHU ACM 综合训练1 E题 Mouse and Parenthesis(线段树+括号匹配)
- hdu2546 饭卡 01背包 贪心 TWT Tokyo Olympic--1 Combo -1
- HDU2955 Robberies 01背包 TWT Tokyo Olympic 2Combo -2
- poj1321 棋盘问题 dfs入门 TWT Tokyo Olympic 1combo-1
- POJ3126 Prime Path BFS搜索 TWT Tokyo Olympic 3combo-1
- UVA10494 - If We Were a Child Again
- 计算机世界--软件和硬件
- (2016)京东笔试校招编程题目
- Forbidden:You don't have permission to access /index.php on this server
- 开始Docker
- hhuoj Mouse and Parenthesis 线段树求局部最小值 TWT Tokyo Olymipic 2COMBO -1 未完待续
- 在SourceTree建立GitHub远程Remote文件
- Java的异常总结
- 深入理解java虚拟机 第2章(二):对象创建,内存布局,访问
- html表单所有类型
- 第三章-宝箱抽奖模块与代码设计(三)
- 1012: [JSOI2008]最大数maxnumber
- Python之一:怎么在Shell上运行文件?
- day10面向对象