### dario-dsa's blog

By dario-dsa, 6 years ago, , KQUERY
Can anyone help me to solve this problem? I have trouble to put it in BIT way on thinking.
Thanks spoj, c++, Comments (18)
 » 6 years ago, # | ← Rev. 3 →   it can be solved by sorting the query list by k in descending order (same go for array A)then with each query we add those element from A which is greater than k to it original position in the BIT (you need to keep track of those element which is already added so the total number of update is n (the reason we sort A))then it retrun to a basic problem of adding element and counting the number of element in range :p
•  » » Can you give me a little bit more information?
•  » » 6 years ago, # ^ | ← Rev. 6 →   well, let sort A in descending order and B[i] be the position of A[i] in the original array , we will solve from the query with greatest k to the query with smallest know we have query (i, j, k), let x be the number such as all element 1..x from A is added to the tree (BIT), set by 0 at firstthen we add all the element A[j] such as j > x and A[j] > k to the tree (at B[i]) element j <= x is already added so ignore themassign x as the greatest j we found aboveall the element added into the tree so far is greater than k so the only thing left is to count the number of element added in range (i, j)is this enough for an explaination I wonder ? :p
•  » » » nicely explained :p
•  » » » superb explaination
•  » » 6 months ago, # ^ | ← Rev. 2 →   First I have created a segment tree and each query I assigned ascending order sorted array in a range, then applying the binary search for counting greater than K values but I am getting TLE. Note. I have ascending order sorted array so I tried to mid of the right side first, but I am getting TLE. Bellow, I have attached my code. Do you have any suggestions, please? Tanks https://ideone.com/HOs945
•  » » » You answer each query in $O(\log^2 n)$ which is too high for this problem. You can improve it to $O(\log n)$ by storing all queries, answering them in a nicer order and then printing all answers.Try to understand this or this.
 » Create a large area in which you are going to store both the queries (represented by their k values) and the numbers of the original array. Now sort this array in non-increasing order (remember to store the original positions in a separate array).Suppose that you have a segment tree (initially all zeros).Now scan the array from left to right. If the current number was a number in the original array, update it's value to 1 (in the segment tree). If the current number (call it x) is a query then we know that all numbers greater than x have been inserted in the segment tree because we sorted the array (so all intervals that contain it have been updated); execute a range sum query to get the answer.Queries were scrambled when we sorted the array, so you need to print them in the order they were given.The time limit for this problem is tight (1500 ms). Use a Fenwick tree and use static arrays instead of vectors.