Sequence II
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1863 Accepted Submission(s): 475
Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted as
a1,a2,⋯,an There are m queries.
In the i-th query, you are given two integers
li and
ri . Consider the subsequence
ali,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)ki (in ascending order, i.e.,
p(i)1<p(i)2<⋯<p(i)ki ).
Note that
ki is the number of different integers in this subsequence. You should output
p(i)⌈ki2⌉ for the i-th query.
Input
In the first line of input, there is an integer T (
T≤2 ) denoting the number of test cases.
Each test case starts with two integers n (
n≤2×105 ) and m (
m≤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×105 ).
There are two integers
li and
ri 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) . As a result, the problem became more exciting.
We can denote the answers as
ans1,ans2,⋯,ansm . Note that for each test case
ans0=0 .
You can get the correct input
li,ri from what you read (we denote them as
l‘i,r‘i )by the following formula:
li=min{(l‘i+ansi−1) mod n+1,(r‘i+ansi−1) mod n+1}
ri=max{(l‘i+ansi−1) mod n+1,(r‘i+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,⋯,pm ”, where x is the case number (starting from 1) and
p1,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
Hint
Source
2016中国大学生程序设计竞赛(长春)-重现赛
题意:给你n个数,有m次询问。对于每个询问输入l,r,表示 Al...Ar 这个区间我们得到每个数第一次出现的位置下标的排列,假设这个区间有k个不同的数,我们得到的排列是 p1<p2<p3<...<pk,求第(k+1)/2个数
解题思路:本题是强制在线,可以用主席树求出区间不同数的个数,可以倒着插入主席树,再寻找位置的中位数上就可以了
#include
#include
#include
#include
#include
#include
#include