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

30291번 - Asteroid belt 서브태스크다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 1024 MB31133.333%

문제

Everyone thinks Prienai Way asteroid belt is filled with asteroids, but Lukas has a map with empty parts of the belt shown on it. In the map, asteroid belt is divided into M × N squares, each of which is either empty or full of asteroids.

Lukas wants to fly through the asteroid field. Naturally, he can only fly through empty squares.

Lukas’s spaceship can fly horizontally as long as needed, but it uses fuel to fly vertically – 1 fuel unit per each square passed vertically. Lukas’s destination is a planet in square (X2, Y2).

Figure out the smallest amount of fuel Lukas will need to consume to reach the destination.

입력

There are two numbers on the first line: asteroid field length M and height N. Lukas’s starting coordinates X1 and Y1, and the destination planet coordinates X2 ir Y2 are presented on the second line. The third line contains a single integer K – the number of empty regions.

Each of the following K lines consist of three natural numbers: hi, ai, bi, meaning that in hi-th row of the asteroid belt squares from ai to bi inclusively are free.

Both the starting point and the destination belongs to one of the K empty regions. No pair of empty regions that are on the same row touch or cross.

출력

Output one integer – lowest possible amount of fuel needed to reach the destination. If it is impossible to reach the destination, output -1.

제한

  • 1 ≤ X1, X2 ≤ M
  • 1 ≤ Y1, Y2 ≤ N, 1 ≤ ai ≤ bi ≤ M
  • 1 ≤ hi ≤ N.

서브태스크

번호배점제한
17

1 ≤ M ≤ 10, 1 ≤ N ≤ 4, 1 ≤ K ≤ 10

231

1 ≤ M ≤ 1 000, 1 ≤ N ≤ 1 000, 1 ≤ K ≤ 2 000

323

1 ≤ M ≤ 2 000, 1 ≤ N ≤ 2 000, 1 ≤ K ≤ 100 000

439

1 ≤ M ≤ 1 000 000 000, 1 ≤ N ≤ 10 000, 1 ≤ K ≤ 100 000

예제 입력 1

6 7
2 1 2 6
8
1 1 3
5 4 4
2 3 3
3 3 6
4 4 4
4 6 6
6 2 4
7 4 4

예제 출력 1

5

힌트

출처

Olympiad > Lithuanian Olympiad in Informatics > Lithuanian Olympiad in Informatics 2015/2016 > National Round (2) > 10-12 Classes 4번

채점 및 기타 정보

  • 예제는 채점하지 않는다.
(追記) (追記ここまで)

출처

대학교 대회

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

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