#P7624. [AHOI2021初中组] 地铁

    ID: 6572 远端评测题 1000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>二分2021安徽图的建立,建图负权环差分约束

[AHOI2021初中组] 地铁

Description

The subway in City B has a long history. Line X, which Xiaoxue and Xiaokeke take, is a circular line with nn stations on it. The track length between any two adjacent stations is a positive integer. Now Xiaoxue made some observations and obtained mm pieces of information. The ii-th piece of information is of one of the following forms:

  1. The clockwise distance along the ring from SiS_i to TiT_i is not less than a given value LiL_i (SiS_i and TiT_i are two stations).
  2. The clockwise distance along the ring from SiS_i to TiT_i is not greater than a given value LiL_i.

Xiaoxue wants you to compute how many different valid values the total length of Line X can take.

Input Format

The first line contains two positive integers nn and mm, separated by spaces.

The next mm lines each describe one piece of information. The ii-th line contains four positive integers typei,Si,Ti,Litype_i, S_i, T_i, L_i, separated by spaces, where typei{1,2}type_i \in \{1,2\} indicates the type of the information. Stations are numbered clockwise as consecutive integers starting from 1. It is guaranteed that 1Si,Tin1 \le S_i, T_i \le n and SiTiS_i \ne T_i.

Output Format

Output one integer in a single line, representing the required answer. If there are infinitely many possible values, output -1.

It is guaranteed that the answer is not 00, i.e. there is at least one feasible solution.

4 6
1 1 3 3
2 2 4 5
1 2 4 4
1 3 1 4
2 4 2 5
1 4 2 3
4
3 2
2 1 2 1
2 2 3 1
-1
见附加文件的 subway3.in。 
见附加文件的 subway3.ans。

Hint

[Sample 1 Explanation]

Define an array d[1..4]d[1..4], where d[i]d[i] denotes the track length from station ii to station i+1i+1 clockwise.

  1. d=[1,2,2,2]d=[1,2,2,2], total length is 77.
  2. d=[1,2,2,3]d=[1,2,2,3], total length is 88.
  3. d=[1,2,2,4]d=[1,2,2,4], total length is 99.
  4. d=[1,2,3,4]d=[1,2,3,4], total length is 1010.

It can be proven that no other total lengths are possible, so the answer is 44.

[Sample 2 Explanation]

The track length from station 33 to station 11 clockwise can be any positive integer.

[Constraints and Hints]

  • For 30%30\% of the testdata, n,m9n, m \le 9 and Li5L_i \le 5 are guaranteed.
  • For another 15%15\% of the testdata, TiT_i is the first station after SiS_i in the clockwise direction.
  • For another 20%20\% of the testdata, TiT_i is the second station after SiS_i in the clockwise direction.
  • For another 25%25\% of the testdata, n,m50n, m \le 50 are guaranteed.
  • For 100%100\% of the testdata, 3n5003 \le n \le 500, 1m5001 \le m \le 500, and 1Li1091 \le L_i \le 10^9 are guaranteed.

Translated by ChatGPT 5