ZOJ 3633 Alice's present(線段樹),zoj3633

來源:互聯網
上載者:User

ZOJ 3633 Alice's present(線段樹),zoj3633

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
51 2 3 1 231 41 53 561 2 3 3 2 141 42 53 64 6
Sample Output
12OK333OK
Hint

Alice will check each interval from right to left, don't make mistakes.


題意:給定一個區間,詢問區間有木有重複的數。

map一下,表示某個數的靠左邊相同的位置,沒有置為-1;轉化為詢問區間最大值。

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>typedef long long LL;using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )const int maxn=500000+100;int t[maxn<<2],a[maxn],b[maxn];map<int,int>f;//int hash[maxn];int n,m;void pushup(int rs){    t[rs]=max(t[rs<<1],t[rs<<1|1]);}void build(int rs,int l,int r){    if(l==r)    {        t[rs]=a[l];        return ;    }    int mid=(l+r)>>1;    build(rs<<1,l,mid);    build(rs<<1|1,mid+1,r);    pushup(rs);}int query(int rs,int l,int r,int x,int y){    if(x<=l&&y>=r)  return t[rs];    int mid=(l+r)>>1;    int res1,res2;    res1=res2=-1;    if(x<=mid)  res1=query(rs<<1,l,mid,x,y);    if(y>mid)   res2=query(rs<<1|1,mid+1,r,x,y);    return max(res1,res2);}int main(){    int x,y;    while(~scanf("%d",&n))    {        f.clear();//        CLEAR(hash,0);        REPF(i,1,n)        {            scanf("%d",&x);            b[i]=x;            if(!f[x])            {                f[x]=i;                a[i]=-1;            }            else            {                a[i]=f[x];                f[x]=i;            }        }        build(1,1,n);        scanf("%d",&m);        REP(i,m)        {            scanf("%d%d",&x,&y);            int ans=query(1,1,n,x,y);            if(ans<x) puts("OK");            else  printf("%d\n",b[ans]);        }        puts("");    }    return 0;}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.