luogu#P7624. [AHOI2021初中组] 地铁

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

[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 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