Sequence II HDU - 5919(主席树)
Mr. Frog has an integer sequence of length n, which can be denoted as a1,a2,?,ana1,a2,?,an There are m queries.
In the i-th query, you are given two integers lili and riri. Consider the subsequence ali,ali+1,ali+2,?,ariali,ali+1,ali+2,?,ari.
We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p(i)1,p(i)2,?,p(i)kip1(i),p2(i),?,pki(i) (in ascending order, i.e.,p(i)1<p(i)2<?<p(i)kip1(i)<p2(i)<?<pki(i)).
Note that kiki is the number of different integers in this subsequence. You should output p(i)?ki2?p?ki2?(i)for the i-th query.
Input
In the first line of input, there is an integer T (T≤2T≤2) denoting the number of test cases.
Each test case starts with two integers n (n≤2×105n≤2×105) and m (m≤2×105m≤2×105). There are n integers in the next line, which indicate the integers in the sequence(i.e., a1,a2,?,an,0≤ai≤2×105a1,a2,?,an,0≤ai≤2×105).
There are two integers lili and riri in the following m lines.
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l‘i,r‘i(1≤l‘i≤n,1≤r‘i≤n)li‘,ri‘(1≤li‘≤n,1≤ri‘≤n). As a result, the problem became more exciting.
We can denote the answers as ans1,ans2,?,ansmans1,ans2,?,ansm. Note that for each test case ans0=0ans0=0.
You can get the correct input li,rili,ri from what you read (we denote them as l‘i,r‘ili‘,ri‘)by the following formula:
li=min{(l‘i+ansi?1) mod n+1,(r‘i+ansi?1) mod n+1}
li=min{(li‘+ansi?1) mod n+1,(ri‘+ansi?1) mod n+1}
ri=max{(l‘i+ansi?1) mod n+1,(r‘i+ansi?1) mod n+1}
ri=max{(li‘+ansi?1) mod n+1,(ri‘+ansi?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,?,pmp1,p2,?,pm”, where x is the case number (starting from 1) and p1,p2,?,pmp1,p2,?,pm 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
題意:長度為n的序列和m次詢問,每次詢問是l到r之間按每個數第一次出現的位置排序,問中位數的位置是什么。
思路:因為我們要記錄每個數第一次出現的位置。之前bzoj有一個類似的題,bzoj題目的網址
但是這個題需要記錄第一次出現的位置,所以我們要從后往前遍歷數組,如果之前出現過,就將之前的位置清空,在當前位置記錄下來。這個操作在主席樹里操作。對于l~r,我們可以算出來這段區間里有多少個數,然后就在轉化成了求區間l ~r內第k個數的位置。
代碼如下:
努力加油a啊,(o)/~
總結
以上是生活随笔為你收集整理的Sequence II HDU - 5919(主席树)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: New Year and Old Sub
- 下一篇: Just $h$-index HDU -