As a doll master, Alice owns a wide range of dolls, and each ofthem has a number tip on it's back, the tip can be treated as apositive integer. (the number can be repeated). One day, Alicehears that her best friend Marisa's birthday is coming , so shedecides
to sent Marisa some dolls for present. Alice puts her dollsin a row and marks them from 1 to
n. Each time Alicechooses an interval from i to j in thesequence ( include
i and j ) , and thenchecks the number tips on dolls in the interval from right to left.If any number appears more than once , Alice will treat thisinterval as unsuitable. Otherwise, this interval will be treated assuitable.
This work is so boring and it will waste Alice a lot of time. SoAlice 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 Aliceowns.
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. Thenfollowed by m lines, each line contains two integer
u,v ( 1≤ u< v≤n ),indicate the left endpoint and right endpoint of theinterval. 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 morethan 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
时间复杂度 nlogn
这个题数据不强,直接用个数组来保存而不用map也能水过
若数据强,它输入的数据 less than 2^31-1,开不了这么大的数组
#include<stdio.h> #include<map> using namespace std; #define MAXN 511111 map<int,int>num; int d[MAXN],c[MAXN]; //d数组存储位置,c数组存储数 int main(void) { int n,i,tmp,m; // freopen("d:\\in.txt","r",stdin); map<int,int>::iterator a; while(scanf("%d",&n)==1) { d[0]=-1; for(i=1;i<=n;i++) { scanf("%d",&tmp); a=num.find(tmp); if(a!=num.end()) { if(a->second>d[i-1]) { d[i]=a->second; c[i]=a->first; } else { d[i]=d[i-1]; c[i]=c[i-1]; } a->second=i; } else { d[i]=d[i-1]; c[i]=c[i-1]; num.insert(pair<int,int>(tmp,i)); } } scanf("%d",&m); int b1,b2; for(i=1;i<=m;i++) { scanf("%d%d",&b1,&b2); if(d[b2]>=b1) printf("%d\n",c[b2]); else printf("OK\n"); } printf("\n"); num.clear(); } return 0; }