Counting the number of intervals
Practice
4.3 (18 votes)
Advanced data structures
Data structures
Medium
Segment trees
Problem
53% Success 1872 Attempts 30 Points 3s Time Limit 512MB Memory 1024 KB Max Code

You are given a sequence of \(N\) integers \(a_1, a_2, \dots, a_N\) and an integer \(K\). Your task is to count the number of intervals \([l, r] \) such that \(a_l + a_r + min(a_l, a_{l + 1}, \dots, a_r) \le K\).

Input format

  • First line: Two space-separated integers \(N, K\)
  • Second line: \(N\) space-separated integers denoting the elements of the sequence

Output format

  • Print the answer in a single line.

Constraints

\(1 \le N \le 5\times10^5\),  \(80\%\) test cases \(N \le 2\times 10^5\)

\(1 \le K \le 10^{18}\)

\(1 \le a_i \le 10^{18}\)

 

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
8 votes
Tags:
ApprovedMathMediumOpenTrees
Points:30
2 votes
Tags:
Data StructuresAdvanced Data StructuresSegment Trees
Points:30
16 votes
Tags:
Advanced Data StructuresArithmetic ProgressionData StructuresLazy PropagationMediumSegment Trees