Блог пользователя dario-dsa

Автор dario-dsa, 13 лет назад, По-английски

KQUERY
Can anyone help me to solve this problem? I have trouble to put it in BIT way on thinking.
Thanks

  • Проголосовать: нравится
  • 0
  • Проголосовать: не нравится

»
13 лет назад, скрыть # |
← Rev. 3  
Проголосовать: нравится 0 Проголосовать: не нравится

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

»
13 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Actually, we can solve this problem with a more "classic" way : let's build a segment tree, with a sorted subarray in every vertex. Then we can divide [l, r] into subsegments, and do a Binary Search on subsegments. O(logN ^ 2) for the query.

»
13 лет назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

Check this code http://mohamedmosamin.wordpress.com/2013/09/03/spoj-3266-k-query-kquery/. Use it the same idea that shows lazycode97

»
12 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

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.

»
8 лет назад, скрыть # |
← Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I get WA on this problem, but i don't know why.. Can someone please help me ?

here is my implementation

»
6 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Before going into the solution, consider this: For every query, count the number of element which are less than or equal to k,let us call it X. our answer for every query will be j-i+1-X

Now, to calculate the value of X:- - Declare an array A of size n. Initially all values zero. - Store the indexes of all the integers in a 2-D vector V. There can be at most n distinct integers. - Store all the queries in a vector and sort it according to k(ascending order). - Now iterate over query vector and for every number less than or equal to k, update the array for all the indexes stored in V for corresponding number. - Count the sum of elements in array A from i to j. X=sum(l,r) by Range sum query using segment tree or BIT. - Answer to each query=**j-i+1-X**

My code