http://acm.hdu.edu.cn/showproblem.php?pid=5919
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
题目大意:给出一个含有n个元素的序列,对于每个询问[l,r],定义p[i]=a[i]在该区间内第一次出现的下标,对序列p进行去重操作,得到了k个不同的数,输出第k/2(向上取整)大的数。
思路:如果倒着把数插入主席树的话,很容易维护相同元素只留一个的性质。定义数组pre[a[i]]表示a[i]上次出现的位置,若pre[a[i]]=-1,则我们可以在位置i插入一个数,否则说明a[i]在i位置后面出现过,我们要先把靠后的位置上的a[i]清掉(p数组定义),再在位置i上插入一个数。由于这个修改并不会对其他元素产生影响,所以可以直接修改。因为我们是倒着插数的,如果用rt[i]表示处理了a[i]之后的主席树的根节点的值,那么对于区间[l,r]的查询操作,我们从rt[l]开始先查出[l,r]的不同的元素个数,再输出从rt[l]开始的第(k+1)/2大的数即可。
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
int n,m,tot;
int rt[maxn],a[maxn],pre[maxn],ans[maxn];
struct node
{
int ls,rs,sum;
}tree[maxn*40];
void insert(int &x,int y,int l,int r,int num,int v)
{
tree[++tot]=tree[y];
x=tot;
tree[x].sum+=v;
if(l==r)
return;
int mid=(l+r)>>1;
if(num<=mid)
insert(tree[x].ls,tree[y].ls,l,mid,num,v);
else
insert(tree[x].rs,tree[y].rs,mid+1,r,num,v);
}
int query(int i,int l,int r,int x,int y)//查询区间[x,y]内不同元素的个数
{
if(x==l&&y==r)
return tree[i].sum;
int mid=(l+r)>>1;
if(y<=mid)
return query(tree[i].ls,l,mid,x,y);
else if(x>mid)
return query(tree[i].rs,mid+1,r,x,y);
else
return query(tree[i].ls,l,mid,x,mid)+query(tree[i].rs,mid+1,r,mid+1,y);
}
int cal(int i,int l,int r,int k)//查询区间[x,y]内第k大的数
{
if(l==r)
return l;
int dis=tree[tree[i].ls].sum;
int mid=(l+r)>>1;
if(k<=dis)
return cal(tree[i].ls,l,mid,k);
else
return cal(tree[i].rs,mid+1,r,k-dis);
}
int main()
{
int t;
scanf("%d",&t);
int times=0;
while(t--)
{
tot=0;
memset(pre,-1,sizeof(pre));
scanf("%d %d",&n,&m);
rt[n+1]=0;
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=n;i>=1;i--)
{
if(pre[a[i]]==-1)
insert(rt[i],rt[i+1],1,n,i,1);
else
{
int temp;
insert(temp,rt[i+1],1,n,pre[a[i]],-1); //更换位置
insert(rt[i],temp,1,n,i,1);
}
pre[a[i]]=i;
}
int l,r,k;
for(int i=1;i<=m;i++)
{
scanf("%d %d",&l,&r);
l=(l+ans[i-1])%n+1;
r=(r+ans[i-1])%n+1;
if(l>r)
swap(l,r);
k=query(rt[l],1,n,l,r); //计算[l,r]内不同数的个数
ans[i]=cal(rt[l],1,n,(k+1)/2);
}
printf("Case #%d:",++times);
for(int i=1;i<=m;i++)
printf(" %d",ans[i]);
printf("\n");
}
return 0;
}