zoj 3633 Alice's present

Alice's present

Time Limit: 5 Seconds       Memory Limit: 65536 KB

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 uv ( 1≤ uv≤ 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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
针对性很强,不懂3633意思勿下 有时会出现无法连接服务,重开一般就行 有什么需要改进的地方,多多建议哦,传达给您的上家就行 运行环境 需要.net 4支持。 怎么开始使用本软件 首先向上家购买注册码,注意 注册信息的QQ是3633网站的登录名,而且有绑定机器的哦~ 然后打开软件,如果有什么警告提示,直接继续或continue。 打开软件后,填入网站登录密码,设置预设方案,点击打开登录页,点击登录 点击任务中心,点击设置绑定。(自动开始功能已经关闭) 直接点击设置绑定,可以实现一键登录自动开始,但需先设置预设方案。 字体可以大点或小点吗? 鼠标点下页面,CTRL+滚动条,就可以放大缩小 可以自定义条件吗? 可以,单条件:非白号 仅白号 非秒单 仅秒单 非手机 仅手机 非福建 或非地区 非其它{地区名1,地区名2} 基5 基3 基6 三心 一心 四心 一钻 二钻 一冠 二冠 单链接 多链接 非实名 仅实名 加1 不加 不聊 要聊 组合条件:三心非白号非秒单 基5四心非手机非福建单链接 建议用基础佣金来限定,不要用非秒单来限定。 选择预设方案,如果没有就手输,输完,点设置条件,查看是否能正确设置 加QQ无反应 请将本软件添加为360或其它安全软件的信任软件。 该旺旺低于要求的最低信益 接手的单有时会超出设置的条件范围,绑定框点关闭,到已接任务里去查看,然后绑定。这种情况有时是小号前后多了空格引起的。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值