Getting TLE in RPLN Spoj

Problem Link -

Ideone Link for code -

I have used Segment tree method to solve this.Please help me in figuring out what’s wrong with this code.


Instead of using cout and cin use printf() and scanf().

You dont have to initialise the tree with zero for every test case.

I think this should do. :slight_smile:

1 Like

done , still getting TLE. Thanks for helping though :slight_smile:

still need help?
Your code get ac when submitted by removing all the "endl"s


in general, avoid using endls, instead use ‘\n’.
but if you still want your endls, just include this line on top of your code->

" #define endl ‘\n’ "

if this helped, it would be great if you could upvote this answer…


It helped me get the AC thanks for that.

But , I was actually looking for corrections in approach.Anyways , thanks for the help though.

You will get an upvote for this buddy :slight_smile:

@shubham99, the reason is using endl flushes the output stream after printing new line. Hence its slower where’s otherwise all data is stored in a buffer and whole buffer is printed at once. Link:

Thanks @vsp4 , I have seen that link after looking at that answer although thanks for pointing it out. Actually , I am looking for the optimizations to make my program faster if possible by some small corrections missed by me.

i don’t think that you would be able to make this Segtree any faster. All the operations are perfect. Also, why not use vectors?