Sequence II
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2355 Accepted Submission(s): 628
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.
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:
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.
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】不同数的个数的变形。
从后往前加入数字,在这个位置权值+1, 如果出现过,在上次的位置权值-1, 建立主席树
求出对应区间不同数个数K,最后在第L棵线段树上直接求第(K+1)/2小值。
代码:
#include<bits/stdc++.h>
#define mem(p,k) memset(p,k,sizeof(p));
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int N=300010;
int n,q,tot;
int a[N],root[N],bk[N];
int ls[N*30],rs[N*30],sum[N*30];
void update(int l,int r,int &now,int pre,int k,int val){
now=tot++;
ls[now]=ls[pre];
rs[now]=rs[pre];
sum[now]=sum[pre]+val;
if(l==r)return;
int m=(l+r)>>1;
if(k<=m) update(l,m,ls[now],ls[pre],k,val);
else update(m+1,r,rs[now],rs[pre],k,val);
}
int query(int L,int R,int l,int r,int rt){
if(L<=l && r<=R){
return sum[rt];
}
int m=(l+r)>>1,s=0;
if(L<=m) s+=query(L,R,l,m,ls[rt]);
if(R>m) s+=query(L,R,m+1,r,rs[rt]);
return s;
}
int fin(int k,int l,int r,int rt){
if(l==r)return l;
int m=(l+r)>>1;
if(k<=sum[ls[rt]]) return fin(k,l,m,ls[rt]);
return fin(k-sum[ls[rt]],m+1,r,rs[rt]);
}
int main(){
int T,cur=1;
cin>>T;
while(T--){
mem(bk,0);
mem(ls,0);
mem(rs,0);
mem(sum,0);
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)scanf("%d",a+i);
root[n+1]=0;
tot=1;
for(int i=n;i;i--){
if(bk[a[i]]){//cout<<i<<endl;
update(1,n,root[i],root[i+1],bk[a[i]],-1);
update(1,n,root[i],root[i],i,1);
//cout<<query(1,n,1,n,root[i])<<endl;
}
else update(1,n,root[i],root[i+1],i,1);
bk[a[i]]=i;
}
int ans=0;
cout<<"Case #"<<cur++<<":";
while(q--){
int l,r;
scanf("%d%d",&l,&r);
l=(l+ans)%n+1;
r=(r+ans)%n+1;
if(l>r)swap(l,r);
int s=query(l,r,1,n,root[l]);
s=(s+1)>>1;
ans=fin(s,1,n,root[l]);
printf(" %d",ans);
}
cout<<endl;
}
return 0;
}