Classic task

4.8

12 votes
Ad-Hoc, DSU, Data Structures, Disjoint Data Structures, Disjoint Set, Hard, 難しい
Problem

Given an undirected graph with n vertices. There are m sets of edges in this graph, the ith set is represented by 2 integers (li,ri) meaning that there are edges (li,ri),(li+1,ri1),,(ri,li) in the graph.
Find the number of connected components in this graph.

Input

The first line contains 2 integers - n,m (1n500000,1m100000).

The next m lines contain 2 integers - li,ri (1lirin).

Output

Output the number of connected components in the graph.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

The edges are (1,3),(2,2),(3,1),(2,5),(3,4),(4,3),(5,2), so there are 2 connected components: (1,3,4),(2,5).

Editor Image

?