#P4501. [ZJOI2018] 胖

    ID: 3445 远端评测题 3000ms 512MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2018线段树二分各省省选浙江st表,稀疏表

[ZJOI2018] 胖

Description

Cedyks is a rich boy. He lives in the famous The Place (palace).

Cedyks is a hard-working boy. Every day he solves different problems to train his The Salt (soul).

One day, he plans to build a wall around his palace. There are nn watchtowers on the wall. You can regard the wall as a line segment, and the watchtowers as nn points on the segment, where 11 and nn are the two endpoints of the wall. The distance between watchtower ii and watchtower i+1i + 1 is wiw_i, and the road between them is bidirectional.

The wall is built soon. Now Cedyks starts planning the roads from his palace to the wall. Because of the problem title, Cedyks decides to measure a construction plan by the sum of the shortest path lengths from his palace to every watchtower.

Now Cedyks has mm design plans. In the kk-th plan, TkT_k bidirectional roads will be built between the palace and the watchtowers. The ii-th road connects to watchtower aia_i and has length lil_i.

Computing the sum of shortest paths to every watchtower is heavy work. Originally, Cedyks wanted to use the well-known SPFA algorithm, but because his butter (buffer) is too small, he has to use the primitive Bellman-Ford algorithm instead. The process is roughly as follows:

  1. Define the palace as node 00, and the ii-th watchtower as node ii. A bidirectional edge (ui,vi,li)(u_i, v_i, l_i) is a bidirectional road connecting uiu_i and viv_i. Let dd be the distance array. Initially, d0=0,di=1018(i[1,n])d_0 = 0, d_i = 10^{18}(i ∈ [1, n]).
  2. Let the auxiliary array c=dc = d. For each edge (ui,vi,wi)(u_i, v_i, w_i) in order, relax it: cui=min(cui,dvi+wi)c_{u_i} = \min(c_{u_i} , d_{v_i} + w_i), cvi=min(cvi,dui+wi)c_{v_i} = \min(c_{v_i} , d_{u_i} + w_i).
  3. Let tt be the number of positions where cc and dd differ. That is, let S={icidi}S = \{i|c_i≠d_i\}, then t=St = |S|. If t=0t = 0, it means dd is the final shortest path result, and the algorithm ends. Otherwise, set d=cd = c and go back to step 2.

Because there are too many design plans to compute, Cedyks hires some people to help. To prevent them from slacking off with made-up data, he defines the checksum value of a design plan as the sum of tt every time the Bellman-Ford algorithm enters step 3 when running on this plan. He will ask several hired people to compute the same plan and compare the checksum values they provide.

You are one of the laborers hired by Cedyks. Being smart, you find that in this situation, computing the sum of shortest path lengths is very easy. However, since you have to obey, you still need to compute the checksum value for each plan to report.

Input Format

The first line contains two integers n,mn,m, representing the number of watchtowers and the number of design plans.

The next line contains n1n - 1 numbers wiw_i, representing the length of the road between watchtower ii and watchtower i+1i + 1.

Then follow mm lines, each describing a design plan. The first integer KK is the number of roads in the plan. Then follow KK pairs (ai,li)(a_i, l_i), each representing an edge from the palace to watchtower aia_i with length lil_i.

Output Format

For each design plan, output one line with one integer, representing the checksum value.

5 5
2 3 1 4
1 2 2
2 1 1 4 10
3 1 1 3 1 5 1
3 1 10 2 100 5 1
5 1 1 2 1 3 1 4 1 5 1
5
8
5
8
5

Hint

Sample Explanation

For the first design plan, the changes of dd at each stage are:

  • $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,10^{18},2,10^{18},10^{18},10^{18}] \rightarrow$ $[0,4,2,5,10^{18},10^{18}] \rightarrow [0,4,2,5,6,10^{18}] \rightarrow [0,4,2,5,6,10]$.

So the checksum value is 1+2+1+1=51+2+1+1=5.

For the second design plan, the changes of dd at each stage are:

  • $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,1,10^{18},10^{18},10,10^{18}] \rightarrow$ $[0,1,3,11,10,14] \rightarrow [0,1,3,6,10,14] \rightarrow [0,1,3,6,7,14] \rightarrow [0,1,3,6,7,11]$.

So the checksum value is 2+3+1+1+1=82+3+1+1+1=8.

For the third design plan, the changes of dd at each stage are:

  • $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,1,10^{18},1,10^{18},1] \rightarrow [0,1,3,1,2,1]$。

So the checksum value is 3+1+1=53+1+1=5.

For the fourth design plan, the changes of dd at each stage are:

  • $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,10,100,10^{18},10^{18},1] \rightarrow$ $[0,10,12,103,5,1] \rightarrow [0,10,12,6,5,1] \rightarrow [0,10,9,5,1]$。

So the checksum value is 3+3+1+1=83+3+1+1=8.

For the fifth design plan, the changes of dd at each stage are:

  • $[0,10^{18},10^{18},10^{18},10^{18},10^{18}] \rightarrow [0,1,1,1,1,1]$

So the checksum value is 55.

Constraints

Test point nn mm KK Other constraints
1,2 1000\le 1000 100\le 100 None
3,4 2×105\le 2 \times 10^5
5,6 2×105\le 2 \times 10^5 1wi,li501 \le w_i,l_i \le 50
7,8,9,10 None

For 100%100\% of the testdata, it is guaranteed that within each design plan, all aia_i are pairwise distinct and 1ain1\le a_i\le n.
For 100%100\% of the testdata, it is guaranteed that 1wi,li109,1K2×1051\le w_i,l_i\le 10^9,1\le\sum K\le 2\times 10^5.

Thanks to @Xeonacid for providing the statement.

Translated by ChatGPT 5