Sherlock and Inversions
Practice
4.8 (13 votes)
Algorithms
Approved
Hard
Open
Sqrt Decomposition
Problem
70% Success 2651 Attempts 50 Points 5s Time Limit 256MB Memory 1024 KB Max Code
Watson gives to Sherlock an array of N integers denoted by A1, A2 ... AN.
Now he gives him Q queries of form Li, Ri. For each such query Sherlock has to report the number of inversions in subarray denoted by [Li, Ri].
Inversions in a subarray denoted by [a, b] are number of pairs (i, j) such that a ≤ i < j ≤ b and Ai > Aj.
Input
First line contains N and Q. Next line contains N space separated integers denoting array A.
Each of the next Q lines contain two space separated integers denoting Li, Ri.
Output
For each query, print the required answer in one line.
Constraints
20% files: 1 ≤ N, Q ≤ 103
20% files: 1 ≤ N ≤ 103 and 1 ≤ Q ≤ 105
60% files: 1 ≤ N, Q ≤ 105
1 ≤ Ai ≤ 109
1 ≤ Li ≤ Ri ≤ N
Submissions
Please login to view your submissions
Similar Problems
Points:50
5 votes
Tags:
Segment treeAdvanced Data StructuresData StructuresFenwick (Binary Indexed) Trees
Points:50
2 votes
Tags:
Advanced Data StructuresBinary Indexed TreesData StructuresFenwick TreeHard
Points:50
6 votes
Tags:
Fenwick (Binary Indexed) TreesData StructuresAdvanced Data Structures
Editorial