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 Base64图片压缩上传OSS

    this.compress(result, 800, 0.5).then(val => { //得到压缩图片 let data = val; that.file = that.dataURLto ...

  2. ASE Alpha Sprint - backend scrum 3

    本次scrum于2019.11.7再sky garden进行,持续10分钟. 参与人: Zhikai Chen, Jia Ning, Hao Wang 请假: Xin Kang, Lihao Ran, ...

  3. VirtualBox中安装CentOS 7后无法上网问题

    1.在VirtualBox的设置界面,点击“网络”, 将虚拟机的“连接方式”设置为桥接模式, “界面名称”选择笔记本的无线网卡(一般是“wireless Network”的选项) 将“接入网线”勾选上 ...

  4. MySQL第五天——日志

    日志 log_error(错误日志) 用于记录 MySQL 运行过程中的错误信息,如,无法加载 MySQL数据库的数据文件,或权限不正确等都会被记录在此. 默认情况下,错误日志是开启的,且无法禁止. ...

  5. SQL语句分类

    SQL Structured Query Language SQL是结构化查询语言,是一种用来操作RDBMS的数据库语言,当前关系型数据库都支持使用SQL语言进行操作,也就是说可以通过 SQL 操作 ...

  6. JAVA中的反射机制 详解

    主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...

  7. chrles设置断点

    1.选择你要断点的接口,右键Breakpoints 2.配置断点接口proxy>Breakpoint settings query设置为* 3.开始断点,重新抓取接口 修改入参.请求头 修改出参 ...

  8. Codeforces 846F - Random Query

    原题链接:http://codeforces.com/contest/846/problem/F 题意:给一个数列,任意取区间[l, r],问区间内不同数字的个数的期望是多少. 思路: 对于第i个数a ...

  9. InputStream类的available()方法

    InputStream类的available()方法 这个方法可以在读写操作前先得知数据流里有多少个字节可以读取需要注意的是,如果这个方法用在从本地文件读取数据时,一般不会遇到问题,但如果是用于网络操 ...

  10. NOIp 数学 (小学奥数)

    Basic knowledge \[ C_n^m=\frac{n!}{m!(n - m)!} \] 快速幂 // Pure Quickpow inline int qpow(int n, int m, ...