Skip to main content
Back to problems
Codeforces
Easy
Arrays
Math
Sorting
On Segment's Own Points

Count how many integers belong to at least one given closed segment, then report the numbers that are covered exactly once.

Acceptance 0%
Problem Statement

You are given several closed segments on the number line. Consider every integer point that lies inside at least one segment. Your task is to determine the integer points that belong to exactly one segment and output how many such points there are.

A point is called an "own point" of a segment collection if it is covered by exactly one segment among all given segments. The problem asks for the total number of such integer points across all segments.

Input Format

  • The first line contains an integer nn — the number of segments.
  • Each of the next nn lines contains two integers lil_i and rir_i describing a segment [li,ri][l_i, r_i].

Assume segments are closed and endpoints are integers.

Output Format

  • Print a single integer: the number of integer points that are covered by exactly one segment.

Constraints

  • 1n1 \le n
  • Segment endpoints are integers.
  • Segments are closed intervals [li,ri][l_i, r_i].

The exact bounds are not specified here; use an approach suitable for counting coverage over interval endpoints.

Examples
Sample cases returned by the problem API.

Example 1

Input

3
1 3
2 5
7 7

Output

5

Explanation

Covered integers are 1,2,3,4,5,7. Points covered exactly once are 1,4,5,7, and also 2? No, 2 and 3 are covered by two segments. So the total is 4.

This example is illustrative of the counting idea; exact official samples may differ.

Premium problem context

Unlock deeper context for this problem

Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.

Guided hints
Editorial and discussion links
Concept map and variants
Sign in to unlock
Track your progress
Sign in to bookmark this problem, save notes, and manage its revision plan.