Can anybody give some hints on this problem plz ??
# | User | Rating |
---|---|---|
1 | tourist | 4009 |
2 | jiangly | 3823 |
3 | Benq | 3738 |
4 | Radewoosh | 3633 |
5 | jqdai0815 | 3620 |
6 | orzdevinwang | 3529 |
7 | ecnerwala | 3446 |
8 | Um_nik | 3396 |
9 | ksun48 | 3390 |
10 | gamegame | 3386 |
# | User | Contrib. |
---|---|---|
1 | cry | 165 |
2 | maomao90 | 163 |
2 | Um_nik | 163 |
4 | atcoder_official | 161 |
5 | adamant | 160 |
6 | -is-this-fft- | 158 |
7 | awoo | 157 |
8 | TheScrasse | 154 |
9 | nor | 153 |
9 | Dominater069 | 153 |
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 !!!