2016 the 12th session of Hunan Province University student computer Program Design Competition---parenthesis (segment tree to find interval maximum)

Source: Internet
Author: User

Link to original topic

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

Descriptionbobo has a balanced parenthesis sequence p=p1 p2...pn of length N and q questions. The i-th question is whether P remains balanced after Pai and PBI swapped. Note that questions is individual so that they has no affect on others. 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 ').Inputthe input contains at the most sets. For each set:the the first line contains the integers n,q (2≤n≤105,1≤q≤105). The second line contains n characters P1 p2...pn. The i-th of the last Q lines contains 2 integers ai,bi (1≤ai,bi≤n,ai≠bi).

Outputfor each question, output " Yes"If P remains balanced, or" No"otherwise. Sample Input
4 2 (()) 1 32 32 1 () 1 2
Sample Output
Noyesno
Source

The 12th session of Hunan Province College students computer Program design Contest

Test instructions: gave a balanced bracket sequence s (balance is the brace match OK) now Q, each time input two number A, B Q will s[a] s[b] After the swap whether the balance, the balance is output "Yes" otherwise output "No";

Idea: Define array num[], Num[i] represents the difference between the number of left brackets in s[1] to s[i] minus the number of closing brackets, and the analysis shows that because S is the balance bracket sequence, then num[i]>=0 a<b, then S[a] s[b, only num[a]~num[ B-1], and when Num[k]<0 (A<=K<B) is not balanced after the exchange, only s[a]= ' (' s[b]= ') can be switched on to num[k]<0.   So when s[a]= ' (' s[b]= '), the minimum value of the interval a~b-1 is calculated by the segment tree, when the minimum value is less than 2 o'clock, that is, what is the imbalance after the exchange? S[a]= ' (' s[b]= ') ' Exchange num[a]~num[b-1] minus 2;

The code is as follows:

#include <iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<cmath>#defineEPS 1e-8#defineMAXN 105#defineINF 0x3f3f3f3f3f3f3f3f#defineIn Freopen ("In.txt", "R", stdin);using namespacestd;Chars[100005];intnum[100005];intb;structnode{//int l,r;    intv;} node[100005*4]; voidBuildintLintRinti) {    if(l==r) {node[i].v=Num[l]; return; }    intMid= (l+r) >>1; Build (L,mid,i<<1); Build (Mid+1,r,i<<1|1); NODE[I].V=min (node[i<<1].v,node[i<<1|1].v);} voidQueryintLintRint&tmp,inti) {    if(l>=a&&r<=b) {tmp=node[i].v;return; } intMid= (l+r) >>1; if(mid>=b) query (l,mid,tmp,i<<1); Else  if(mid<a) query (mid+1,r,tmp,i<<1|1); Else {          intTMP2; Query (L,mid,tmp,i<<1); Query (Mid+1,r,tmp2,i<<1|1); TMP=min (TMP,TMP2); }} intMain () {intn,q;  while(SCANF ("%d%d", &n,&q)! =EOF) {scanf ("%s", s+1); num[0]=0;  for(intI=1; i<=n;i++) {Num[i]=num[i-1]; if(s[i]=='(') num[i]++; Elsenum[i]--; } Build (1N1);  while(q--) {scanf ("%d%d",&a,&b); if(a>b) Swap (A, a); if(s[a]==s[b]| | s[a]==')'&&s[b]=='(') puts ("Yes"); Else{b--; inttmp=99999999; Query (1, N,tmp,1); if(tmp<2) puts ("No"); ElsePuts"Yes"); }        }    }    return 0;}/**8 8 (()) (()) 2 7*/

2016 the 12th session of Hunan Province University student computer Program Design Competition---parenthesis (segment tree to find interval maximum)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.