Counting close pairs in a range
Time limit3sMemory limit512 MB
Given a sequence and K, each query asks how many index pairs inside a subarray have value difference at most K.
- Level
Hard8 of 10
- Topics
- Divide and conquer, Prefix sum, Sorting, Binary search
- Solved
- No attempts yet
Problem
You are given a sequence of length and an integer . Write a program that answers queries of the following form.
l r: print the number of pairs with and .
Input
The first line contains the length of the sequence and the integer (, ).
The second line contains separated by spaces ().
The third line contains the number of queries ().
Each of the next lines contains one query as two integers and ().
Output
For each query, print the answer on its own line, in the order the queries are given.