Tasks of a class monitor
Practice
3.3 (13 votes)
Binary search
Sparse table
Sparse table
Advanced data structures
Data structures
Problem
19% Success 1978 Attempts 50 Points 2s Time Limit 512MB Memory 1024 KB Max Code

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),\ (l\le r)\) 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 \(i^{th}\) 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
\(1\le N\le 1e6\\ 1\le K\le 1e9\\   1\le i\le N\\ 1\le H[i]\le 1e9\\\)

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
Tags:
Sparse TableAdvanced Data StructuresCentroid decompositionMedium-HardData Structures
Points:50
2 votes
Tags:
Data StructuresApprovedMedium-HardData Structures
Points:50
2 votes
Tags:
Data StructuresApprovedMedium-HardBinary search algorithmData Structures