KQuery Problem TLE

Sir, I am getting TLE on SPOJ but accepted on Hackerblaocks. Actually, I am realizing that in every another Segment Problem i get TLE on different platforms. Could you give me the idea of what are things to keep in mind while solving segment Trees Problems? Sir i have not learnt about Fenwick tree yet, are they helpful in tackling with TLE
My Solution-https://ide.codingblocks.com/s/71026

Spoj test cases are little bit high.
You are doing v.pb() in your code which is O(n) operation.
Try by directly accessing using v[i] which is O(1) operation.
Your code is correct .It just needs little bit optimizations(only for SPOJ platform).

1 Like