Shil and Special Pairs
Practice
4.8 (22 votes)
Approved
Bit manipulation
Data structures
Hard
Open
Ready
Sorting
Problem
77% Success 513 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 lijr 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 ≤ piN
1 ≤ lrN

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

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
13 votes
Tags:
AlgorithmsApprovedHardOpenSqrt-Decomposition
Points:50
2 votes
Tags:
Advanced Data StructuresBinary Indexed TreesData StructuresFenwick TreeHard
Points:30
45 votes
Tags:
MediumApprovedBacktracking