Vanya and Array
Practice
4 (463 votes)
Ad Hoc
Approved
Bit manipulation
Data structures
Easy
Fenwick tree
Sorting
Problem
68% Success 5697 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Let's define 2 functions a function \(F(i)\) and \(Val(i,j)\) for an Array A of size N as follows:

\(F(i) =\) \( \sum_{j=i+1}^{N} Val(i,j) \)

\( Val(i,j) =\) 1 ,if \(A[i] < A[j]\), else, \(Val(i,j) =\) 0.

Now, Vanya has been given one such array A of size N and an integer K. She needs to find the number of Distinct Unordered Pairs of elements \((a,b)\) in array A such that \(F(a) +F(b) \ge K\).

Input Format:

The first line contains 2 space separated integers N and K denoting the size of array A and the integer k from the description given above. The next line contains N space separated integers denoting the elements of array A .

Output Format:

You need to print the required answer on a single line.

Constraints:

Subtask 1: (Worth 40% of the total points):

\( 1 \le N \le 5000 \)

\( 1 \le A[i] \le 10^5 \)

\( 0 \le K \le 10^4 \)

Subtask 2: (Worth 60% of the total points) :

\( 1 \le N \le 10^6 \)

\( 1 \le A[i] \le 10^9 \)

\( 0 \le K \le 10^9 \)

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:30
50 votes
Tags:
ApprovedData StructuresEasyOpenSegment TreesTrees
Points:30
8 votes
Tags:
ApprovedData StructuresEasyFenwick TreeFenwick tree
Points:30
8 votes
Tags:
Data StructuresEasyFenwick TreeFenwick tree