hdu 1806(线段树区间合并)
2024-10-19 07:39:19
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 .
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.
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
-1 -1 1 1 1 1 3 10 10 10
2 3
1 10
5 10
0
Sample Output
1
4
3
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 ;
}
最新文章
- iOS 触摸事件与UIResponder(内容根据iOS编程编写)
- 解决JSP页面获取的数据库数据乱码问题
- TypeScript Handbook 2——接口1(翻译)
- tomcat WEB-INF中的结构
- .net RPC框架选型
- 玩转无线电 -- GPS Hacking (上)
- SQL SERVER树型数据处理时,函数递归调用问题,查询根节点,子节点函数
- ERROR (ClientException) nova image-list
- 使用git的正确姿势
- GCC依赖库顺序问题
- Python安装MySQLdb并连接MySQL数据库
- ZOJ 3635 Cinema in Akiba[ 大规模阵列 ]
- 在线用户数-Constants
- java集合之ArrayList源码解读
- SpringBoot之旅第四篇-web开发
- 【不遮遮掩掩】Github上传本地代码以及常见问题解决方案
- BZOJ4652 NOI2016循环之美(莫比乌斯反演+杜教筛)
- python中的全局变量和局部变量(转)
- InfluxDB 安装以及使用
- asp.net调用js方法