Frequent values

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1476    Accepted Submission(s): 541

Problem 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
 
有个RMQ的解法,利用游标编码,代码简单,但是理解可能复杂点
#include<iostream>
#include<cstdio>
#include<algorithm>
#define N 100005
using namespace std; int a[N];
struct Tree{
int l,r;
int lv,rv,mv;
}tree[*N]; void PushUp(int l,int r,int idx){
tree[idx].lv = tree[idx<<].lv;
tree[idx].rv = tree[idx<<|].rv;
tree[idx].mv = max(tree[idx<<].mv,tree[idx<<|].mv);
int mid = (l+r)>>;
int len = r- l+;
if(a[mid]==a[mid+]){
if(tree[idx].lv==len-(len>>)) tree[idx].lv+=tree[idx<<|].lv;
if(tree[idx].rv==(len>>)) tree[idx].rv+=tree[idx<<].rv;
tree[idx].mv = max(tree[idx].mv,tree[idx<<].rv+tree[idx<<|].lv);
}
}
void build(int l,int r,int idx){
tree[idx].l = l;
tree[idx].r = r;
if(l==r){
tree[idx].lv = tree[idx].rv = tree[idx].mv = ;
return;
}
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
PushUp(l,r,idx);
}
int query(int l,int r,int idx){
if(tree[idx].l>=l&&tree[idx].r<=r){
return tree[idx].mv;
}
int mid = (tree[idx].l+tree[idx].r)>>;
int ans = ;
if(l<=mid) ans = max(ans,query(l,r,idx<<));
if(r>mid) ans=max(ans,query(l,r,idx<<|));
if(a[mid]==a[mid+]){
ans = max(ans,min(mid-l+,tree[idx<<].rv)+min(r-mid,tree[idx<<|].lv));
}
return ans;
}
int main()
{
int n,m;
while(scanf("%d",&n)!=EOF,n){
scanf("%d",&m);
for(int i=;i<=n;i++){
scanf("%d",&a[i]);
}
build(,n,);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b,));
}
}
return ;
}
 

hdu 1806(线段树区间合并)的更多相关文章

  1. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  2. hdu 3308(线段树区间合并)

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. HDU 3911 线段树区间合并

    北京赛区快了,准备袭击数据结构和图论.倒计时 18天,线段树区间合并.维护一个最长连续.. 题意:给一个01串,以下有一些操作,问区间最长的连续的1的个数 思路:非常裸的线段树区间合并 #includ ...

  4. hdu 3308 线段树 区间合并+单点更新+区间查询

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  5. HDU 3308 LCIS (线段树区间合并)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3308 题目很好懂,就是单点更新,然后求区间的最长上升子序列. 线段树区间合并问题,注意合并的条件是a[ ...

  6. hdu 3911 Black And White (线段树 区间合并)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3911 题意: 给你一段01序列,有两个操作: 1.区间异或,2.询问区间最长的连续的1得长度 思路: ...

  7. HDU 3308 (线段树区间合并)

    http://acm.hdu.edu.cn/showproblem.php?pid=3308 题意: 两个操作  : 1 修改 单点  a 处的值. 2 求出 区间[a,b]内的最长上升子序列. 做法 ...

  8. HDU 6638 - Snowy Smile 线段树区间合并+暴力枚举

    HDU 6638 - Snowy Smile 题意 给你\(n\)个点的坐标\((x,\ y)\)和对应的权值\(w\),让你找到一个矩形,使这个矩阵里面点的权值总和最大. 思路 先离散化纵坐标\(y ...

  9. LCIS HDU - 3308 (线段树区间合并)

    LCIS HDU - 3308 Given n integers. You have two operations: U A B: replace the Ath number by B. (inde ...

随机推荐

  1. React Render Callback Pattern(渲染回调模式)

    React Render Callback Pattern,渲染回调模式,其实是将this.props.children当做函数来调用. 例如: 要根据user参数确定渲染Loading还是Profi ...

  2. ACE线程管理机制-并发控制(4)

    转载于:http://www.cnblogs.com/TianFang/archive/2006/12/04/581857.html ACE Synchronization类 这一类并发控制对象一般也 ...

  3. HDU 2852 主席树

    KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)T ...

  4. swift的UIbutton

    override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, ...

  5. 手脱ASProtect v1.2(无Stolen Code)

    1.载入PEID ASProtect v1.2 2.载入OD > 01C04200 push 跑跑赛道.0042C001 ; //入口处 C3 retn AA stos byte ptr es: ...

  6. [技巧篇]08.Struts2拦截器中获取Servlet API方法

    讲课中遇到的解决Session拦截器的后腿问题,还有如何在拦截器中获取Servlet API,这里留一个备注,方便学生查找

  7. JavaScript实现35选7并记录历史状态

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAL4AAABQCAYAAACnOs9vAAAJy0lEQVR4nO2dbWwUxxnH/2c5SElQSl ...

  8. 页面自适应<meta name="viewport">标签设置

    viewport: 它在页面中设置,是应对手机模式访问网站.网页对屏幕而做的一些设置.通常手机浏览器打开页面后,会把页面放在一个虚拟的“窗 口”–这个比窗口大,也就是你常发现页面可以进行拖动.放大放小 ...

  9. NOI2001 方程的解数

    1735 方程的解数 http://codevs.cn/problem/1735/ 2001年NOI全国竞赛  时间限制: 5 s  空间限制: 64000 KB     题目描述 Descripti ...

  10. Eclipse中 将java Gradle项目转换为web项目

    1.找到项目工作空间目录,打开.project文件,并修改文件, 修改如下:      找到:<natures> </natures>代码段,在代码段中加入如下内容并保存:   ...