今天怎么不开心
题意:
给一个长为n的整数不下降序列,给q个询问,每次询问一个区间l,r中出现次数最多的整数的出现次数是多少。
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.
题意简单粗暴,一看就是维护区间信息的数据结构,线段树每个点维护三个值即可,看评论里大多数说是RMQ,应该是指稀疏表的做法吧。
线段树每个点维护最值、左边连续区间、右边连续区间三个信息,最值由两个子节点的最值、左子节点右端+右子节点左端求得。
代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
#define mxn 100010
int n,q;
int a[mxn];
struct item{
int val;
int cnt;
item(){}
item(int a,int b){
val=a;
cnt=b;
}
};
struct node{
int ll,rr;
item maxx;
item lft,rgt;
node(){}
node(item a,item b,item c){
lft=a;
maxx=b;
rgt=c;
}
};
class segment_tree{
private:
node nd[mxn<<2];
void merge(int id){
int m=(nd[id].ll+nd[id].rr)>>1,ls=id<<1,rs=ls|1;
nd[id].maxx=nd[ls].maxx.cnt>nd[rs].maxx.cnt ? nd[ls].maxx : nd[rs].maxx;
if(nd[ls].rgt.val==nd[rs].lft.val&&nd[id].maxx.cnt<nd[ls].rgt.cnt+nd[rs].lft.cnt){
nd[id].maxx.val=nd[ls].rgt.val;
nd[id].maxx.cnt=nd[ls].rgt.cnt+nd[rs].lft.cnt;
}
nd[id].lft=nd[ls].lft;
if(nd[ls].lft.val==nd[rs].lft.val) nd[id].lft.cnt+=nd[rs].lft.cnt;
nd[id].rgt=nd[rs].rgt;
if(nd[rs].rgt.val==nd[ls].rgt.val) nd[id].rgt.cnt+=nd[ls].rgt.cnt;
}
public:
void build(int l,int r,int id){
nd[id].ll=l;
nd[id].rr=r;
if(l==r){
nd[id].maxx=nd[id].lft=nd[id].rgt=item(a[l],1);
return;
}
int m=(l+r)>>1,ls=id<<1,rs=ls|1;
build(l,m,ls);
build(m+1,r,rs);
merge(id);
}
node find(int l,int r,int id){
if(nd[id].ll==l&&nd[id].rr==r)
return nd[id];
int m=(nd[id].ll+nd[id].rr)>>1,ls=id<<1,rs=ls|1;
node a,b;
if(r<=m) return find(l,r,ls);
else if(l>m) return find(l,r,rs);
else{
a=find(l,m,ls);
b=find(m+1,r,rs);
if(a.rgt.val==b.lft.val&&a.maxx.cnt<a.rgt.cnt+b.lft.cnt){
a.maxx.cnt=a.rgt.cnt+b.lft.cnt;
a.maxx.val=a.rgt.val;
}
if(a.maxx.cnt<b.maxx.cnt)
a.maxx=b.maxx;
if(a.rgt.val==b.rgt.val)
a.rgt.cnt=a.rgt.cnt+b.rgt.cnt;
else a.rgt=b.rgt;
if(a.lft.val==b.lft.val)
a.lft.cnt=a.lft.cnt+b.lft.cnt;
return a;
}
}
}Tree;
int main(){
while(scanf("%d",&n)!=EOF&&n){
scanf("%d",&q);
for(int i=0;i<n;++i) scanf("%d",&a[i]);
Tree.build(0,n-1,1);
int l,r;
node tem;
for(int i=0;i<q;++i){
scanf("%d%d",&l,&r);
tem=Tree.find(l-1,r-1,1);
printf("%d\n",tem.maxx.cnt);
}
}
return 0;
}