Alice's present

Description

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 jin 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 integeruv ( 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

题意:

  给你n个数m次询问,每次询问l,r,问你l,r内一个个出现重复的数是多少

题解:

  我们预处理出当前这个数上一次出现的位置,否则是0

  在跑RMQ,找最大位置就好了

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include<map>
using namespace std;
const int N = 5e5+, M = , mod = 1e9 + , inf = 0x3f3f3f3f;
typedef long long ll;
int n,a[N],dp[N][];
map<int,int> mp;
void RMQ_init() {
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++) dp[i][] = a[i];
for(int j=;(<<j)<=n;j++) {
for(int i=;i + (<<j) - <= n; i++) {
if(dp[i][j-] > dp[i+(<<(j-))][j-])
dp[i][j] = dp[i][j-];
else {
dp[i][j] = dp[i+(<<(j-))][j-];
}
}
}
}
int rmq(int l,int r) {
if(l==r) return a[l];
int k = (int) (log((double) r-l+) / log(2.0));
return max(dp[l][k], dp[r - (<<k) + ][k]);
}
int main() {
//cout<<(1<<19)<<endl;
while(scanf("%d",&n)!=EOF) {
int tmp[N];
tmp[] = ;
for(int i=;i<=n;i++) scanf("%d",&a[i]), tmp[i] = a[i];
mp.clear();
for(int i=;i<=n;i++) {
if(mp.count(a[i])) {
int t = a[i];
a[i] = mp[t]; mp[t] = i;
}
else mp[a[i]] = i, a[i] = ;
}
RMQ_init();
int q;
scanf("%d",&q);
for(int i=;i<=q;i++) {
int a,b,ans;
scanf("%d%d",&a,&b);
ans = rmq(a,b);
if(!ans||ans<a||ans>b) printf("OK\n");
else printf("%d\n",tmp[ans]);
}
printf("\n");
}
return ;
}

ZOJ 3633 Alice's present RMQ的更多相关文章

  1. ZOJ 3633 Alice's present 倍增 区间查询最大值

    Alice's present Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...

  2. ZOJ 3757 Alice and Bob and Cue Sports(模拟)

    题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...

  3. HDU 4791 &amp; ZOJ 3726 Alice&#39;s Print Service (数学 打表)

    题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showP ...

  4. zoj 3757 Alice and Bob and Cue Sports 模拟

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3757 #include<cstdio> #incl ...

  5. zoj 3757 Alice and Bob and Cue Sports 月赛A 模拟

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3757 题意:根据所给的台球规则,按照每次的结果计算最终两人的得分 ...

  6. ZOJ 3757 Alice and Bod 模拟

    上次的ZJU月赛题,规则比较复杂,当时就连题意都没摸清楚,只觉得非常复杂 比完后敲啊敲啊敲,连续WA啊,该反思下自己,没按照题意来设置条件,题目中说了 白球入袋并且... 给对手加分 ,白球未入袋并且 ...

  7. ZOJ Monthly, November 2012

    A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstri ...

  8. [GodLove]Wine93 Tarining Round #2

    比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44704#overview 题目来源: ZOJ Monthly, June 2 ...

  9. ZOJ 3529 A Game Between Alice and Bob(博弈论-sg函数)

    ZOJ 3529 - A Game Between Alice and Bob Time Limit:5000MS     Memory Limit:262144KB     64bit IO For ...

随机推荐

  1. Hdu-6243 2017CCPC-Final A.Dogs and Cages 数学

    题面 题意:问1~n的所有的排列组合中那些,所有数字 i 不在第 i 位的个数之和除以n的全排,即题目所说的期望,比如n=3 排列有123(0),132(2),231(3),213(2),312(3) ...

  2. C++数字图像处理(1)-伽马变换

    https://blog.csdn.net/huqiang_823/article/details/80767019 1.算法原理    伽马变换(幂律变换)是常用的灰度变换,是一种简单的图像增强算法 ...

  3. 控件——DataGridview

    控件:DataGridview    用来显示数据,      可以显示和编辑来自多种不同类型的数据源的表格数据. 一.两种显示数据的方式:手动,后台代码 主要通过后台代码:先建立三大类   然后绑定 ...

  4. javascript 将单词首字母大写,其余小写

    // 1 别人写的,我拿来参考了一下 function titleCase(str) { var array = str.toLowerCase().split(" "); for ...

  5. 使用eclipse,对spring boot 进行springloader或者devtool热部署失败处理方法

    确定配置进行依赖和配置没有错误后. 调整spring boot 的版本,因为新版本对老版本的spring boot 不能使用. 改为: <groupId>org.springframewo ...

  6. vue 返回上一页在原来的位置

    http://www.jb51.net/article/118592.htm http://blog.csdn.net/qq_26598303/article/details/51189235 htt ...

  7. (转)RabbitMQ学习之路由(java)

    http://blog.csdn.net/zhu_tianwei/article/details/40887755 参考:http://blog.csdn.NET/lmj623565791/artic ...

  8. trigger事件就是继承某一个类的事件.

    <html><head><script type="text/javascript" src="/jquery/jquery.js" ...

  9. java 1.8 内存告警问题

    Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512m; support was removed in 8.0 ...

  10. BZOJ 1264: [AHOI2006]基因匹配Match DP_树状数组_LCS转LIS

    由于有重复数字,我们以一个序列为基准,另一个序列以第一个序列每个数所在下标为这个序列每个数对应的值. 注意的是,拆值的时候按照在第一个序列中的位置从大到小排,强制只能选一个. 最后跑一边最长上升子序列 ...