Tasks of a class monitor

3.3

13 votes
Binary Search, Sparse table, Sparse Table, Advanced Data Structures, Data Structures
Problem

A class teacher calls the class monitor to assign a task. The task is to make the whole class stand in a row according to the roll number. The class monitor is asked to calculate the total number of roll number pairs (l, r), (lr) such that the absolute difference between the height of the tallest student among roll number l to roll number r and the height of the student whose roll number l is less than or equal to K
There are N students in a class. The information of a class is given to you in the form of an array H.
Here, H[i] denotes the height of the student having roll number i.
  
Input format

  • The first line consists of two space-separated integers N denoting the size of the class and K denoting the difference parameter.
  • The next N lines contain information about the class.
  • The ith line represents the height of the student whose roll number is i.

Output format
The output contains a single integer denoting the number of required roll number pairs.

Constraints
1N1e61K1e91iN1H[i]1e9

Sample Input
3 3
2
3
6
Sample Output
5
Time Limit: 2
Memory Limit: 512
Source Limit:
Explanation

The possible roll number pairs are :(1,1), (1,2), (2,2) , (2,3) and (3,3).

Editor Image

?