Shil and Special Pairs
Practice
4.8 (22 votes)
Approved
Bit manipulation
Data structures
Hard
Open
Ready
Sorting
Problem
77% Success 516 Attempts 50 Points 2s Time Limit 256MB Memory 1024 KB Max Code
Shil has a permutation p1 , p2 .. pN of numbers from 1 to N and M queries. Each query consists of two integers l and r . Answer to each query is total number of pairs[i , j] such that l ≤ i ≤ j ≤ r and|pi - pj| ≤ D.
INPUT:
First line consists of three integers N, M and D. Next line consists of permutation p1 , p2 .. pN of the first N natural numbers. Next M lines consists of two integers l and r .
OUTPUT:
Output M integers with ith integer corresponding answer to ith query.
CONSTRAINTS:
1 ≤ N, M ≤ 105
1 ≤ D ≤ 10
1 ≤ pi ≤ N
1 ≤ l ≤ r ≤ N
Please login to use the editor
You need to be logged in to access the code editor
Loading...
Please wait while we load the editor
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
2 votes
Tags:
Data StructuresFenwick TreeFenwick treeHard
Points:50
2 votes
Tags:
Data StructuresBinary indexed treeDatastructuresAdvanced Data StructuresFenwick (Binary Indexed) Trees
Points:30
45 votes
Tags:
MediumApprovedBacktracking
Editorial
Login to unlock the editorial