Zoj 3633 Alice's present

来源:互联网 发布:论文账务处理程序优化 编辑:程序博客网 时间:2024/05/17 05:14
Alice'spresent

Time Limit: 5 Seconds    Memory Limit: 65536 KB

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 ton. Each time Alicechooses an interval from i to j in thesequence ( includei 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 intergerm ( 1 ≤ m≤ 50,000 ),indicate how many intervals Alice will query. Thenfollowed by m lines, each line contains two integeru,v ( 1≤ u< vn ),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

51 2 3 1 231 41 53 561 2 3 3 2 141 42 53 64 6

Sample Output

12OK333OK


时间复杂度 nlogn 
 空间复杂度n
这个题数据不强,直接用个数组来保存而不用map也能水过
若数据强,它输入的数据 less than 2^31-1,开不了这么大的数组
#include<stdio.h>#include<map>using namespace std;#define MAXN 511111map<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;} 



原创粉丝点击