As a doll master, Alice owns a wide range of dolls, and each of them has a number tip on it's back, the tip can be treated as a positive integer. (the number can be repeated). One day, Alice hears that her best friend Marisa's birthday is coming , so she decides to sent Marisa some dolls for present. Alice puts her dolls in a row and marks them from 1 to n. Each time Alice chooses an interval from i to j in the sequence ( include i and j ) , and then checks the number tips on dolls in the interval from right to left. If any number appears more than once , Alice will treat this interval as unsuitable. Otherwise, this interval will be treated as suitable.
This work is so boring and it will waste Alice a lot of time. So Alice asks you for help .
Input
There are multiple test cases. For each test case:
The first line contains an integer n ( 3≤ n ≤ 500,000) ,indicate the number of dolls which Alice owns.
The second line contains n positive integers , decribe the number tips on dolls. All of them are less than 2^31-1. The third line contains an interger m ( 1 ≤ m ≤ 50,000 ),indicate how many intervals Alice will query. Then followed by m lines, each line contains two integer u, v ( 1≤ u< v≤ n ),indicate the left endpoint and right endpoint of the interval. Process to the end of input.
Output
For each test case:
For each query, If this interval is suitable , print one line "OK". Otherwise, print one line ,the integer which appears more than once first.
Print an blank line after each case.
Sample Input
5 1 2 3 1 2 3 1 4 1 5 3 5 6 1 2 3 3 2 1 4 1 4 2 5 3 6 4 6
Sample Output
1 2 OK 3 3 3 OK
Hint
Alice will check each interval from right to left, don't make mistakes.
查询区间,从右到左第一个重复重现的数字。
按查询有右端点离线,原数组从左到右,当发现某个数字在左边已经出现过了,就把他左边的位置变成位置下标,最后查询就是区间求最大值。
#include <stdio.h>
#include <map>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 500005
#define M 50005
#define max(a,b) (a>b?a:b)
#define L t<<1
#define R t<<1|1
#define clr(a) memset(a,0,sizeof a)
#define rep(i,s,t) for(int i=s;i<t;i++)
map<int,int>mp;
map<int,int>::iterator it;
int n,a[N],k,m,ans[M];
int pos[N],to[N];
int root[N<<2];
struct Q{
int l,r,id;
}q[M];
bool cmp(Q i,Q j){
return (i.r<j.r)||(i.r==j.r&&i.l<j.l);
}
inline void add(int t,int l,int r,int x,int v){
if(l==r){
root[t]=v;
}
else{
int mid=(l+r)>>1;
if(x<=mid) add(L,l,mid,x,v);
else add(R,mid+1,r,x,v);
root[t]=max(root[L],root[R]);
}
}
inline int query(int t,int l,int r,int x,int y){
if(l>=x && r<=y) return root[t];
int mid=(l+r)>>1;
if(y<=mid) return query(L,l,mid,x,y);
else if(x>mid) return query(R,mid+1,r,x,y);
else return max(query(L,l,mid,x,mid),query(R,mid+1,r,mid+1,y));
}
int main(){
while(~scanf("%d",&n)){
mp.clear(),k=1;clr(root);
rep(i,1,n+1) scanf("%d",&a[i]),mp[a[i]]=1;
for(it=mp.begin();it!=mp.end();it++){
it->second=k;
to[k++]=it->first;
}
rep(i,1,n+1) a[i]=mp[a[i]];
scanf("%d",&m);
rep(i,0,m){
scanf("%d%d",&q[i].l,&q[i].r);
q[i].id=i;
}
sort(q,q+m,cmp);
clr(pos);clr(ans);
int j=1;
rep(i,0,m){
while(j<=q[i].r){
if(pos[a[j]]!=0){
add(1,1,n,pos[a[j]],pos[a[j]]);
}
pos[a[j]]=j;
j++;
}
ans[q[i].id]=query(1,1,n,q[i].l,q[i].r);
}
rep(i,0,m){
// printf("%d \n",ans[i]);
if(ans[i]==0) puts("OK");
else{
printf("%d\n",to[a[ans[i]]]);
}
}
printf("\n");
}
return 0;
}