algorithmdata-structuressegment-tree

Getting Wrong Answer on CSES Problem Set Hotel Queries


https://cses.fi/problemset/task/1143/

I am trying to solve this problem, I used Segment Tree for solving it. I tried every possible test cases but it is not getting accepted.

Just failing in one test case which is too big. Please help me out to solve this problem ..!!!

Solution : https://cses.fi/paste/072a5f07ec4a533b18c669/

Test results


Solution

  • The problem is when a group of guests don't fit. Your code will recurse down the right spine, then go past because the test l == r && seg[index] >= val will be false. The result is that you treat the leaf corresponding to the last hotel as a branch and zero it out as the max of the two empty children whose seg values are uninitialized.

    If I'm right, then this test should fail:

    1 2
    1
    2 1