http://poj.org/problem?id=3368

Frequent values
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 24727   Accepted: 8612

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

Source

题意:排好序的序列,询问区间值连续出现最多的次数。st表
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5 + ;
const int M = 1e5 + ;
int dp[N][] , a[N] , b[N] ;//b数组统计连续相同个数
int n , q ; void RMQ()
{
for(int i = ; i <= n ; i++)
{
dp[i][] = b[i];
}
for(int j = ; j < ; j++)
{
for(int i = ; i <= n ; i++)
{
if(i + ( << j) - <= n)
{
dp[i][j] = max(dp[i][j-] , dp[i+(<<(j-))][j-]);
}
}
} } int query(int l , int r)
{
if(r < l) return ;//这个条件不能少,如果查询左右端点相等时,l将大于r
int ans = ;
int k = (int)log2(r - l + );
ans = max(dp[l][k] , dp[r - ( << k) + ][k]);
return ans ;
} int main()
{ while(~scanf("%d" , &n) && n)
{ scanf("%d" , &q);
//init();
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
b[i] = ;
}
for(int i = ; i <= n ; i++)
{
if(a[i] == a[i-])
{
b[i] = b[i-] + ;
}
else
{
b[i] = ;
}
}
RMQ();
for(int i = ; i <= q ; i++)
{
int l , r , ans = ;
scanf("%d%d" , &l , &r);
int t = l ;
while(t <= r && a[t] == a[t-])//以防左端点被截断
{
t++;
}
ans = max(t - l , query(t , r));//左端点被截断的话,需要用值减去下标
printf("%d\n" , ans);
} } return ;
}

线段树

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <algorithm>
#include <iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include <stdio.h>
#include <string.h>
#include <vector>
#define ME(x , y) memset(x , y , sizeof(x))
#define SF(n) scanf("%d" , &n)
#define rep(i , n) for(int i = 0 ; i < n ; i ++)
#define INF 0x3f3f3f3f
using namespace std;
const int N = 1e5 + ;
const int M = 1e5 + ;
int dp[N][] , a[N] , b[N] ;//b数组统计连续相同个数
int n , q , k = , ans = ; struct node{
int l , r , val ;
}tree[N << ]; void build(int l , int r , int root)
{
tree[root].l = l , tree[root].r = r ;
if(l == r)
{
tree[root].val = b[k++];
// cout << root << " " << tree[root].val << endl ;
return ;
}
int mid = (l + r) >> ;
build(l , mid , root*);
build(mid+ , r , root*+);
tree[root].val = max(tree[root*].val , tree[root*+].val);
} void query(int l , int r , int root)
{
if(r < l)
return ;
if(tree[root].l >= l && tree[root].r <= r)
{
ans = max(tree[root].val , ans) ;
return ;
}
int mid =(tree[root].l + tree[root].r) >> ;
if(l <= mid)
query(l , r , root*);
if(r > mid)
query(l , r , root*+);
} int main()
{ while(~scanf("%d" , &n) && n)
{ scanf("%d" , &q);
//init();
for(int i = ; i <= n ; i++)
{
scanf("%d" , &a[i]);
b[i] = ;
}
for(int i = ; i <= n ; i++)
{
if(a[i] == a[i-])
{
b[i] = b[i-] + ;
}
else
{
b[i] = ;
}
}
// memset(tree , 0 ,sizeof(tree));
k = ;
build( , n , );
for(int i = ; i <= q ; i++)
{
int l , r , sum = ;
scanf("%d%d" , &l , &r);
int t = l ;
while(t <= r && a[t] == a[t-])//以防左端点被截断
{
t++;
}
query(t , r , ); sum = max(ans , t - l);
printf("%d\n" , sum);
ans = ;
} } return ;
}

RMQ(连续相同最大值)的更多相关文章

  1. SPOJ - GSS1-Can you answer these queries I 线段树维护区间连续和最大值

    SPOJ - GSS1:https://vjudge.net/problem/SPOJ-GSS1 参考:http://www.cnblogs.com/shanyr/p/5710152.html?utm ...

  2. uva 11235 RMQ范围最大值

    题目大意:给一个整数上升序列,对于一系列询问区间(i,j),回答这段区间出现次数最多值所出现的次数. 分析:一个上升序列,相同的值聚集在一起,把相同的值的区间看作一个整体,假设这样的整体有n个,把他们 ...

  3. Can you answer these queries I SPOJ - GSS1 (线段树维护区间连续最大值/最大连续子段和)

    You are given a sequence A[1], A[2], ..., A[N] . ( |A[i]| ≤ 15007 , 1 ≤ N ≤ 50000 ). A query is defi ...

  4. POJ 3419 Difference Is Beautiful (DP + 二分 + rmq)

    题意:给n个数(n<=200000),每个数的绝对值不超过(10^6),有m个查询(m<=200000),每次查询区间[a,b]中连续的没有相同数的的最大长度. 析:由于n太大,无法暴力, ...

  5. HDU 1540 / POJ 2892 Tunnel Warfare (单点更新,区间合并,求包含某点的最大连续个数)

    题意:一条线上有n个点,D x是破坏这个点,Q x是表示查询x所在的最长的连续的点的个数,R是恢复上一次破坏的点. 思路:这题的关键是查询. 将被毁的村庄看成空位,当查询某个点的时候,如果我们知道它左 ...

  6. HDU 5289 Assignment(2015 多校第一场二分 + RMQ)

    Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total ...

  7. Hlg 1832 【线段树 && RMQ】.cpp

    题意: 在给出的区间内求出最大买进卖出的差价. 思路: 对于弱数据:维护一个从左到右的最大差价和最小值.即当发现当前值比最小值小的时候更新最小值,否则看一下当前值与之前最小值的差价是否比最大差价大,是 ...

  8. POJ 3368/RMQ/线段数

    题目链接 /* 给出一段序列,询问[L,R]区间内最大相同数的个数. 用一个很巧妙地方法,转化成求区间内的最大值的问题. RMQ维护区间最大值. MAX处理: */ for(int i=1;i< ...

  9. POJ3264/RMQ

    题目链接 /* 询问一段区间内的元素差值最大是多少,用RMQ维护一个最大值和一个最小值,相减即可. */ #include<cstdio> #include<cstring> ...

随机推荐

  1. 前端开发HTML&css入门——盒子模型以及部分CSS样式

    CSS处理网页时,它认为每个元素都包含在一个不可见的盒子里.• 为什么要想象成盒子呢?因为如果把所有的元素都想象成盒子,那么我们对网页的布局就相当于是摆放盒子.• 我们只需要将相应的盒子摆放到网页中相 ...

  2. GB28181 To RTMP/HLS/HTTP-FLV/DASH Gateway

    I. Deployment  / Architecture Block Diagram II. Resources Used 1. freeswitch —— sip server https://f ...

  3. MySQL的删除语句

    虽然现在数据库空间越来越大,但处理数据时候还是有要删除的时候,以下整理了一些最常用的删除语句. 分成两种 一个是删除指定数据,另一个删除所有数据. 一.删除指定数据 DELETE FROM 表名 WH ...

  4. 使用Jmeter聚合报告生成对比图表

    背景 最近在帮别的项目组执行性能测试,使用的工具是Jmeter.接口录制和参数化前一个人已经做好了,我主要的工作就是执行脚本,撰写测试报告.事情并不复杂,可做起来却极为耗时. 首先,由于有6组账号,分 ...

  5. nodejs和npm之间的关系

    Node.js是JavaScript的一种运行环境,是对Google V8引擎进行的封装.是一个服务器端的javascript的解释器. 包含关系,nodejs中含有npm,比如说你安装好nodejs ...

  6. 手工实现Array List和Linked List

    Array List样例: /** * 增加泛型 * 自动增加数组容量 * 增加set.get方法:增加数组边界的检查 * 增加remove方法 */package cn.study.lu.four; ...

  7. Netty模型

  8. [CSP-S模拟测试]:周(week)(搜索)

    题目描述 退役之后,$liu\_runda$总会想起学$OI$的时候自己怎样被郭神虐爆……$liu\_runda$学文化课的时候想要学$OI$,学$OI$的时候想要学文化课.为了解决矛盾,他决定以周为 ...

  9. list_car()函数小记

    一 ,list_car  ,前端传过来参数字典,从字典中获取参数 二, 根据参数去数据库中查找,条件查找 三,将查找出来的对象,flask_sqlalchemy.BaseQuery,然后通过这个对象的 ...

  10. python之面向过程,函数式编程,面向对象浅析

    python编程有面向过程.面向函数.面向对象三种,那么他们区别在哪呢?这个问题,让我想起我在学习编程的时候,我的老师给我举的例子.分享给大家. 面向过程就是将编程当成是做一件事,要按步骤完成! 比如 ...