SEACO - Editorial

Because the alternative is to update the actual array, which will result in O (N) complexity per command, thus O (N^2) comolexity for whole task, giving TLE.

using difference array, u apply update in constant time.

I have used difference arrays only and got 100 points. I have added explanation just above, alongwith a link to my code.

Ask me anything if required…

Hers’s the link

https://discuss.codechef.com/questions/108189/seaco-editorial/110993

Please upvote if u find that helpful…

The contest has ended.

Now submission is not allowed for contest. U may solve the question in practice section using following link

This is the contest problem link.

Just remove the contest name and ull be able to submit solution…

Following link

I saw your code . The last part of your approach is the same as mine.But I haven’t yet found out the mistake in my code.Please tell me if you find any bug in my code.
Thank you

Hii! Can u please elaborate why did u do the following steps for type 2:


if(command[i][0] == 2){
                    range[command[i][2]]+=t+1;
                    range[command[i][1]-1] -= t+1;
                }

I mean I am not able to get why you add t+1 to range[command[i][2]] and subtract it from range[command[i][1]-1] and why not like you did in query of type 1!

Thanks :slight_smile:

Well, the essential point is given in the question itself,

“It’s guaranteed that r is strictly less than the enumeration/number of the current command.”

So, we run a loop in backward order, keeping track which command is to be executed how many times…

Well, a restore to actual sum sequence is required because after that, we are to provide for command type 1 also.

I have discussed my implementation in detail in the following link

https://discuss.codechef.com/questions/108189/seaco-editorial/110993

Please UPVOTE if you find this helpful…

Here, variable t is keeping the track how many times this particular command is to be executed within the previously processed commands of type 2.

In the problem, we are supposed to execute all the commands exactly once, so 1 is added for that execution in third loop.

Please UPVOTE, much needed…

Hope its clear now…

Feel free to ask anything…

For example N = 3, M = 3

1 1 1

2 1 1

2 1 2

initially range array 0 0 0 0

During first loop

when i == 3

t is 0

we added (t+1) = 1 to range[2]

subtracted 1 from range[1-1]

range array becomes -1 0 1 0

i == 2

t = 1 (0 + range[2])

we added (t+1) = 2 to range[1]

subtracted 2 from range[0]

range array becomes -3 2 1 0

ignore i == 1

final range array -3 2 1 0

sum array 0 3 1 0 (using reverse loop)

From here, we can ignore commands of type 2 and see, command 1 is to be executed 3 times because of other commands + 1 time for its own, as i mentioned above.

Answer array is 4 0 0

1 Like

I am not sure what you are trying to say. You can see my code that just uses running totals of the difference arrays. Both command types are handled in a single pass starting from the last command.

Well,

I don’t really understand what you are missing here, because i code in java and not used to C language, so don’t understand your code…

I think it depends upon the approach we are following. In your approach, as your code proved, There’s no need to restore sum sequences…

But in my approach, you need to restore sum array atleast once to find number of executions of command 1…

So, in short, restoring sum array is dependent upon approach followed.

Hope that helps…

Please UPVOTE…

Feel free to ask anything…

Illiteracy in popular programming languages does not justify your condescending attitude. :wink:

Thanks for the detailed explanation. But can u pls tell me how u came up with such a solution. I mean is this any standard algorithm? If yes, pls link some material where I can read more about it.
Thanks again :slight_smile:

Well,

I’m a new programmer with just one year in java programming mate…

I meant to co-operate as much as i can…

I tried my best explaining to you my code, the query you asked as well as the difference in our approaches…

But when i cannot understand a code, how can i explain what’s going on in the loop and statements…

Well, if you found my explanation / approach / effort or code, anything, Please Upvote…

Feel free to ask anything…

I’ll be pleased to clarify your doubts… :slight_smile:

May be it is some kind of misunderstanding, but I honestly don’t care about your code and have never asked you to explain to me anything about it. This is the editorial thread, in case you have not noticed. :slight_smile:

@dishant_18

You can read more about difference arrays at

example problem mentioned on that page is just the same…

Oh Sure,

I merely tried to help… :slight_smile: