#P14192. [ICPC 2024 Hangzhou R] Fuzzy Ranking

[ICPC 2024 Hangzhou R] Fuzzy Ranking

题目描述

In Pigeland, there are nn universities, numbered from 11 to nn. Each year, several ranking organizations publish rankings of these universities. This year, there are kk ranking lists, where each list is a permutation of integers from 11 to nn, representing the universities. In each ranking, the closer a university is to the beginning of the permutation, the better its ranking is in this list.

:::align{center}

A true story in the 2024 ICPC World Final. :::

Supigar, a year-4 student who wants to apply for PhD programs in Pigeland, has his own method to evaluate the nn universities comprehensively. He considers that university xx is superior\textit{superior} to another university yy if and only if:

  • xx is ranked better than yy in at least one list, or
  • xx is ranked better than z (zx,zy)z\ (z \neq x, z \neq y) in at least one list, and zz is superior to yy.

Clearly, under this definition, there might exist some pairs of universities xx and yy (x<yx < y) such that xx is superior to yy while yy is also superior to xx. Supigar calls such pairs fuzzy\textit{fuzzy}.

Supigar has qq queries, where the ii-th query can be represented by three integers idiid_i, lil_i and rir_i (liril_i \le r_i). For each query, he will consider the idiid_i-th rank list and all the universities between the lil_i-th position and the rir_i-th position (both inclusive) in that list. He wants to know, among these universities, how many pairs of them are fuzzy. Note that the definition of fuzzy pairs requires considering the superior relationships among all kk rank lists.

输入格式

There are multiple test cases. The first line of the input contains an integer TT (1T2×1051 \leq T \leq 2 \times 10^5) indicating the number of test cases. For each test case:

The first line contains three integers nn, kk, and qq (1n,k,q2×1051 \le n, k, q \leq 2 \times 10^5, 1n×k2×1051 \leq n \times k \le 2 \times 10^5) indicating the number of universities, rank lists, and queries, respectively.

For the following kk lines, the ii-th line contains nn distinct integers ai,1,ai,2,,ai,na_{i,1}, a_{i,2}, \ldots, a_{i,n} (1ai,jn1 \le a_{i,j} \le n) indicating the ii-th rank list.

For the following qq lines, the ii-th line contains three integers idiid_i', lil_i', and rir_i' (0idi<k0 \le id_i' < k, 0li,ri<n0 \le l_i', r_i' < n) indicating the encoded\textbf{encoded} index of the rank list and the query range for the ii-th query.

  • The real value of idiid_i is equal to ((idi+vi1)modk)+1((id_i' + v_{i - 1}) \bmod k) + 1.
  • The real value of lil_i is equal to ((li+vi1)modn)+1((l_i' + v_{i - 1}) \bmod n) + 1.
  • The real value of rir_i is equal to ((ri+vi1)modn)+1((r_i' + v_{i - 1}) \bmod n) + 1.

Where vi1v_{i - 1} is the answer for the (i1)(i - 1)-th query. Specifically, we define v0=0v_0 = 0. With the encoded queries, you're forced to calculate the answer to each query before processing the next one. It's guaranteed that 1idik1 \le id_i \le k and 1lirin1 \le l_i \le r_i \le n after decoding.

It is also guaranteed that neither the sum of n×kn \times k nor the sum of qq of all test cases will exceed 2×1052 \times 10^5.

输出格式

For each test case output qq lines. Each line contains a single integer representing the number of fuzzy pairs as the answer to the ii-th query.

2
5 2 2
1 2 3 4 5
5 4 3 2 1
1 0 2
1 2 1
5 3 3
1 2 3 4 5
1 3 2 4 5
1 2 3 5 4
0 0 2
0 2 3
1 0 3
3
10
1
1
2

提示

For the first sample test case, the two decoded queries are 2 1 3\texttt{2 1 3} and 1 1 5\texttt{1 1 5}.

For the second sample test case, the three decoded queries are 1 1 3\texttt{1 1 3}, 2 4 5\texttt{2 4 5}, and 3 2 5\texttt{3 2 5}.