POJ 3368 解题报告 RMQ
来源:互联网 发布:电子阅览室软件破解 编辑:程序博客网 时间:2024/06/07 14:57
Description
You are given a sequence of n integers a1 , a2 , ... , an in non-decreasing order. In addition to that, you are given several queries consisting of indices i and j (1 ≤ i ≤ j ≤ n). For each query, determine the most frequent value among the integers ai , ... , aj.
Input
The input consists of several test cases. Each test case starts with a line containing two integers n and q (1 ≤ n, q ≤ 100000). The next line contains n integers a1 , ... , an (-100000 ≤ ai ≤ 100000, for each i ∈ {1, ..., n}) separated by spaces. You can assume that for each i ∈ {1, ..., n-1}: ai ≤ ai+1. The following q lines contain one query each, consisting of two integers i and j (1 ≤ i ≤ j ≤ n), which indicate the boundary indices for the
query.
The last test case is followed by a line containing a single 0.
Output
For each query, print one line with one integer: The number of occurrences of the most frequent value within the given range.
Sample Input
10 3-1 -1 1 1 1 1 3 10 10 102 31 105 100
Sample Output
143
Source
题目大意:
解题思路:
#include<cstdio>#include<cstring>#include<iostream>using namespace std;int a[100100];int rt[100100];int dp[200100][30];int log2(int x){int res=0;while(x){x/=2;res++;}return res;}int main(){int n,q;while(~scanf("%d",&n)){memset(dp,0,sizeof(dp));memset(rt,0,sizeof(rt));if(n==0)return 0;scanf("%d",&q);for(int i=1;i<=n;i++)scanf("%d",&a[i]);int now=n;rt[n]=n;for(int i=n-1;i>=1;i--){if(a[i]<a[now])now=i;rt[i]=now;}dp[1][0]=1;for(int i=2;i<=n;i++){if(a[i]==a[i-1])dp[i][0]=dp[i-1][0]+1;else dp[i][0]=1;}int log2n=log2(n)-1;for(int i=1;i<=log2n;i++){for(int j=1;j<=n;j++){dp[j][i]=max(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);}}while(q--){int l,r;scanf("%d%d",&l,&r);if(rt[l]>=r)printf("%d\n",r-l+1);else{int tmp=rt[l]-l+1;int len=r-rt[l];int log2len=log2(len)-1;tmp=max(tmp,max(dp[rt[l]+1][log2len],dp[r-(1<<(log2len))+1][log2len]));printf("%d\n",tmp);}}}}
- POJ 3368 解题报告 RMQ
- POJ 3264 解题报告 RMQ 问题 ST算法
- 解题报告:POJ 3162 Walking Race 树型DP+单调RMQ
- 线段树 ST算法 RMQ poj 3264 Balanced Lineup 解题报告
- [bzoj3489] A simple rmq problem 解题报告
- poj数百篇解题报告
- poj 1141解题报告
- poj 1001解题报告
- poj 1011 解题报告
- poj 1087 解题报告
- poj 2153解题报告
- poj 2051解题报告
- POJ 1167 解题报告
- poj 1026 解题报告
- POJ 1077 解题报告
- POJ 1042 解题报告
- POJ 1095 解题报告
- POJ 1159解题报告
- ASP.NET下调用ffmpeg与mencoder实现视频转换截屏
- XSL 语言入门4 -- xsl实际项目应用示例1
- android用intent打开各种文件
- spring jdbdTemplate详解
- hadoop 伪分布 搭建
- POJ 3368 解题报告 RMQ
- UILabel设置不同的字体颜色
- VC++中,CString,in,char,等数据类型的相互转化
- 425 Can't open data connection
- 《程序员的自我修养》读书笔记 PAGE77 图3-6理解
- Json字符串与Json对象互转
- Windows Server 2008 R2 安装及配置
- 线程,进程间的通讯和同步原理,实现用例和应用
- js中闭包与原型链的含义及用途