K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分)
You have an array: a1, a2, , an and you must answer for some queries.
For each query, you are given an interval [L, R] and two numbers p and K. Your goal is to find the Kth closest distance between p and aL, aL+1, …, aR.
The distance between p and ai is equal to |p - ai|.
For example:
A = {31, 2, 5, 45, 4 } and L = 2, R = 5, p = 3, K = 2.
|p - a2| = 1, |p - a3| = 2, |p - a4| = 42, |p - a5| = 1.
Sorted distance is {1, 1, 2, 42}. Thus, the 2nd closest distance is 1.
Input
The first line of the input contains an integer T (1 <= T <= 3) denoting the number of test cases.
For each test case:
冘The first line contains two integers n and m (1 <= n, m <= 10^5) denoting the size of array and number of queries.
The second line contains n space-separated integers a1, a2, …, an (1 <= ai <= 10^6). Each value of array is unique.
Each of the next m lines contains four integers L’, R’, p’ and K’.
From these 4 numbers, you must get a real query L, R, p, K like this:
L = L’ xor X, R = R’ xor X, p = p’ xor X, K = K’ xor X, where X is just previous answer and at the beginning, X = 0.
(1 <= L < R <= n, 1 <= p <= 10^6, 1 <= K <= 169, R - L + 1 >= K).
Output
For each query print a single line containing the Kth closest distance between p and aL, aL+1, …, aR.
Sample Input
1
5 2
31 2 5 45 4
1 5 5 1
2 5 3 2
Sample Output
0
1
現在才寫這篇博客有點晚了。當時做這道題的時候還不太會,現在想想大體上可以想明白了。
這種第k大或者第k小的問題就是主席樹。但是這次是絕對值第k小,這就很懵逼。。我們二分去找答案,二分到一個值之后,在[0,1000000]這個區間里去看[p-mid,p+mid]這個區間內有多少個值,要是個數小于k說明需要擴大范圍,要是大于k的話,就需要減少范圍。但是要是等于k,也要去減少范圍,因為我們需要去更精確這個值。
代碼如下:
快速讀入優化后只需要3000+ms,有時候讀入優化真的很管用。。
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Super Mario HDU - 44
- 下一篇: 热狗树 树形dp(中国石油大学我要变强