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 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
Submissions
Please login to view your submissions
Similar Problems
1.Frames
Points:50
4 votes
Tags:
ApprovedCombinatoricsData StructuresFenwick TreeFenwick treeHardOpenTrees
Points:50
15 votes
Tags:
ApprovedData StructuresHardLoopsOpenTrees
Points:50
2 votes
Tags:
Data StructuresFenwick TreeFenwick treeHard
Editorial