#C7138. Merge Overlapping Intervals

    ID: 50976 Type: Default 1000ms 256MiB

Merge Overlapping Intervals

Merge Overlapping Intervals

You are given a list of intervals represented as strings in the format start-end, where start and end are integers and the interval is inclusive, i.e., it represents the set \( \{ start, start+1, \ldots, end \} \). Your task is to merge all overlapping or adjacent intervals and output the resulting list of intervals in sorted order based on their start values.

Two intervals \([a, b]\) and \([c, d]\) are considered overlapping or adjacent if \( b \geq c - 1 \). In this problem, however, the merging condition is simplified to merging if \( b \geq c \), which works correctly under the assumption that adjacent intervals (e.g. "1-2" and "2-3") should be merged into "1-3".

Example:
For input intervals: "1-3", "2-4", "6-8", "7-9", the merged intervals are: "1-4" and "6-9".

inputFormat

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

  • The first line contains an integer n, which is the number of intervals.
  • Each of the next n lines contains an interval in the format start-end, where start and end are integers and start ≤ end.

outputFormat

Print the merged intervals to stdout, each on a new line. Each interval must be printed in the format start-end.

## sample
1
1-3
1-3

</p>