Given multiple queries of the form : "L R k" , how to find the count of numbers less than 'k' in the range {L,R} of the array.
There are no update queries.
N=1000000.
I am not interested in the solution with a merge-sort-tree .
# | User | Rating |
---|---|---|
1 | tourist | 3985 |
2 | jiangly | 3814 |
3 | jqdai0815 | 3682 |
4 | Benq | 3529 |
5 | orzdevinwang | 3526 |
6 | ksun48 | 3517 |
7 | Radewoosh | 3410 |
8 | hos.lyric | 3399 |
9 | ecnerwala | 3392 |
9 | Um_nik | 3392 |
# | User | Contrib. |
---|---|---|
1 | cry | 169 |
2 | maomao90 | 162 |
2 | Um_nik | 162 |
4 | atcoder_official | 161 |
5 | djm03178 | 158 |
6 | -is-this-fft- | 157 |
7 | adamant | 155 |
8 | Dominater069 | 154 |
8 | awoo | 154 |
10 | luogu_official | 150 |
Given multiple queries of the form : "L R k" , how to find the count of numbers less than 'k' in the range {L,R} of the array.
There are no update queries.
N=1000000.
I am not interested in the solution with a merge-sort-tree .
Name |
---|
Constraints?
What is the source of this problem? Please provide a link if possible.
https://www.spoj.com/problems/KQUERY/
If you can process the queries offline you should sort all of the values in the array. After that let's iterate over the sorted values, and add 1 to it's original position in the array using a segment tree, when all values < some k have been added, the answer for the query with that k is the sum on the query's range. Each query costs us log(n) to process thus this approach solves the problem in O(nlogn).
If you can't sort the queries offline, i sugest building a mergesort tree over the original array. In each node of the tree you will keep the values of that node's subarray sorted. Now for each query you will need to decompose the range into base nodes, and for each node binary search the number of values <k.' Building the mergesort tree takes nlogn time, and each query can be answered in logn^2, so the total time complexity is O(nlog(n)^2) This approach can also be used to handle updates. To do this you have to keep a binary search tree in each node instead of a sorted array.
What is a mergesort tree ?
Basically divide the elements in segments like in a segment tree and for every node you keep all the values in that range sorted. When you do a query like :"number of elements between a and b from l to r" you use the same idea of segment tree to choose the segments and after that you can use binary search to count the number of elements in every range that you are interested in.
I just wrote this code for a merge sort tree that support the query that you wanted.
Using algorithm's functions the code, as you can see, is really short.
You can use policy based data structure to solve this question (ordered_set) in O(N*logN).
You can refer to this link to look at this data structure. https://codeforces.net/blog/entry/11080
You can also solve it using SQRT Decomposition but you need to be careful about the time limit.
I know it's old post, but can you please tell me pbds approach?
I know how to find if l = 0. But don't know for other values of l.
If you really wanna get to it... you can turn each element into a point (i, a[i]) and use an implicit 2d segment tree to query for points in the region where x is [l, r] and y is [k, infinity). O(n log^2 n)
Merge sort tree has same time complexity and MUCH better constant factor
We can solve this problem Offline. First, sort the queries in increasing order and the values of the array as {value,index}. For each query ,using a data structure which can do sums and update operations (eg segment tree or bit), set to one the all the indices which have value < K of the current query and >= K of the previous query .As you have done this for previous queries the sum query will take into account all values < K.the answer of i'th query is sum(l,r) after performing updates. Here's my submission for a similar problem.
Wavelet tree can solve online version of this problem in O(n*log(maxA)) and offline version in O(n*log(n)) (using compression).
Useful links:
Errichto's wavelet tree tutorial on YouTube
Codeforces blog with implementation