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. Vue —— You may use special comments to disable some warnings. Use // eslint-disable-next-line to ignore the next line. Use /* eslint-disable */ to ignore all warnings in a file.问题

    方法1: 在build/webpack.base.conf.js文件中,找到module->rules中有关eslint的规则,注释或者删除掉就可以了 module: { rules: [ // ...

  2. JS中兼容问题的汇总

    获取非行内样式的兼容方式 function getStyle(obj,attr){ //获取非行间样式,obj是对象,attr是值 if(obj.currentStyle){ //针对ie获取非行间样 ...

  3. wepy_two

    2.代码高亮WebStorm/PhpStorm(其他工具参见:wepy官网代码高亮) (1)打开Settings,搜索Plugins,搜索Vue.js插件并安装. (2) 打开Settings,搜索F ...

  4. Hibernate Validation与Spring整合各注解的用法Demo

    转自:https://www.aliyun.com/jiaocheng/1315650.html <dependency> <groupId>org.hibernate< ...

  5. js 一个不得不注意的浏览器兼容性问题 进制转换

    写几行JS代码 var num = '022'; alert(num+' '+parseInt(num)+' '+parseInt(num,10)); 不同的浏览器将会得到不同的结果在谷歌浏览器下的结 ...

  6. 网络拓扑_VLAN与Trunk配置

    实验目的: 1.实现VLAN10的两台主机互通; VLAN20的两台主机互通; 2.VLAN10与VLAN20主机不能互通. 拓扑图: 步骤: 1.依图配置PC1,PC2,PC3,PC4的IP,掩码, ...

  7. HTTP RESTful服务开发 spring boot+Maven +Swagger

    这周配合第三方平台整合系统,需要提供HTTP REST服务和使用ActiveMQ推送消息,研究了下,做个笔记. 1.使用eclipse创建Spring Boot项目  创建Spring Boot项目( ...

  8. [APIO2009]抢掠计划(Tarjan,SPFA)

    [APIO2009]抢掠计划 题目描述 Siruseri 城中的道路都是单向的.不同的道路由路口连接.按照法律的规定, 在每个路口都设立了一个 Siruseri 银行的 ATM 取款机.令人奇怪的是, ...

  9. bootstrap-table使用stickyHeader固定表头时,表头不跟随表体水平滚动问题解决

    解决方法: onAll: function () { // 修复stickyHeader表头不跟随表体水平滚动的问题 if (params.stickyHeader) { var fixedTable ...

  10. centos7标准版命令界面和图形界面相互切换

    1.root登陆终端 2.输入命令 vi /etc/inittab ,查看两种界面的启动模式: 3.退出vi模式,,输入命令systemctl get-default 查看当前系统启动模式:我的是命令 ...