题目:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3633
离线化处理
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std ;
const int maxn= 500005;
struct node{
int x;
int id;
friend bool operator <(const node&a,const node&b){
return a.x< b.x;
}
}h[maxn];
int order[maxn];
int num[maxn];//存放原始数据
bool vis[maxn];
int main(){
int n,m;
while(~scanf("%d",&n)){
for(int i= 1;i<= n;i++){
scanf("%d",&h[i].x);
h[i].id = i;
num[i]=h[i].x ;//hash之后顺序就变了
}
sort(h+1,h+n+1);
int k= 1;
order[h[1].id] = k;
for(int i= 2;i<= n;i++){
if(h[i- 1].x == h[i].x)
order[h[i].id] = k;
else
order[h[i].id] = ++k;
}
//for(int i = 1;i<= n;i++)
// printf("%d",order[i]);
int u,v;
scanf("%d",&m);
for(int i= 1;i<= m;i++){
scanf("%d%d",&u,&v);
memset(vis, 0, sizeof(vis));
int flag= 1;
for(int k = v;k>= u;k--){
if(vis[order[k]]){
flag= 0;
printf("%d\n",num[k]);
break;
}
else
vis[order[k]]= 1;
}
if(flag)
printf("OK\n");
}
printf("\n");
}
return 0;
}