ZJU 3633

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 u, v ( 1≤ u< vn ),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

 

 

题意求给定区间里从右向左第一个开始重复的数字,开始理解成第一个在区间里出现一次以上的数 纠结好久~

将每个数在左面出现的位置记下来,用位置的数字求RMQ

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<map>
using namespace std;

#define MAX(a,b) ((a)>(b)?(a):(b))


int a[500005];
int arr[500005];
int res[500005][20];

int pow2(int k)
{
	if(k==0)return 1;
	if(k==1)return 2;
	int t=pow2(k/2);
	if(k&1)
	{
		return t*t*2;
	}
	else
	{
		return t*t;
	}
	return -1;
}

void st(int n)
{
	memset(res,-1,sizeof(res));

	for(int i=1;i<=n;i++)
	{
		res[i][0]=arr[i];
	}
	int step=1;
	int k=1;
	while(step<=n)
	{
		for(int i=1;i<=n-2*step+1;i++)
		{
			res[i][k]=MAX(res[i][k-1],res[i+step][k-1]);
		}
		step*=2;
		k++;
	}
}

int query(int left,int right)
{
	int len=right-left+1;

	int k=(int)log2(len);
	int a=res[left][k];
	int b=res[right-pow2(k)+1][k];
	return MAX(a,b);
}

int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        map<int,int>mp;
        memset(arr,0,sizeof(arr));
        for(int i=1;i<=n;i++)
        {
            arr[i]=mp[a[i]];
            mp[a[i]]=i;
        }
        st(n);
        int c;
        scanf("%d",&c);
        for(int i=1;i<=c;i++)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            int res=query(l,r);
            if(res<l)
            {
                printf("OK\n");
            }
            else
            {
                printf("%d\n",a[res]);
            }
        }
            printf("\n");
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值