#P8501. [NOI2022] 二次整数规划问题

[NOI2022] 二次整数规划问题

Problem Description

In this problem, you need to solve a famous NP problem—the quadratic integer programming problem.

The quadratic integer programming problem has variables: you need to give an integer sequence (x1,x2,,xn)(x_1, x_2, \ldots, x_n) of length nn that satisfies all conditions below.

The quadratic integer programming problem has constraints: the integer sequence you give must satisfy the following two types of constraints:

  1. The first type constrains the value of each single variable: given a positive integer kk (3k53 \leq k \leq 5) and nn intervals [li,ri][l_i, r_i] (1in1 \leq i \leq n), where 1lirik1 \leq l_i \leq r_i \leq k, your sequence must satisfy 1in\forall 1 \leq i \leq n, lixiril_i \leq x_i \leq r_i.
  2. The second type constrains the values between variables: given mm triples (pi,qi,bi)(p_i, q_i, b_i), your sequence must satisfy 1jm\forall 1 \leq j \leq m, xpjxqjbj\lvert x_{p_j} - x_{q_j} \rvert \leq b_j.

The quadratic integer programming problem has an objective function: given k2k-2 objective parameters v2,v3,,vk1v_2,v_3,\dots,v_{k-1} (note that the index range is from 2\boldsymbol{2} to k1\boldsymbol{k-1}), for an integer sequence {p1,p2,,pn}\{p_1,p_2,\dots,p_n\} with value range [1,k][1,k], let cic_i be the number of elements in the sequence whose value equals ii, and let GG be the number of integer ordered pairs (i,j)(i, j) satisfying 1i,jn1 \leq i,j \leq n and pipj1|p_i-p_j|\leq 1. Note that when ij\boldsymbol{i \neq j}, (i,j)\boldsymbol{(i, j)} and (j,i)\boldsymbol{(j, i)} are different ordered pairs. Define the weight of this sequence as

$$W(p_1, p_2, \ldots, p_n) = 10^6 G+\sum_{i=2}^{k-1} c_i v_i \text{。}$$

Your sequence needs to maximize its weight while satisfying the two types of constraints above. It is guaranteed that, under the given constraints, there exists a sequence that satisfies them.

The quadratic integer programming problem does not have to include multiple queries, but we will give qq queries. Each query provides different weight parameters v2,v3,,vk1v_2, v_3, \ldots, v_{k-1}. For each query, you need to find a sequence that satisfies the constraints and maximizes the weight. To reduce the output size, you only need to output the weight of this sequence.

Input Format

This problem contains multiple test cases. The first line contains a non-negative integer and a positive integer C,TC, T, representing the test point ID and the number of testdata. C=0C = 0 indicates that this set of data is a sample.

For each testdata, the first line contains four integers k,n,m,qk, n, m, q, describing the sequence value range, the sequence length, the number of constraints between variables, and the number of queries.

The next nn lines each contain two integers li,ril_i, r_i, describing the allowed value interval for each element in the sequence.

The next mm lines each contain three integers pj,qj,bjp_j, q_j, b_j, describing one constraint between variables.

The next qq lines each contain k2k - 2 non-negative integers v2,v3,,vk1v_2, v_3, \ldots, v_{k - 1}, describing the weight parameters of one query.

Output Format

For each query of each testdata, output one line containing one integer, representing the maximum possible weight of the sequence.

Hint

[Sample #1]

See qip/qip1.in and qip/qip1.ans in the attachment.

This sample satisfies the property of test point 11 in the Constraints.


[Sample Explanation #1]

In the first testdata, the optimal sequence for both queries is (1,2,2,1,3)(1, 2, 2, 1, 3), where c2=2,G=21c_2 = 2, G = 21.


[Sample #2]

See qip/qip2.in and qip/qip2.ans in the attachment.

This sample satisfies the property of test point 33 in the Constraints.


[Sample Explanation #2]

In the first testdata, the optimal sequences for the two queries are (4,4,3,3)(4,4,3,3) and (4,3,2,2)(4,3,2,2).


[Sample #3]

See qip/qip3.in and qip/qip3.ans in the attachment.

This sample satisfies the property of test point 55 in the Constraints.


[Sample Explanation #3]

In the first testdata, one optimal sequence for the three queries is (3,3,3,3,3)(3, 3, 3, 3, 3), (2,2,3,3,2)(2, 2, 3, 3, 2), and (3,2,4,4,2)(3, 2, 4, 4, 2).


[Sample #4]

See qip/qip4.in and qip/qip4.ans in the attachment.

This sample satisfies the property of test point 22 in the Constraints.


[Sample #5]

See qip/qip5.in and qip/qip5.ans in the attachment.

This sample satisfies the property of test point 44 in the Constraints.


[Sample #6]

See qip/qip6.in and qip/qip6.ans in the attachment.

This sample satisfies the property of test point 88 in the Constraints.


[Sample #7]

See qip/qip7.in and qip/qip7.ans in the attachment.

This sample satisfies the property of test point 1414 in the Constraints.


[Sample #8]

See qip/qip8.in and qip/qip8.ans in the attachment.

This sample satisfies the property of test point 1717 in the Constraints.


[Constraints]

Let q\sum q be the sum of qq over all testdata in a single test point. For all test points,

  • 1T6001 \leq T \leq 600.
  • In the ii-th (1iT1 \le i \le T) testdata, 1nmax(Ti,2log2T)1 \leq n \leq \max(\frac{T}{i},2 \log_2 T).
  • 3k53 \leq k \leq 5, 0m3n0 \leq m \leq 3n, 1q,q3×1051 \leq q,\sum q \leq 3 \times 10^5.
  • 1lirik1 \leq l_i \leq r_i \leq k.
  • 1pj,qjn1 \leq p_j,q_j \leq n, 0bj<k0 \leq b_j<k.
  • 0v2,,vk110120 \leq v_2,\dots,v_{k-1} \leq 10^{12}.
Test Point ID TT \leq k=k= q\sum q \leq Special Property Score
11 1010 33 200200 None 44
22 600600 3×1053 \times 10^5 66
33 1010 44 200200 44
44 600600 3×1053 \times 10^5 66
55 1010 55 300300 55
66 1515 500500 44
77 2525 750750
88 5050 10001000 66
99 8080 15001500
1010 120120 20002000 55
1111 200200 80008000 A 33
1212 400400 3×1043 \times 10^4 44
1313 600600 2×1052 \times 10^5 55
1414 200200 80008000 B 33
1515 400400 3×1043 \times 10^4 44
1616 600600 2×1052 \times 10^5
1717 120120 10510^5 C
1818 150150 2×1052 \times 10^5 55
1919 180180 3×1053 \times 10^5
2020 300300 5×1045 \times 10^4 None
2121 450450 10510^5 44
2222 600600 3×1053 \times 10^5

Special Property A: m=0m=0.

Special Property B: m10m \leq 10, and the sum of mm over all testdata in a single test point does not exceed 200200.

Special Property C: The data is generated randomly. Specifically, when generating each testdata in the test point, parameters k,n,m,qk,n,m,q and kk non-negative constants p0,p1,p2,,pk1p_0,p_1,p_2,\dots,p_{k-1} are given, and it is guaranteed that pk10p_{k-1} \neq 0. Then the data is generated by the following rules:

  • For 1in1 \leq i \leq n, independently generate x,y[1,k]x,y \in [1,k] uniformly at random, then li=min(x,y),ri=max(x,y)l_i=\min(x,y),r_i=\max(x,y).
  • Repeatedly generate triples as follows until there are mm triples:
    1. Independently generate u,v[1,n]u,v \in [1,n] uniformly at random.
    2. Randomly generate ww with weights pp (for 0ik10 \leq i \leq k-1, the probability that w=iw=i is pip0+p1++pk1\frac{p_i}{p_0+p_1+\dots+p_{k-1}}).
    3. If after adding (u,v,w)(u,v,w) to the existing set of triples, there is no sequence (x1,x2,,xn)(x_1,x_2,\dots,x_n) satisfying all constraints, then discard this triple; otherwise, keep it.
  • For each query, v2,,vk1v_2, \ldots, v_{k-1} are independently generated uniformly at random in [0,1012][0,10^{12}].

Translated by ChatGPT 5