You are given an array aa consisting of nn integers, and qq queries to it. ii-th query is denoted by two integers lili and riri. For each query, you have to find any integer that occurs exactly once in the subarray of aa from index lili to index riri (a subarray is a contiguous subsegment of an array). For example, if a=[1,1,2,3,2,4]a=[1,1,2,3,2,4], then for query (li=2,ri=6)(li=2,ri=6) the subarray we are interested in is [1,2,3,2,4][1,2,3,2,4], and possible answers are 11, 33 and 44; for query (li=1,ri=2)(li=1,ri=2) the subarray we are interested in is [1,1][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 nn (1≤n≤5⋅1051≤n≤5⋅105).
The second line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤5⋅1051≤ai≤5⋅105).
The third line contains one integer qq (1≤q≤5⋅1051≤q≤5⋅105).
Then qq lines follow, ii-th line containing two integers lili and riri representing ii-th query (1≤li≤ri≤n1≤li≤ri≤n).
Output
Answer the queries as follows:
If there is no integer such that it occurs in the subarray from index lili to index ririexactly once, print 00. Otherwise print any such integer.
Example
6
1 1 2 3 2 4
2
2 6
1 2
4
0
题意:题目意思是给你N个数,然后有Q个询问每个询问给你一个区间,然后问你是否存在一个数只出现一次,若存在,则输出任意一个出现一次的数,否则输出0;
题解:我们离线处理,将所有询问按有边界小的排序,然后对于每一个新加入的数字,判断它上一次出现的位置,直到当前区间的有边界,然后判断在该区间是否
存在一个数上一次出现的位置比该区间的左边界 L 小。对于出现多次的我们只保留最近的一次,对于前面的赋值INF即可,
参考代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn=5e5+10; 4 const int INF=0x3f3f3f3f; 5 typedef pair<int,int> PII; 6 int n,q,a[maxn],pos[maxn],ans[maxn]; 7 struct Node{ 8 int l,r,id; 9 } node[maxn]; 10 11 struct Tree{ 12 int l,r; 13 PII Min_num; 14 } tree[maxn<<2]; 15 16 bool cmp(Node a,Node b) 17 { 18 return a.r==b.r? a.l<b.l : a.r<b.r; 19 } 20 21 inline void build(int l,int r,int pos) 22 { 23 tree[pos].l=l,tree[pos].r=r; 24 if(l==r) return; 25 int mid=(tree[pos].l+tree[pos].r)>>1; 26 build(l,mid,pos<<1); 27 build(mid+1,r,pos<<1|1); 28 } 29 30 inline void update(int k,int pre,int pos) 31 { 32 if(tree[k].l==tree[k].r) 33 { 34 tree[k].Min_num=PII(pre,pos); 35 return ; 36 } 37 int mid=(tree[k].l+tree[k].r)>>1; 38 if(pos<=mid) update(k<<1,pre,pos); 39 else update(k<<1|1,pre,pos); 40 tree[k].Min_num=min(tree[k<<1].Min_num,tree[k<<1|1].Min_num); 41 } 42 43 inline PII getmin(int k,int l,int r) 44 { 45 if(tree[k].l==l && tree[k].r==r) return tree[k].Min_num; 46 int mid=(tree[k].l+tree[k].r)>>1; 47 if(r<=mid) return getmin(k<<1,l,r); 48 else if(l>=mid+1) return getmin(k<<1|1,l,r); 49 else return min(getmin(k<<1,l,mid),getmin(k<<1|1,mid+1,r)); 50 } 51 52 int main() 53 { 54 scanf("%d",&n); 55 memset(pos,0,sizeof pos); 56 memset(ans,0,sizeof ans); 57 for(int i=1;i<=n;i++) scanf("%d",a+i); 58 59 scanf("%d",&q); 60 for(int i=1;i<=q;i++) scanf("%d%d",&node[i].l,&node[i].r),node[i].id=i; 61 sort(node+1,node+1+q,cmp); 62 build(1,n,1); 63 64 for(int i=1,j=1;i<=n;i++) 65 { 66 if(pos[a[i]]) update(1,INF,pos[a[i]]); 67 update(1,pos[a[i]],i); 68 for(;i==node[j].r&&j<=q;j++) 69 { 70 PII temp=getmin(1,node[j].l,node[j].r); 71 if(temp.first<node[j].l) ans[node[j].id]=a[temp.second]; 72 } 73 pos[a[i]]=i; 74 } 75 76 for(int i=1;i<=q;i++) printf("%d ",ans[i]); 77 78 return 0; 79 }