luogu#P7624. [AHOI2021初中组] 地铁
[AHOI2021初中组] 地铁
Background
AHOI2021 Junior T4.
You may choose to skip the background part.
Xiaokeke found that the algorithms she learned were actually of little use in real life, and felt very depressed. Seeing this, Xiaoxue muttered a few words, “They should still be useful, right?”
“But what if they are useless? Algorithms are just a stepping stone to a famous university.”
“I disagree. The Flea King once told me that in future research or work, we will meet again some things from informatics competitions, although it may not be as hard as informatics competitions.
“Apart from practical benefits, doing informatics competitions also lets you enjoy a lot of the fun of thinking.”
“You are right. Every time in the exam I cannot solve a problem and start doubting whether the problem is wrong, after the exam when I read the editorial I always feel both annoyed and happy—why couldn’t I think of such a natural idea!”
A seed of a theoretical computer scientist quietly sprouted.
The sandstorm suddenly, as if by magic, dispersed. The two, who really could not sit still anymore, decided to go out and take the subway to wander around, getting off at will. Even without trying on purpose, Xiaoxue came up with an interesting problem on the subway. Can you solve it?
Problem 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