This is a trivial application of Fenwick tree. Those not familiar with Fenwick tree may look at the topcoder editorial available here.
All this is asking you to do is to online update of a single element in the array and the sum in a range. Note that the sum of elements of an array from indices i through j can be written as cum_sum[j]-cum_sum[i] where cum_sum[i] is the cumulative sum of all elements in 1 through i.
Seeking the cumulative sum at any point in an array after multiple updates can be done efficiently in O(log n) using the Fenwick tree. You may use the update and read function (available here) in order to do this efficiently.
is there any efficient method to construct the fenwick tree …i saw many of the sols construct the tree by reading the data and updating the tree. in worst case if we have to construct the tree of n node it will take O(nlogn) time. and if there are Q query hence the total time complexity will be
O(n logn+Qlogn).
It seems the constraints of the problem doesn’t really force us to find a subquadratic solution.
I found a few solutions that run in O(Q2) in the worst case, and to hide their identities, I tried to replicate their solution. I got AC with this code (this submission) ! I simply kept track of all updates so far in a list, and during queries I simply determined which of those updates are relevant, using a linear scan!
This is a bit saddening, because some solvers who did this didn’t get the chance to learn Fenwick/Binary-indexed/segment tree.
It might have been better to have Q ≤ 200000 instead of just Q ≤ 50000 .
Well, I’m really sorry to hear this,as my initial idea when setting this problem was to teach the concept of Fenwick Trees to some people (I learnt it myself while setting the problem), but, with this hack some people might have missed it Will try to be more careful next time
Yes, the constraints on the problem were really weak. I looked at the constraints and tried the brute force solution described above and got AC so i didn’t bother spending more time on it. As it was an easy question, i did not realize it was meant to be solved using BIT. Would have been interesting with tougher test cases.
Tree construction could be done in o(n) time. you could check the process() method from my solution here http://www.codechef.com/viewsolution/3049621. The trick is to create a prefix tree and use that for fenwick tree creation
You can initialize the tree with zeroes (straight in the constructor), count the differences in number of marbles that the queries cause, and sum those up with array A, which also gives O(N+Q\log{N}) complexity.
Hi
I tried to solve the problem using fenwick tree but I’m getting a runtime error(SIGSEGV). I checked to see if I was doing some invalid array accesses but found none. Please check: http://ideone.com/dGHVz9
Thanks
I read the whole BIT during contest time cuz i didnt know about it before. So, i understood how to create a tree and perform operations on that further. I may be wrong somewhere, the same can be done by some other way like using update you are saying.
But even if i am doing this way, why i am getting wa. Also, i saw many solutions doing exactly same but i am not able to differentiate the error in my code.
Could this problem be solved using a segment tree ? From what i have read up a segment tree requires ( 2*2^(log n base 2) -1 ) memory for array of size n .Now i tried submitting a solution using segment tree . Here is the link . However i get sigsegv error. Any way you’ll could assist me on this .
@xellos0 as i can understand ur code ur code also taking O(nlogn+Qlogn) since ur put() function taking logN time and u r calling this function after taking the N input and of query type “T” & “G”. BTW ur code is really amaZing