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变形了, 对 原数组重 ...
随机推荐
- Microsoft Deployment Toolkit 2013 Preview Release Now Available
MDT 2013 provides a common console with comprehensive tools and guidance for every organizational ro ...
- [置顶] JDK-CountDownLatch-实例、源码和模拟实现
Conception A synchronization aid that allows one or more threads to wait until a set of operations b ...
- HDU 3032 Nim or not Nim? (sg函数求解)
Nim or not Nim? Problem Description Nim is a two-player mathematic game of strategy in which players ...
- 具体解释coredump
一,什么是coredump 我们常常听到大家说到程序core掉了,须要定位解决,这里说的大部分是指相应程序因为各种异常或者bug导致在执行过程中异常退出或者中止,而且在满足一定条件下(这里为什么说须要 ...
- Struts2 API的chm格式帮助文档制作教程
Struts2 API的chm格式帮助文档制作教程 在SSH三个框架中,Struts2的API文档是最难做的,这里所说的格式是chm格式的,chm的格式很方便,Hibernate API文档和Spri ...
- HttpContext的解释意义
在.ashx中,我们HttpContext这个词,到底是什么意思?下面给大家说说 HttpContext 类:封装有关个别 HTTP 请求的所有 HTTP 特定的信息. 在处理请求执行链的各个阶段中, ...
- windows cmd: 打开windows系统程序或服务的常见命令
Windows常用CMD命令 http://www.cnblogs.com/sbaicl/archive/2013/03/05/2944001.html 其实查找Windows自带程序的命令行很简单, ...
- openstack第1天
入门openstack题外篇 老实说,我在写这篇文章的时候,对云的了解还是比较模糊的,也许是刚接触,不管怎样 总得写点什么,写完之后也许数月之后,感觉写的不是那么好,到时候在做修该吧! 今天我们就提一 ...
- Android操作HTTP实现和服务器通信
众所周知,Android与服务器通信通常采用HTTP通信方式和Socket通信方式,而HTTP通信方式又分get和post两种方式.至于Socket通信会在以后的博文中介绍. HTTP协议简介: HT ...
- win7安装 Apache2.2 PHP5.3 MySQL5.6
. APACHE2.2 经典参考资料 http://blog.csdn.net/yousuosi/article/details/9859507 官方下载地址 http://mirror.bi ...