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 ...
随机推荐
- ps -aux ,ps aux ,ps -ef 的区别
Linux中的ps命令是Process Status的缩写.ps命令用来列出系统中当前运行的那些进程.ps命令列出的是当前那些进程的快照,就是执行ps命令的那个时刻的那些进程,如果想要动态的显示进程信 ...
- Windows下Python + AutoCAD 多义线绘图小结
简介 在windows下台下, 使用comtypes库, 通过ActiveX操作autocad, 从而读取AutoCAD数据 comtypes.client AutoCAD ActiveX GetAc ...
- BZOJ 3065 替罪羊树+动态开节点线段树
思路: RT 可以看VFK的题解 我写了半天拍了半天... 不过是$nlog^2n$的 要写垃圾回收的 线段树 如果某个节点的sum是0 也可以free掉 //By SiriusRen #inclu ...
- BZOJ 4547 矩阵快速幂
思路: 肯定每回只加最大值和次大值 如果 一开始的最大值>0且次大值<0 那就一直加 加到次大值>0 搞一个矩阵 推斐波那契数列 求和 就好- //By SiriusRen #inc ...
- javascript变量中基本类型和引用类型的详解解读
前言: Javascript语言中的变量和其他语言的变量有很大区别,javascript松散类型的本质,决定了它只是在特定时间时间保存特定值得名字而已.由于不存在定义某个变量必须保存何种数据类型值的规 ...
- WebService 服务接口
天气预报Web服务,数据来源于中国气象局Endpoint :http://www.webxml.com.cn/WebServices/WeatherWebService.asmxDisco :http ...
- Ext未定义问题解决
做的项目用到EXT.NET,调试时候没问题,发布到IIS上出现EXT未定义,把项目的应用程序池改为Classic 模式就可以了.
- 目录处理文件&链接命令
一.目录处理文件 1.删除文件或目录 rm -rf [文件或目录] //remove:删除文件或目录 -r:删除目录 -f:强制 2.复制文件或目录 cp [选项] [原文件或 ...
- Bootstrap 有一个 class 属性叫做 well,它的作用是为设定的列创造出一种视觉上的深度感
Bootstrap 有一个 class 属性叫做 well,它的作用是为设定的列创造出一种视觉上的深度感
- Mysql 5.7 for windows 免安装版(解压版)安装和配置
网上写的不近详细,这里重新整理下. 准备: 1.windows操作系统 2.mysql 的解压版压缩文件 第一步: 解压mysql的压缩包到你的安装目录,因为是虚拟机,这里我就安装在C盘下:C:\my ...