Logo
(追記) (追記ここまで)

24845번 - Moving Cells 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 512 MB142240.000%

문제

Little Alice has got a modern pixel picture for her birthday.

The picture is a rectangular grid of size $n\times m$. Each column of the grid has one or more consecutive cells colored black, all the other cells are colored white.

Alice considers the picture beautiful if there is a path between any two black cells $u$ and $v$ that runs only through the black cells, each time going from a cell to a side-adjacent cell --- begin in the black cell $u,ドル then go to a side adjacent to $u$ black cell $w,ドル then go to a side adjacent to $w$ black cell, and so on, eventually reaching the black cell $v$.

Since the picture is modern, it can be changed. In one action you may select any column and move all black cells in that column one cell in the same direction --- up or down. Cells can be moved only if they do not go outside the picture.

Alice wonders what is the minimum number of actions it would take to get a beautiful black picture.

입력

The first line of input has two integers $n$ and $m$ --- the number of rows and the number of columns in the picture, respectively (1ドル \le n, m \le 100,000円$). It is guaranteed that the total number of the picture cells does not exceed 10ドル^{6}$ (1ドル \le n \cdot m \le 1,000円,000円$).

The next $m$ lines contain two integers $s_i$ and $t_i$ each --- the starting and the ending positions of black cells in the $i$-th column of the grid (1ドル \le s_i \le t_i \le n$).

출력

Output a single integer --- the minimum number of actions you need to make the given picture beautiful.

제한

예제 입력 1

9 3
1 2
4 5
7 9

예제 출력 1

4

힌트

출처

Olympiad > Russian Olympiad in Informatics > Russia High School Programming Contest > Russia High School Programming Contest 2021 C번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /