BRCKTS - Brackets

We will call a bracket word any word constructed out of two sorts of characters: the opening bracket "(" and the closing bracket ")". Among these words we will distinguish correct bracket expressions. These are such bracket words in which the brackets can be matched into pairs such that

  • every pair consists of an opening bracket and a closing bracket appearing further in the bracket word
  • for every pair the part of the word between the brackets of this pair has equal number of opening and closing brackets
On a bracket word one can do the following operations:
  • replacement -- changes the i-th bracket into the opposite one
  • check -- if the word is a correct bracket expression


Write a program which

  • reads (from standard input) the bracket word and the sequence of operations performed,
  • for every check operation determines if the current bracket word is a correct bracket expression,
  • writes out the outcome (to standard output).


Ten test cases (given one under another, you have to process all!). Each of the test cases is a series of lines. The first line of a test consists of a single number n (1<=n<=30000) denoting the length of the bracket word. The second line consists of n brackets, not separated by any spaces. The third line consists of a single number m -- the number of operations. Each of the following m lines carries a number k denoting the operation performed. k=0 denotes the check operation, k>0 denotes replacement of k-th bracket by the opposite.


For every test case your program should print a line:
Test i:
where i is replaced by the number of the test and in the following lines, for every check operation in the i-th test your program should print a line with the word YES, if the current bracket word is a correct bracket expression, and a line with a word NO otherwise. (There should be as many lines as check operations in the test.)


[and 9 test cases more]
Test 1:
[and 9 test cases more]

Warning: large Input/Output data, be careful with certain languages

hide comments
kshubham02: 2016-06-12 23:58:25

Isn't there any upper bound on 'm'? My m*n solution gives TLE. I didn't think it would happen after seeing the limit as 11sec.

nonushikhar: 2016-06-01 13:00:09

easy one :)
HINT :::
go seg_tree :)))

Aditya: 2016-03-23 22:15:23

Nice problem :)

Rudra Nil Basu: 2016-03-21 15:38:12

Century ! :D

raghav12345: 2016-01-16 10:43:36

really want to learn segment tree then hacker earth segment tree
notes is the best option :)

Alex Gu: 2016-01-03 03:02:24

No constraint on m?

anshal dwivedi: 2015-12-25 10:51:50

confidence booster...! segment tree rocks..!

Prashant Kumar: 2015-12-20 15:18:11

Is (()()) a valid bracket ..... if not why?

trieuman189: 2015-10-27 19:59:48

Blog Thuật toán SPOJ can help you :

Tarik Sahni: 2015-10-23 14:06:40

Took a lot of time
silly mistakes # print test i :P

Last edit: 2015-10-23 21:46:09

Added by:Adam Dzedzej
Time limit:11s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: NODEJS PERL 6
Resource:Internet Contest Pogromcy Algorytmow(Algorithm Tamers) 2003 Round IV