poj 3368 Frequent values(段树)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 13516 | Accepted: 4971 |
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 10
2 3
1 10
5 10
0
Sample Output
1
4
3
由于是非递减数列。同样的元素必须是连在一块的。能够统计不同元素的个数,以元素的个数建树,给出一段区间,再二分查找出如今第几个元素,特殊考虑最前和最后,中间的用线段树。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn=100000+100;
int p;
int tree[maxn<<2];
int num[maxn],sum[maxn];
int a[maxn];
void build(int rt,int l,int r)
{
if(l==r)
{
tree[rt]=num[l];
return;
}
int mid=(l+r)>>1;
build(rt<<1,l,mid);
build(rt<<1|1,mid+1,r);
tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);
}
int query(int rt,int l,int r,int L,int R)
{
if(L<=l&&R>=r)
{
return tree[rt];
}
int ans=0;
int mid=(l+r)>>1;
if(L<=mid)
ans=query(rt<<1,l,mid,L,R);
if(R>mid)
ans=max(ans,query(rt<<1|1,mid+1,r,L,R));
return ans;
} int find(int k)
{
int l=1,r=p;
int m;
while (l<=r)
{
m=(l+r)>>1;
if(k>sum[m])
l=m+1;
else if(k<sum[m])
r=m-1;
else
break;
}
if(sum[m-1]>=k)
return m-1;
else if(sum[m]>=k)
return m;
else
return m+1;
}
int main()
{
int n,m;
while(~scanf("%d",&n)&&n)
{
memset(num,0,sizeof(num));
memset(sum,0,sizeof(sum));
memset(tree,0,sizeof(tree));
scanf("%d",&m);
for(int i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
int temp;
temp=0;
p=1;
num[p]=1;
sum[0]=0;
for(int i=1;i<n;i++)
{
if(a[temp]==a[i])
{
num[p]++;
}
else
{
temp=i;
sum[p]=sum[p-1]+num[p];
p++;
num[p]=1;
}
}
sum[p]=sum[p-1]+num[p];
build(1,1,p);
int l,r;
for(int i=0;i<m;i++)
{
int u,v;
int ans=0;
scanf("%d%d",&l,&r);
u=find(l);
v=find(r);
ans=sum[u]-l+1;
if(u==v)
{
printf("%d\n",r-l+1);
continue;
}
if(u+1<=v-1)
ans=max(ans,query(1,1,p,u+1,v-1));
ans=max(ans,r-sum[v-1]);
printf("%d\n",ans);
}
}
}
版权声明:本文博客原创文章,博客,未经同意,不得转载。
poj 3368 Frequent values(段树)的更多相关文章
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- 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 ST算法/线段树
Frequent values Time Limit: 2000MS Memory Lim ...
- poj 3368 Frequent values(RMQ)
/************************************************************ 题目: Frequent values(poj 3368) 链接: http ...
- POJ 3368 Frequent values (基础RMQ)
Frequent values Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14742 Accepted: 5354 ...
- [RMQ] [线段树] POJ 3368 Frequent Values
一句话,多次查询区间的众数的次数 注意多组数据!!!! RMQ方法: 预处理 i 及其之前相同的数的个数 再倒着预处理出 i 到不是与 a[i] 相等的位置之前的一个位置, 查询时分成相同的一段和不同 ...
- POJ 3368 Frequent values(线段树区间合并)
[题目链接] http://poj.org/problem?id=3368 [题目大意] 有一个有序序列,要求区间查询出现次数最多的数 [题解] 维护每个区间左端点和右端点,以及左右的长度,还有区间的 ...
- poj 3368 Frequent values -Sparse-Table
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16537 Accepted: 5981 Description You ...
- poj 3368 Frequent values(RMQ)
题目:http://poj.org/problem?id=3368 题意:给定n个数,顺序为非下降,询问某个区间内的数出现最多的数的 出现次数.. 大白书上的 例题..算是RMQ变形了, 对 原数组重 ...
随机推荐
- HDU 1276 士兵队列训练问题
模拟题,学了一下list it=li.erase(it):指向删除后的第一个元素 #include <cstdio> #include <list> using namespa ...
- Dream
即使下着雨,天空依旧明亮,因为远方有我的梦想. ——forever97
- Flex 全屏显示方法
1,修改html-template下的index.template.html文件…增加四行 1</html> 上述文件增加了四行…见我文中有提示 2,Mxml文件: 假如一个button按 ...
- Roman Roulette(约瑟夫环模拟)
Roman Roulette Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- CF 192 Div2
A.Cakeminator 暴搞之,从没有草莓覆盖的行.列遍历 char map[30][30]; int vis[30][30]; int hang[30],lie[30]; int main() ...
- Android滑动事件冲突
首先,我们假设这样一个场景:一个ViewPager里面嵌套一个ViewPager,内部滑动方向和外部滑动方向一样时,该怎么解决这一冲突呢? 针对滑动冲突这里给出两种解决方案:外部拦截法,内部拦截法. ...
- C++对C语言的非面向对象特性扩充(2)
上一篇随笔写了关于C++在注释,输入输出,局部变量说明的扩充,以及const修饰符与C中的#define的比较,也得到了几位学习C++朋友们的帮助讲解,十分感谢,我也希望欢迎有更多学习C++的朋友一起 ...
- UltraEdit中java代码换行问题
在很多开发中始用的文本编辑器中UltraEdit是非常主流的种了,但是,随着版本的更新,最近几版中出现了以下问题 问题描述:当用UltraEd(14.0版本)编写Java代码时,当遇到{}换行时,希望 ...
- Dapper 多数据库优化
Dapper是近2年异军突起的新ORM工具,它有ado.net般的高性能又有反射映射实体的灵活性,非常适合喜欢原生sql的程序员使用,而且它源码很小,十分轻便.我写本博客的目的不是为了介绍Dapper ...
- 转: seajs手册与文档之--模块定义
模块定义 define id dependencies factory exports require require.async require.resolve require.load requi ...