Can anybody give some hints on this problem plz ??
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
Name |
---|
Do you how to think of it, or which method to solve it? With these problems its typically to think of them as prefix sums where '(' = 1 and ')' = -1, we could do the other way around but this is more intuitive to me. If the cumulative "balace" is always non negative in the range and the total balance is 0 in the range it is balanced.
For method you just have to some method to minimize *= -1 and if the amount of brackets not even it is always "Impossible" otherwise it's possible
This sequence ')))(((' has total balance 0, yet it's not balanced.
And what about the update operation ?
As I said cumulative "balace" is always non negative in the range
So something like )))((( will give -1 -2 -3 2 1 0.
It cannot be negative (too many right brackets)
Easy way to keep updatable prefix sums is binary indexed tree.
Main difficulty of problem is solvign query 2 though.
Hi!
It's just a Segment Tree, in each node of the segment tree you can store two numbers: # of open parenthesis not matched in this range, # of close parenthesis not matched in this range.
When you need to merge two intervals, you only match open parenthesis of the left child with the close parenthesis of the right child as much as you can and the rest just add it to the respective field.
For more details, this is my code: https://pastebin.com/BwJ1bv2C
Hope it can be useful!
You're right. Thanks !!!