Sequence II
Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1045 Accepted Submission(s): 251
Problem Description
Mr. Frog has an integer sequence of length n, which can be denoted as
a
1
,a
2
,⋯,a
n![]()
There are m queries.
In the i-th query, you are given two integers l
i![]()
and
r
i![]()
. Consider the subsequence
a
l
i![]()
,a
l
i+1![]()
,a
l
i+2![]()
,⋯,a
r
i![]()
![]()
.
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)
k
i![]()
![]()
(in ascending order, i.e.,
p
(i)
1
<p
(i)
2
<⋯<p
(i)
k
i![]()
![]()
).
Note that k
i![]()
is the number of different integers in this subsequence. You should output
p
(i)
⌈k
i![]()
2![]()
⌉![]()
for the i-th query.
In the i-th query, you are given two integers l
We can denote the positions(the positions according to the original sequence) where an integer appears first in this subsequence as p
Note that k
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×10
5![]()
) and m (
m≤2×10
5![]()
). There are n integers in the next line, which indicate the integers in the sequence(i.e.,
a
1
,a
2
,⋯,a
n
,0≤a
i
≤2×10
5![]()
).
There are two integers l
i![]()
and
r
i![]()
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 ans
1
,ans
2
,⋯,ans
m![]()
. Note that for each test case
ans
0
=0
.
You can get the correct input l
i
,r
i![]()
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×10
There are two integers l
However, Mr. Frog thought that this problem was too young too simple so he became angry. He modified each query to l
We can denote the answers as ans
You can get the correct input l
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: p
1
,p
2
,⋯,p
m![]()
”, where x is the case number (starting from 1) and
p
1
,p
2
,⋯,p
m![]()
is the answer.
For each test case, output one line “Case #x: p
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 1Hint![]()
题意:
求给定一个区间,在对前边一个答案进行运算后得到一个新的区间,然后问这个区间里面有k个不同的数,把它们第一次出现的位置从小到大排序,问第k/2个位置是什么;
题解:
好像是强制在线,一看要解决的问题,求区间不同数的个数和区间第K大,主席树没跑,因为他要的是第一次出现的位置,我们从后向前插入。
代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int MAX=2e5+500;
int a[MAX],root[MAX];
map<int,int >mp;
struct
{
int l,r,sum;
} T[MAX*40];
int cnt=0;
void update(int l,int r,int &x,int y,int pos,int v)
{
T[++cnt]=T[y];
x=cnt;
T[cnt].sum+=v;
if(l==r)
return ;
int mid=(r+l)>>1;
if(pos<=mid)
{
update(l,mid,T[cnt].l,T[y].l,pos,v);
}
else
update(mid+1,r,T[cnt].r,T[y].r,pos,v);
}
int query(int rt,int l,int r,int L,int R)
{
if(L<=l&&R>=r) return T[rt].sum;
int mid=(l+r)>>1;
int ans=0;
if(L<=mid)
ans+=query(T[rt].l,l,mid,L,R);
if(R>mid)
ans+=query(T[rt].r,mid+1,r,L,R);
return ans;
}
int he(int rt,int l,int r,int k)
{
if(l==r) return l;
int m=(l+r)>>1;
int tmp=T[T[rt].l].sum;
if(k<=tmp)
return he(T[rt].l,l,m,k);
else
return he(T[rt].r,m+1,r,k-tmp);
}
void init()
{
mp.clear();
cnt=0;//忘记初始化cnt,bug了很长时间。
memset(root,0,sizeof(root));
}
int main()
{
int n,t,q;
int cas=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&q);
for(int i=1; i<=n; i++) scanf("%d",&a[i]);
init();
T[n+1].l=T[n+1].r=T[n+1].sum=0;
for(int i=n; i>=1; i--)
{
if(mp[a[i]]==0)
update(1,n,root[i],root[i+1],i,1);
else
{
int tmp;
update(1,n,tmp,root[i+1],mp[a[i]],-1);
update(1,n,root[i],tmp,i,1);
}
mp[a[i]]=i;
}
printf("Case #%d:",cas++);
int ans=0;
while(q--)
{
int x,y,l,r;
scanf("%d%d",&x,&y);
x=(x+ans)%n+1;
y=(y+ans)%n+1;
l=min(x,y);
r=max(x,y);
int k=(query(root[l],1,n,l,r)+1)>>1;
// cout<<k<<endl;
ans=he(root[l],1,n,k);
printf(" %d",ans);
}
printf("\n");
}
return 0;
}