#P16582. [GKS 2016 #B] Watson and Intervals

    ID: 16555 Type: RemoteJudge 6000ms 1024MiB Tried: 0 Accepted: 0 Difficulty: 6 Uploaded By: Tags>2016扫描线差分Google Kick Start

[GKS 2016 #B] Watson and Intervals

题目描述

Sherlock and Watson have mastered the intricacies of the language C++C++ in their programming course, so they have moved on to algorithmic problems. In today's class, the tutor introduced the problem of merging one-dimensional intervals. NN intervals are given, and the iith interval is defined by the inclusive endpoints [Li,Ri][L_i, R_i], where LiRiL_i \le R_i.

The tutor defined the covered area of a set of intervals as the number of integers appearing in at least one of the intervals. (Formally, an integer pp contributes to the covered area if there is some jj such that LjpRjL_j \le p \le R_j.)

Now, Watson always likes to challenge Sherlock. He has asked Sherlock to remove exactly one interval such that the covered area of the remaining intervals is minimized. Help Sherlock find this minimum possible covered area, after removing exactly one of the NN intervals.

输入格式

Each test case consists of one line with eight integers NN, L1L_1, R1R_1, AA, BB, C1C_1, C2C_2, and MM. NN is the number of intervals, and the other seven values are parameters that you should use to generate the other intervals, as follows:

First define x1=L1x_1 = L_1 and y1=R1y_1 = R_1. Then, use the recurrences below to generate xix_i, yiy_i for i=2i = 2 to NN:

  • $x_i = (A\times x_{i-1} + B\times y_{i-1} + C_1) \text{ modulo } M$.
  • $y_i = (A\times y_{i-1} + B\times x_{i-1} + C_2) \text{ modulo } M$.

We define Li=min(xi,yi)L_i = \min(x_i, y_i) and Ri=max(xi,yi)R_i = \max(x_i, y_i), for all i=2i = 2 to NN.

输出格式

For each test case, output one line containing Case #xx: yy, where xx is the test case number (starting from 11) and yy is the minimum possible covered area of all of the intervals remaining after removing exactly one interval.

3
1 1 1 1 1 1 1 1
3 2 5 1 2 3 4 10
4 3 4 3 3 8 10 10
Case #1: 0
Case #2: 4
Case #3: 9

提示

In case 11, using the generation method, the set of intervals generated are: {[1,1]}\{[1, 1]\}. Removing the only interval, the covered area is 00.

In case 22, using the generation method, the set of intervals generated are: {[2,5],[3,5],[4,7]}\{[2, 5], [3, 5], [4, 7]\}. Removing the first, second or third interval would cause the covered area of remaining intervals to be 55, 66 and 44, respectively.

In case 33, using the generation method, the set of intervals generated are: {[3,4],[1,9],[0,8],[2,4]}\{[3, 4], [1, 9], [0, 8], [2, 4]\}. Removing the first, second, third or fourth interval would cause the covered area of remaining intervals to be 1010, 99, 99 and 1010, respectively.

Limits

1T501 \le T \le 50.

0L1R11090 \le L_1 \le R_1 \le 10^9.

0A1090 \le A \le 10^9.

0B1090 \le B \le 10^9.

0C11090 \le C_1 \le 10^9.

0C21090 \le C_2 \le 10^9.

1M1091 \le M \le 10^9.

Small dataset (Test set 1 - Visible)

1N10001 \le N \le 1000.

Large dataset (Test set 2 - Hidden)

1N5×105(500000)1 \le N \le 5 \times 10^5 (500000).