Kinako Bread
Practice
4.1 (23 votes)
Algorithms
Approved
Medium
Open
Trees
Two pointer
Problem
87% Success 1048 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code

Tohka's favorite food is kinako bread, but she likes to eat bread in general too. For dinner, Shido has bought Tohka N loaves of bread to eat, arranged in a line. Of the N loaves of bread, there are M distinct types of bread numbered from 1 to M. Tohka is very touched that Shido would buy her so much bread, but she is on a diet. Of the ith type of bread, Tohka doesn't want to eat more than Ri loaves. However, the bread is so tasty that she would like to eat at least Li loaves of it. Tohka has decided that she will eat all the bread in a consecutive section of the line, and no more. How many ways can she choose a consecutive section of the line to eat?

Input:
The first line of input will have N and M.
The second line of input will have the types of bread in the line, in order, separated from each other by single spaces. It is guaranteed that each type for bread from 1 to M appears at least once.
The next M lines will contain Li and Ri, for every i from 1 to M.

Output:
The output should consist of one integer on a single line, the number of ways to pick a consecutive section of the line.

Constraints:
1 ≤ MN ≤ 105
1 ≤ LiRiN

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
6 votes
Tags:
Advanced Data StructuresData StructuresMediumSegment Trees
Points:30
25 votes
Tags:
Advanced Data StructuresData StructuresSegment Trees
Points:30
1 votes
Tags:
Data StructuresMediumSegment Trees