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
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.
这一题的大体思路就是先通过map判重从右向左扫描一遍b数组之中存储的是和该物品相同的,最靠右的物品的位置。当其左边没有与其相同的物品时b[i]值为0,求的结果就是l到r之中b[i]的最大值。
考虑10^5单点扫的查询复杂度O(n^2)采用分块的做法,所有的数据分为sqrt(n)块并记录每一块当中b[i]的最大值。如果[l,r]完全包含该块,则与该块的最大值比较,否则进行单点比较。复杂度约为(n^1.5)
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
using namespace std;
struct block{
int left,right,v;
}bl[1000];
int a[1000005];
int b[1000005];
map<int,int> v;
int main()
{
int n,m,i,j,k,bn,s,l,r,le,ri,ans;
while(scanf("%d",&n)!=EOF)
{
v.clear();
memset(b,0,sizeof(b));
for (i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
for (i=n;i;i--)
{
k=a[i];
if (v[k]) b[v[k]]=i;
v[k]=i;
}
s=(int) (sqrt(n));
bn=n/s+(n%s!=0);
for (i=1;i<=bn;i++)
{
bl[i].left=(i-1)*s+1;
bl[i].right=min(n,i*s);
bl[i].v=0;
for (j=bl[i].left;(j<=bl[i].right);j++)
bl[i].v=max(bl[i].v,b[j]);
}
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&l,&r);
le=l/s+2;
ri=r/s;
ans=0;
for (i=le;i<=ri;i++)
ans=max(ans,bl[i].v);
for (i=l;i<=(bl[le].left-1)&&i<=r;i++)
ans=max(ans,b[i]);
for (i=bl[ri].right+1;i<=r;i++)
ans=max(ans,b[i]);
if (ans<l) printf("OK\n");
else printf("%d\n",a[ans]);
}
printf("\n");
}
return 0;
}
还有一种使用线段树的做法
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <map>
using namespace std;
map<int,int> q;
int a[500005],node[4000005];
int n,i,k,m,ans,le,ri;
void inse(int x,int sl,int sr,int ql,int qr,int v)
{
int md;
if (node[x]<v) node[x]=v;
if (sl==ql&&sr==qr) return;
md=(sl+sr)/2;
if (ql<=md)
if (qr<=md) inse(x*2,sl,md,ql,qr,v);
else inse(x*2,sl,md,ql,md,v);
if (qr>md)
if (ql>md) inse(x*2+1,md+1,sr,ql,qr,v);
else inse(x*2+1,md+1,sr,md+1,qr,v);
}
int quer(int x,int sl,int sr,int ql,int qr)
{
if (sl==ql&&sr==qr) return(node[x]);
int t=0;
int md=(sl+sr)/2;
if (ql<=md)
if (qr<=md) t=max(t,quer(x*2,sl,md,ql,qr));
else t=max(t,quer(x*2,sl,md,ql,md));
if (qr>md)
if (ql>md) t=max(t,quer(x*2+1,md+1,sr,ql,qr));
else t=max(t,quer(x*2+1,md+1,sr,md+1,qr));
return t;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
memset(node,0,sizeof(node));
q.clear();
for (i=1;i<=n;i++)
scanf("%d",&a[i]);
for (i=n;i;i--)
{
k=a[i];
int p=q[k];
if (p)
inse(1,1,n,p,p,i);
q[k]=i;
}
scanf("%d",&m);
while (m--)
{
scanf("%d%d",&le,&ri);
ans=quer(1,1,n,le,ri);
if (ans<le) printf("OK\n");
else printf("%d\n",a[ans]);
}
printf("\n");
}
return 0;
}