#K84912. Maximum Passengers on a Train

    ID: 36525 Type: Default 1000ms 256MiB

Maximum Passengers on a Train

Maximum Passengers on a Train

You are given a train with (N) stations numbered from 1 to (N) and (M) passengers. Each passenger is described by two integers (S) and (D), where (S) is the boarding station and (D) is the departure station. When a passenger boards the train at station (S), they remain on the train until station (D) (inclusive of boarding and leaving immediately after (D)).

Your task is to calculate the maximum number of passengers present on the train at any point during its journey.

A useful approach is to use a difference array (or prefix sum) method. For each trip, you can increment the count at (S) and decrement it just after (D), then compute the prefix sums to simulate the number of passengers at each station.

For example, if the input is:

5 3
1 3
2 5
3 5

The maximum number of passengers on the train at any point is 3.

inputFormat

The input is read from standard input (stdin). The first line contains two integers (N) and (M), representing the number of stations and the number of trips respectively. Each of the next (M) lines contains two integers (S) and (D) representing the boarding and departure stations of a passenger.

outputFormat

Output a single integer to standard output (stdout): the maximum number of passengers on the train at any point in time.## sample

5 3
1 3
2 5
3 5
3

</p>