[HDU5217]Brackets

Problem Description
Miceren likes playing with brackets. 

There are  N brackets on his desk forming a sequence. In his spare time, he will do  Q operations on this sequence, each operation is either of these two types: 

1. Flip the  X-th bracket, i.e. the  X-th bracket will become ) if it is ( before, otherwise become ( .

2. In a given subsequence  [L, R], query the  K-th bracket position number after deleting all matched brackets. If the amount of brackets left is less than  K, output -1. 
For example, if  N = 10,  L = 2,  R = 9 and the sequence is ))(()))((). In the sub sequence [2, 9], the brackets sequence is )(()))((. After deleting all matched brackets, the sequence becomes ) )((. If  K = 1, answer is 2. If  K = 2, answer is 7. If  K = 3, answer is 8. If  K = 4, answer is 9. If  K  5, answer is -1.

Miceren is good at playing brackets, instead of calculating them by himself. 

As his friend, can you help him?
 

Input
The first line contains a single integer  T, indicating the number of test cases.

Each test case begins with two integers  N, Q, indicating the number of brackets in Miceren's desk and the number of queries. 

Each of following  Q lines describes an operation: if it is "1 X", it indicate the first type of operation. Otherwise it will be "2 L R K", indicating the second type of operation.

T is about 100.

1  N,Q  200000.

For each query,  1  X  N and  1  L  R  N 1  K  N.

The ratio of test cases with  N > 100000 is less than 10%.
 

Output
For each query operation, output the answer. If there is no  K brackets left, output -1.






[题意]
         给定一段长度为n的括号序列,题目将给你q个操作。
        操作分为两种:
         1  x   翻转第x个括号,即左变右,右变左。
         2  l r k  查询[l,r]之间第k个括号是什么。(要求将[l,r]之间匹配的左右括号完全删去后的第k个) 


    [题解]

            首先我们可以用线段树简单维护出一段区间内消除匹配括号后,左右括号数。显然,消除后的括号是形如))))((((这样的,所以我们可以直接通过判断k是否大于区间内右括号数,就可以查出它是否是右括号。

               接着,如果是右括号,从左往右查找的时候,右括号是不会被后面的左括号消除的,左括号是不会被前面的右括号消除的。于是我们就在线段树上运用二分查找,右括号就从左往右,左括号就从右往左,就可以解决了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值