Блог пользователя OneClickAC

Автор OneClickAC, 7 лет назад, По-английски

Hello CF community,

In this blog, I look to ask up two things which are somewhat related to my approach for yesterday's Div2 E problem Buy Low Sell High. But before asking about that I need to ask something about segment trees.

My approach for yesterday's problem - First I took all the stock values in a pair array with second index storing actual index. I sorted the array so as to get the least to highest prices keeping lowest index in mind first.

I built a segment tree (max query) then.

The basic idea was to now iterate over the pair array whose first index gives me the stock value (say val) and second index gives actual position (pos) of that stock in the real array. The next thing to do is find the maximum value of stock in array (using segment tree) from pos + 1 to n , say max. Since there is a possibility that there can be multiple position with value max in the array, I took up the closest one to pos. For this, I simply stored all the values of position a particular number in a set and just found the upper bound of pos. If that max value is greater than stock value, cool.. update it, add the difference to answer and keep moving, otherwise just simply continue.

The first submission I made got a runtime error and I wasn't able to understand why it happened. After the contest while trying to upsolve or possibly find out the error, what I just did was change all 2node + 1 to 2node and 2node + 2 to 2node + 1 in my segment tree implementation. Surprisingly the problem now got solved.

So my two questions for this problem and particularly my situation are that why the problem got solved with that little change or in-fact why I got that problem when everything was basically fine and same in my update and query function. Was it something related to memory issues?

Second obviously is why my algorithm is wrong as I am still getting wrong answer?

Problem Link - http://codeforces.me/problemset/problem/867/E

My runtime error submission - http://codeforces.me/contest/867/submission/30885275

No runtime error but WA submission - http://codeforces.me/contest/867/submission/30910560

  • Проголосовать: нравится
  • +1
  • Проголосовать: не нравится

»
7 лет назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please help me out?

»
7 лет назад, # |
Rev. 2   Проголосовать: нравится +1 Проголосовать: не нравится

Two children of node u are 2u and 2u + 1, not 2u + 1 and 2u + 2. Then the maximum elements for segment tree is 4n, if you did right. That why you got RE.