hhuoj Mouse and Parenthesis 线段树求局部最小值 TWT Tokyo Olymipic 2COMBO -1 未完待续
2016-09-06 01:40
405 查看
问题 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.
Parenthesis sequence S is balanced if and only if:
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 isbalanced, otherwise 'No'.
样例输入
2 4 2 (()) 1 3 2 3 2 1 () 1 2
样例输出
Case #1: No Yes Case #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; }
相关文章推荐
- 开始Docker
- Forbidden:You don't have permission to access /index.php on this server
- Codeforces Round #281 (Div. 2) C. Vasya and Basketball 二分
- 计算机世界--软件和硬件
- Python 二分法小程序
- 深度(Depth)概念
- maven工程pom文件标签详解
- for添加用户
- UVA10494 - If We Were a Child Again
- Codeforces Beta Round #7 D. Palindrome Degree manacher算法+dp
- struts2与spring整合后Action的配置方式
- CSU 1087 就多了两分钟【恶心题,时间计算】
- 学习Hadoop第五课(配置ssh免密码登陆配置和ssh原理)
- 第29篇 JS及html
- 记一次FPGA特别低级的错误
- WPF DevExpress 设置雷达图Radar样式
- 【LeetCode】169.Majority Element (Easy)
- 计算机世界--函数调用
- 今日成长笔记2016-09-05
- 自定义view