#K64697. Calculate Downtime

    ID: 32033 Type: Default 1000ms 256MiB

Calculate Downtime

Calculate Downtime

You are given a series of downtime periods for a website and a query time range. Each downtime period is represented by its start and end timestamps in the format \(yyyy-MM-dd\ HH:mm\). Your task is to calculate the total number of minutes during which the website was down within the given query range.

Formally, let the query range be \( [Q_s, Q_e] \) and a downtime period be \( [L_s, L_e] \). The overlapping part (if any) between a downtime period and the query range is \( [\max(Q_s, L_s), \min(Q_e, L_e)] \). If \( \max(Q_s, L_s) < \min(Q_e, L_e) \), then the downtime contributed by this period is the difference in minutes. Sum the downtime from all periods to obtain the result.

Note: If there is no overlap, that downtime period contributes 0 minutes.

inputFormat

The input is read from standard input and has the following format:

  • The first line contains an integer \(N\) representing the number of downtime periods.
  • The next \(N\) lines each contain two timestamps in the format \(yyyy-MM-dd\ HH:mm\) separated by a space, representing the start and end times of a downtime period.
  • The last line contains two timestamps (also in the same format) separated by a space, representing the start and end times of the query range.

All timestamps are valid and the start time is always earlier than the end time in each pair.

outputFormat

Output a single integer to standard output representing the total number of minutes the website was down during the query interval.

The answer should be computed by summing the minutes of overlap between each downtime period and the query range.

## sample
3
2023-01-01 00:00 2023-01-01 02:00
2023-01-01 03:00 2023-01-01 05:00
2023-01-01 10:00 2023-01-01 12:00
2023-01-01 01:00 2023-01-01 06:00
180