http://codeforces.com/problemset/problem/1000/F
You are given an array a consisting of n integers, and q queries to it. i-th query is denoted by two integers li and ri. For each query, you have to find any integer that occurs exactly once in the subarray of a from index li to index ri (a subarray is a contiguous subsegment of an array). For example, if a=[1,1,2,3,2,4], then for query (li=2,ri=6) the subarray we are interested in is [1,2,3,2,4], and possible answers are 1, 3 and 4; for query (li=1,ri=2) the subarray we are interested in is [1,1]
, and there is no such element that occurs exactly once.
Can you answer all of the queries?
Input
The first line contains one integer n
(1≤n≤5⋅105
).
The second line contains n
integers a1,a2,…,an (1≤ai≤5⋅105
).
The third line contains one integer q
(1≤q≤5⋅105
).
Then q
lines follow, i-th line containing two integers li and ri representing i-th query (1≤li≤ri≤n
).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index li
to index ri exactly once, print 0
. Otherwise print any such integer.
Example
Input
Copy
6 1 1 2 3 2 4 2 2 6 1 2
Output
Copy
4 0
题目大意:给n个数,m个查询,输出[l,r]内只出现过1次的数,若有多个输出任意一个。
思路:之前写过一个线段树+离线的做法,如下:
https://blog.csdn.net/xiji333/article/details/88077529
现在学了主席树,发现这题用主席树也可做,补充一下:
用pre数组记录一下每个数在原序列上一次出现的位置,若第一次出现,置0即可。根据贪心的思想,只要[l,r]内pre[a[i]]的最小值是<l 的就是有解的,那么我们考虑维护这个最小值。顺序向主席树中插入节点,如果a[i]是第一次出现的,那么直接在i位置插入0,否则,把pre[a[i]]位置的数置为INF,再把i位置的数置为pre[a[i]],即消除前面的区间对现在的影响。那么从root[r]开始查询[l,r]内的pre[a[i]]的最小值就行了。还要记录一下这个最小值在数组a中的位置。这里用了pair来记录。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
#define P pair<int,int>
using namespace std;
const int maxn=5e5+5;
struct node
{
int ls,rs,val,pos;//val:这个数上一次出现的下标 pos:这个数的下标
}tree[maxn*40];
int n,m,tot;
int rt[maxn],a[maxn],pre[maxn];
void insert(int &x,int y,int l,int r,int p,int v)
{
tree[++tot]=tree[y];
x=tot;
if(l==r)
{
tree[x].val=v; //上一次出现的位置
tree[x].pos=l;
return ;
}
int mid=(l+r)>>1;
if(p<=mid)
insert(tree[x].ls,tree[y].ls,l,mid,p,v);
else
insert(tree[x].rs,tree[y].rs,mid+1,r,p,v);
if(tree[tree[x].ls].val<tree[tree[x].rs].val)
tree[x].val=tree[tree[x].ls].val,tree[x].pos=tree[tree[x].ls].pos;
else
tree[x].val=tree[tree[x].rs].val,tree[x].pos=tree[tree[x].rs].pos;
}
P query(int i,int l,int r,int x,int y)
{
if(l==x&&r==y)
{
P p;
p.second=tree[i].pos; //记录该数所在的下标
p.first=tree[i].val; //返回该数上一次出现的下标
return p;
}
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 min(query(tree[i].ls,l,mid,x,mid),query(tree[i].rs,mid+1,r,mid+1,y));
}
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=n;i++)
{
if(pre[a[i]])
{
int tmp;
insert(tmp,rt[i-1],1,n,pre[a[i]],INF);
insert(rt[i],tmp,1,n,i,pre[a[i]]);
}
else
insert(rt[i],rt[i-1],1,n,i,0);
pre[a[i]]=i;
}
scanf("%d",&m);
int l,r;
P p;
for(int i=0;i<m;i++)
{
scanf("%d %d",&l,&r);
p=query(rt[r],1,n,l,r);
if(p.first>=l)
printf("0\n");
else
printf("%d\n",a[p.second]);
}
return 0;
}