#P7624. [AHOI2021初中组] 地铁
[AHOI2021初中组] 地铁
Description
The subway in City B has a long history. Line X, which Xiaoxue and Xiaokeke take, is a circular line with stations on it. The track length between any two adjacent stations is a positive integer. Now Xiaoxue made some observations and obtained pieces of information. The -th piece of information is of one of the following forms:
- The clockwise distance along the ring from to is not less than a given value ( and are two stations).
- The clockwise distance along the ring from to is not greater than a given value .
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 and , separated by spaces.
The next lines each describe one piece of information. The -th line contains four positive integers , separated by spaces, where indicates the type of the information. Stations are numbered clockwise as consecutive integers starting from 1. It is guaranteed that and .
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 , 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 , where denotes the track length from station to station clockwise.
- , total length is .
- , total length is .
- , total length is .
- , total length is .
It can be proven that no other total lengths are possible, so the answer is .
[Sample 2 Explanation]
The track length from station to station clockwise can be any positive integer.
[Constraints and Hints]
- For of the testdata, and are guaranteed.
- For another of the testdata, is the first station after in the clockwise direction.
- For another of the testdata, is the second station after in the clockwise direction.
- For another of the testdata, are guaranteed.
- For of the testdata, , , and are guaranteed.
Translated by ChatGPT 5
京公网安备 11011102002149号