ZOJ 3633 Alice's present RMQ
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 integeru, 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
题意:
给你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的更多相关文章
- ZOJ 3633 Alice's present 倍增 区间查询最大值
Alice's present Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/vi ...
- ZOJ 3757 Alice and Bob and Cue Sports(模拟)
题目链接 题意 : 玩台球.Alice 和 Bob,一共可以进行m次,Alice 先打.有一个白球和n个标有不同标号的球,称目标球为当前在桌子上的除了白球以外的数值最小的球,默认白球的标号为0.如果白 ...
- HDU 4791 & ZOJ 3726 Alice's Print Service (数学 打表)
题目链接: HDU:http://acm.hdu.edu.cn/showproblem.php?pid=4791 ZJU:http://acm.zju.edu.cn/onlinejudge/showP ...
- zoj 3757 Alice and Bob and Cue Sports 模拟
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3757 #include<cstdio> #incl ...
- zoj 3757 Alice and Bob and Cue Sports 月赛A 模拟
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3757 题意:根据所给的台球规则,按照每次的结果计算最终两人的得分 ...
- ZOJ 3757 Alice and Bod 模拟
上次的ZJU月赛题,规则比较复杂,当时就连题意都没摸清楚,只觉得非常复杂 比完后敲啊敲啊敲,连续WA啊,该反思下自己,没按照题意来设置条件,题目中说了 白球入袋并且... 给对手加分 ,白球未入袋并且 ...
- ZOJ Monthly, November 2012
A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstri ...
- [GodLove]Wine93 Tarining Round #2
比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44704#overview 题目来源: ZOJ Monthly, June 2 ...
- 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 ...
随机推荐
- ES6和Node容易搞混淆的点
ES6 import 模块名 from XX '模块标识符' -----导入模块 import '路径 ' -----导入CSS样式 export default { } 和export ...
- mysql.connector 事务总结
mysql.connector事务总结: connection.autocommit = 0 (默认值) 事务处理 使用 connection.commit()方法 #!/usr/bin/env py ...
- Python中断言与异常的区别
异常,在程序运行时出现非正常情况时会被抛出,比如常见的名称错误.键错误等. 异常: >>> s Traceback (most recent call last): File &qu ...
- CV与IP:基础,经典以及最近发展
原文链接:http://blog.csdn.net/dcraw/article/details/7617891 原文编写层层递进,逻辑清晰: 不过看这篇转载可以少点击鼠标:http://blog.cs ...
- vue项目中引用echarts的几种方式
准备工作: 首先我们初始化一个vue项目,执行vue init webpack echart,接着我们进入初始化的项目下.安装echarts, npm install echarts -S //或 ...
- (转)Bootstrap 之 Metronic 模板的学习之路 - (6)自定义和扩展
https://segmentfault.com/a/1190000006815041 前面我们将 Metronic 的结构和源码大致浏览了一遍,Metronic 整个文件包有三百多兆,在实际项目中, ...
- MongoDB 学习笔记(六):备份与用户管理
一.启动项 1.在启动数据库服务时可以在命令行输入mongod命令,然后直接带一些参数,比如“mongod --dbpath D:\Installations\MongoDB-2.4.6\MongoD ...
- Smallest Common Multiple FreeCodeCamp
题目:找出能被两个给定参数和它们之间的连续数字整除的最小公倍数. 范围是两个数字构成的数组,两个数字不一定按数字顺序排序. 分析:首先题目的意思求一个连续数列的所有数字的最小公倍数,这连续的数字序列 ...
- U盘启动盘安装Mac OS
1.下载macOS High Sierra正式版 http://www.pc6.com/mac/gj_715_1.htm http://www.pc6.com/mac/518996.html 下载ma ...
- javaee的toString的用法
toString方法返回该对象的字符串表示,如果不用toString则返回的是地址 package Xuexi; public class Person { private String name; ...