#4515. HDU5919 Sequence II

    ID: 4515 传统题 4500ms 128MiB 尝试: 2 已通过: 1 难度: 10 上传者: 标签>难度提高+/省选-数据结构可持久化可持久化线段树可持久化数据结构

HDU5919 Sequence II

Problem Description

Mr. Frog has an integer sequence of length n n ,which can be denoted as a1,a2,,an a_1, a_2, \cdots, a_n . There are m m queries.
In the i i -th query, you are given two integers li l_i and ri r_i . Consider the subsequence $ a_{l_i}, a_{l_{i+1}}, a_{l_{i+2}}, \cdots, a_{r_i} $.
We can denote the positions (the positions according to the original sequence) where an integer appears first in this subsequence as p1(i),p2(i),,pki(i) p_{1}^{(i)}, p_{2}^{(i)}, \cdots, p_{k_i}^{(i)} (in ascending order, i.e., $ p_{1}^{(i)} < p_{2}^{(i)} < \cdots < p_{k_i}^{(i)} $).
Note that ki k_i is the number of different integers in this subsequence. You should output $ p_{\left \lceil \frac{k_i}{2} \right \rceil}^{(i)} $ for the i i -th query.

Input

In the first line of input, there is an integer T T (T2 T \leq 2 ) denoting the number of test cases.
Each test case starts with two integers n n (n2×105 n \leq 2 \times 10^5 ) and m m (m2×105 m \leq 2 \times 10^5 ). There are n n integers in the next line, which indicate the integers in the sequence (i.e., a1,a2,,an a_1, a_2, \cdots, a_n , 0ai2×105 0 \leq a_i \leq 2 \times 10^5 ).
There are two integers li l_i and ri r_i in the following m m lines.
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to li,ri l_i', r_i' (1lin 1 \leq l_i' \leq n , 1rin 1 \leq r_i' \leq n ). As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,,ansm ans_1, ans_2, \cdots, ans_m . Note that for each test case ans0=0 ans_0 = 0 .
You can get the correct input li,ri l_i, r_i from what you read (we denote them as li,ri l_i', r_i' ) by the following formula: $[ l_i = \min { (l_i' + ans_{i-1}) \mod n + 1, (r_i' + ans_{i-1}) \mod n + 1 } ]$ $[ r_i = \max { (l_i' + ans_{i-1}) \mod n + 1, (r_i' + ans_{i-1}) \mod n + 1 } ]$

Output

You should output one single line for each test case.
For each test case, output one line “Case #x: p1,p2,,pm p_1, p_2, \cdots, p_m ”, where x x is the case number (starting from 1) and p1,p2,,pm p_1, p_2, \cdots, p_m is the answer.

Sample Input

2
5 2
3 3 1 5 4
2 2
4 4
5 2
2 5 2 1 2
2 3
2 4

Sample Output

Case #1: 3 3
Case #2: 3 1

Hint

Source

HDU Online Judge

}