KQUERY

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

Thanks

# | User | Rating |
---|---|---|

1 | MiFaFaOvO | 3681 |

2 | Um_nik | 3567 |

3 | tourist | 3520 |

4 | maroonrk | 3421 |

5 | apiadu | 3397 |

6 | 300iq | 3317 |

7 | ecnerwala | 3309 |

8 | Benq | 3283 |

9 | LHiC | 3229 |

10 | TLE | 3223 |

# | User | Contrib. |
---|---|---|

1 | Errichto | 194 |

2 | antontrygubO_o | 193 |

3 | vovuh | 178 |

4 | pikmike | 177 |

5 | Radewoosh | 168 |

6 | tourist | 166 |

7 | Um_nik | 165 |

8 | ko_osaga | 163 |

9 | McDic | 162 |

9 | Ashishgup | 162 |

Codeforces (c) Copyright 2010-2020 Mike Mirzayanov

The only programming contests Web 2.0 platform

Server time: Jun/07/2020 08:09:47 (h2).

Desktop version, switch to mobile version.

Supported by

User lists

Name |
---|

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?

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 k

now 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 first

then 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 them

assign x as the greatest j we found above

all 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

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.

Thank you for your reply

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.

TLE http://pastebin.com/y5kzd5mE

.

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

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.

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

here is my implementation

You can solve this problem using segment tree with vectors. At every node, you store a sorted vector in segment tree.

Here is my implementation using segment tree with vectors

Bro i think your code is for a similarly named problem KQUERYO. that code will probably not work here due to stricter time limits.

I have made a video tutorial on how to solve this problem. I have used offline approach to solve this problem using segment trees. You can watch the tutorial here: https://youtu.be/smAKks-aKb8

The code link is available in the description section of this video. This problem can also be solved using online approach with segment trees (a.k.a. merge sort trees). I also recommend solving KQUERYO on spoj, https://www.spoj.com/problems/KQUERYO/ once you are able to solve KQUERY.

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 bej-i+1-XNow, 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 inVfor corresponding number. - Count the sum of elements in array A from i to j.by Range sum query using segment tree or BIT. - Answer to each query=**j-i+1-X**X=sum(l,r)My code