POJ 3368 Frequent values RMQ 训练指南 好题
- #include<cstdio>
- #include<cstring>
- const int maxn=1e5+;
- const int inf=0x3f3f3f3f;
- inline int max(int x,int y)
- {
- return x>y?x:y;
- }
- int a[maxn];
- int left[maxn];
- int right[maxn];
- int num[maxn];
- int dp[maxn][];
- void init()
- {
- memset(a,,sizeof a);
- memset(left,-,sizeof left);
- memset(right,-,sizeof right);
- }
- void init_rmq(int n)
- {
- for(int i=;i<=n;i++)
- dp[i][]=a[i];
- for(int j=;(<<j)<=n;j++)
- {
- for(int i=;i+(<<j)-<=n;i++)
- {
- dp[i][j]=max(dp[i][j-],dp[i+(<<(j-))][j-]);
- }
- }
- }
- int query(int l,int r)
- {
- if(l>r)
- return ;
- int k=;
- while((<<(k+))<=r-l+)
- k++;
- return max(dp[l][k],dp[r-(<<k)+][k]);
- }
- void solve(int n,int q)
- {
- init_rmq(n);
- for(int i=;i<q;i++)
- {
- int L,R;
- scanf("%d%d",&L,&R);
- if(num[L]==num[R])
- {
- printf("%d\n",R-L+);
- continue;
- }
- int ans=max(right[num[L]]-L+,R-left[num[R]]+);
- ans=max(ans,query(num[L]+,num[R]-));
- printf("%d\n",ans);
- }
- return ;
- }
- int main()
- {
- int cnt;
- while(scanf("%d",&cnt))
- {
- if(!cnt)
- break;
- int q;
- scanf("%d",&q);
- init();
- int pre=-inf;
- int n=;
- for(int i=;i<=cnt;i++)
- {
- int u;
- scanf("%d",&u);
- if(u!=pre)
- {
- right[n]=i-;
- a[++n]=;
- left[n]=i;
- }
- else
- a[n]++;
- num[i]=n;
- pre=u;
- }
- right[n]=cnt;
- solve(n,q);
- }
- return ;
- }
POJ 3368 Frequent values RMQ 训练指南 好题的更多相关文章
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values RMQ ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- POJ 3368 Frequent values(RMQ 求区间出现最多次数的数字的次数)
题目链接:http://poj.org/problem? id=3368 Description You are given a sequence of n integers a1 , a2 , .. ...
- POJ 3368 Frequent values 【ST表RMQ 维护区间频率最大值】
传送门:http://poj.org/problem?id=3368 Frequent values Time Limit: 2000MS Memory Limit: 65536K Total S ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- poj 3368 Frequent values(段树)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13516 Accepted: 4971 ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
- (简单) POJ 3368 Frequent values,RMQ。
Description You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In ad ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
随机推荐
- URAL 1218 Episode N-th: The Jedi Tournament(强连通分量)(缩点)
Episode N-th: The Jedi Tournament Time limit: 1.0 secondMemory limit: 64 MB Decided several Jedi Kni ...
- hihoCoder#1107 : Shortest Proper Prefix (前缀树)
题目大意:在n个单词中,如果以s作为前缀的单词个数不超过5个,那么称s为proper prefix.如果s为proper prefix并且s的任何一个前缀(不包括s)都不是proper prefix, ...
- 黑马程序员——JAVA基础之包,权限
------- android培训.java培训.期待与您交流! ---------- 包(package) 对类文件进行分类管理. 给类提供多层命名空间. 写在程序文件的第一行. 类名的全称的是:包 ...
- caffe: train error: Serializing 25 layers--- Check failed: proto.SerializeToOstream(&output)
I0221 21:47:41.826748 6797 solver.cpp:259] Train net output #0: loss = 0.00413362 (* 1 = 0.0041 ...
- nginx log记录请求的头信息
记录访问的log,为了在出现特殊情况时,方便检查出现问题的地方.log_format accesslog ‘$remote_addr – $remote_user [$time_local] “$re ...
- ExtJs 入门教程
http://www.cnblogs.com/iamlilinfeng/archive/2012/12/31/2840663.html
- PHP GC垃圾回收机制之引用变量回收周期疑问
普通的引用变量的销毁大家都知道, 当unset的时候如果refcount = 0 则认为无用, 销毁. 但是手册中提到一点会有递归引用的问题,很是奇葩 代码如下 <?php $a = 1; $a ...
- playframework1.x的eclipse插件开源-playtools
playtools介绍 playframework(1.2.x)是一款令人兴奋的java restful风格的web框架,使用它已经有两年多. 其中结合eclipse开发项目往往要不断进行cmd窗口切 ...
- 05-Java 集合类详解
(1)Java集合-Collection A.集合可以理解为一个动态的对象数组,不同的是集合中的对象内容可以任意扩充 B.集合特点:性能高,容易扩展和修改 C.Collection的常用子类:List ...
- Oracle数据库—— 游标的创建和应用
一.涉及内容 游标的创建与应用 二.具体操作 (一)填空题 1.PL/SQL 程序块主要包含3个部分:声明部分.(执行部分 ).异常处理部分. 2.自定义异常必须使用(RAISE )语句引发. (二) ...