#P10511. 方差

    ID: 11695 远端评测题 2000ms 512MiB 尝试: 0 已通过: 0 难度: 6 上传者: 标签>数学二分洛谷原创O2优化前缀和洛谷月赛

方差

Background

Define the variance of a sequence aa of length nn as:

s2=1ni=1n(aia)2s^2=\frac{1}{n} \sum_{i=1}^n (a_i-\overline{a})^2

Here, \sum is the summation symbol. For example, i=15ai=a1+a2+a3+a4+a5\sum_{i=1}^5 a_i=a_1+a_2+a_3+a_4+a_5. a\overline{a} is the average of the sequence aa.

For example, for the sequence {3,5,1,4,2}\{3,5,1,4,2\}, a=3\overline{a}=3. Then $s^2=\frac{1}{n} \sum_{i=1}^n (a_i-\overline{a})^2=\frac{1}{5}[(3-3)^2+(5-3)^2+(1-3)^2+(4-3)^2+(2-3)^2]=2$.

Problem Description

Xiao S thinks math is very easy, so Xiao R wants to test her.

Xiao R gives Xiao S a sequence aa. This sequence consists of mm segments. The ii-th segment is given as l r b, meaning al,al+1,,ara_l,a_{l+1},\ldots,a_r are all equal to bb. It is guaranteed that any two given intervals do not overlap.

Now, Xiao R has qq queries. Each query is of the form l r, asking you to query the variance s2s^2 of the interval [l,r][l,r] (note that ll may equal rr, in which case the variance of this interval is 00).

Since the number may be a decimal, Xiao R finds it inconvenient to judge the answer directly, so he wants Xiao S to compute (rl+1)2s2mod998244353(r-l+1)^2\cdot s^2\bmod 998244353. It can be proven that (rl+1)2s2(r-l+1)^2\cdot s^2 is always an integer.

As Xiao S’s good friend, can you help her?

Input Format

The first line contains three positive integers n,m,qn,m,q, representing the length of the sequence, the number of segments, and the number of queries.

The next mm lines each contain three positive integers, representing li,ri,bil_i,r_i,b_i.

The next qq lines each contain two positive integers x,yx,y. You need to answer the variance for [x,y][x,y].

Output Format

For each query, output one integer per line, representing your answer.

5 3 15
1 1 5
2 2 7
3 5 8
1 1
1 2
1 3
1 4
1 5
2 2
2 3
2 4
2 5
3 3
3 4
3 5
4 4
4 5
5 5
0
4
14
24
34
0
1
2
3
0
0
0
0
0
0

Hint

Sample Explanation

The sequence aa is {5,7,8,8,8}\{ 5, 7, 8, 8, 8 \}. For the 12th query, the average of interval [3,5][3, 5] is a=8\overline{a} = 8, and the variance is $s^2 = \frac{1}{3} [(8 - 8)^2 + (8 - 8)^2 + (8 - 8)^2] = 0$.

Constraints

  • For 20%20\% of the testdata, n,q100n,q\leq 100.
  • For 50%50\% of the testdata, n106n\leq 10^6 and m103m\leq 10^3.
  • For another 10%10\% of the testdata, rili1000r_i-l_i\leq 1000 and q104q \leq 10^4.
  • For another 10%10\% of the testdata, m103m\leq 10^3.

For all testdata, it is guaranteed that:

  • 1lirin10181\leq l_i\leq r_i\leq n\leq 10^{18}, 1mmin(n,2×105)1\leq m\leq \min(n,2\times 10^5), 1q2×1051\leq q\leq 2\times 10^5, 1xyn1\leq x\leq y\leq n, 1bi10181\leq b_i\leq 10^{18}.
  • The testdata guarantees that for any i<ji<j, li<ljl_i<l_j, and [li,ri][l_i,r_i] and [lj,rj][l_j,r_j] do not intersect, i.e. [li,ri][lj,rj]=[l_i,r_i]\cap[l_j,r_j]=\varnothing.
  • The testdata guarantees that if we take the union of all [li,ri][l_i,r_i], then it covers all positive integers on [1,n][1,n]. That is: i=1n[li,ri]Z=[1,n]Z\bigcup_{i=1}^n[l_i,r_i] \cap \Z=[1,n] \cap \Z.

Translated by ChatGPT 5