This page is still under construction.

Parts of this page are still being built. What you see may change.

Crossing Diagonals

Time limit1sMemory limit128 MB

Summary
Count crossing pairs among m chosen diagonals of a regular n-gon, ignoring diagonals that only share a vertex.
Level

Medium6 of 10

Topics
Sorting, Prefix sum, Implementation
Solved
No attempts yet

Problem

You are given a regular polygon with nn vertices, and mm of its diagonals have been selected. Count how many pairs of the selected diagonals cross each other. Two diagonals cross when they intersect strictly inside the polygon; two diagonals that share only a common vertex (endpoint) are not counted as crossing.

Write a program that:

  • reads the description of the polygon and the selected diagonals from standard input,
  • counts the number of pairs of diagonals that cross,
  • writes that number to standard output.

Input

The first line contains two integers nn and mm, separated by a single space, where nn is the number of vertices of the polygon and mm is the number of selected diagonals (4≤n≤1 000 0004 \le n \le 1\,000\,000, 2≤m≤10 000 0002 \le m \le 10\,000\,000).

Each of the next mm lines describes one diagonal. The ii-th of these lines contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n), separated by a single space, denoting the diagonal that joins vertex aia_i with vertex bib_i. The vertices of the polygon are numbered from 11 to nn in counter-clockwise order.

You may assume that every pair of numbers describes a valid diagonal (never a single vertex, and never an edge of the polygon), and that all diagonals in the input are distinct.

Output

Print a single integer: the number of pairs of selected diagonals that cross. If no pair of diagonals crosses, print 00.

Hint

In the picture above, the solid lines and the dashed lines show two different sets of selected diagonals on the same polygon.

Examples2

  1. Example 1

    Input
    6 4
    3 6
    1 4
    6 2
    2 5
    
    Expected output
    4
    
  2. Example 2

    Input
    6 2
    1 3
    1 5
    
    Expected output
    0