Under construction

Still being built. Things may change

Building a Linear Molecule

Time limit5sMemory limit1024 MB

Summary
Count the pairs (L, R) where keeping only atoms L through R leaves a molecule whose bonds form a single simple path.
Level

Medium7 of 10

Topics
Graph, Sliding window, Segment tree
Solved
No attempts yet

Input

Lev received a molecule as a material for a chemistry experiment. The molecule has NN atoms, numbered from 1 to NN with distinct labels, and MM distinct chemical bonds. Each bond joins two different atoms, so the molecule can be treated as an undirected graph. Unlike real chemistry, the molecule does not have to be connected, does not have to consist of two or more atoms, and an atom may not be linked to four or fewer other atoms.

Lev uses a state-of-the-art machine for the experiment. When Lev enters two integers 1≤L≤R≤N1 \le L \le R \le N, the machine removes every atom except the atoms numbered from LL to RR, inclusive. Removing atom xx means removing atom xx together with all bonds attached to it.

Lev must record the resulting molecule in a report, and the molecule in the report must be linear. That is, the atoms can be rearranged along a straight line so that adjacent atoms are bonded to each other and non-adjacent atoms have no bond between them.

A skilled chemist, Lev can make many such molecules. For extra credit, Lev also wants to report how many distinct linear molecules can be made. Lev needs the number of integer pairs (L,R)(L, R) that can be entered into the machine to produce a linear molecule. Write a program that counts these pairs.

Input

The first line contains two integers NN and MM, separated by a space. NN is the number of atoms in the molecule, and MM is the number of chemical bonds.

Each of the next MM lines contains two integers AiA_i and BiB_i, separated by a space. This means there is a chemical bond between atom AiA_i and atom BiB_i. The line numbers are (i+1)(i+1) for ii from 1 to MM.

Examples3

  1. Example 1

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

    Input
    8 7
    2 1
    1 4
    4 3
    3 8
    8 7
    7 5
    5 6
    
    Expected output
    17
    
  3. Example 3

    Input
    12 12
    1 2
    3 4
    5 6
    7 8
    9 10
    11 12
    2 4
    4 6
    6 8
    8 10
    10 12
    12 2
    
    Expected output
    28