题目大意
区间众数
题解
莫队
代码
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define rep(i,x,y) for(register int i=(x);i<=(y);++i)
#define dwn(i,x,y) for(register int i=(x);i>=(y);--i)
#define maxn 200010
#define blo 330
using namespace std;
int read()
{
int x=0,f=1;char ch=getchar();
while(!isdigit(ch)&&ch!='-')ch=getchar();
if(ch=='-')f=-1,ch=getchar();
while(isdigit(ch))x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
return x*f;
}
void write(int x)
{
if(x==0){putchar('0'),putchar('
');return;}
int f=0;char ch[20];
if(x<0)putchar('-'),x=-x;
while(x)ch[++f]=x%10+'0',x/=10;
while(f)putchar(ch[f--]);
putchar('
');
return;
}
int n,m,a[maxn],num[maxn],num2[maxn],nowans,nowl,nowr;
struct node {int l,r,id,ans;}q[maxn];
struct node2 {int a,id;}b[maxn];
bool cmp1(node x,node y){return x.r<y.r;}
bool cmp2(node x,node y){return x.l<y.l;}
bool cmp3(node x,node y){return x.id<y.id;}
bool cmp(node2 x,node2 y){return x.a<y.a;}
bool cmp4(node2 x,node2 y){return x.id<y.id;}
void push(int x){num2[num[x]]--,num[x]++,num2[num[x]]++;while(num2[nowans+1]>0)nowans++;}
void del(int x){num2[num[x]]--,num[x]--,num2[num[x]]++;while(!num2[nowans])nowans--;}
int main()
{
n=read(),m=read();
rep(i,1,n)b[i].a=read(),b[i].id=i;
sort(b+1,b+n+1,cmp);a[b[1].id]=1;
rep(i,2,n)
{
if(b[i].a!=b[i-1].a)a[b[i].id]=a[b[i-1].id]+1;
else a[b[i].id]=a[b[i-1].id];
}
rep(i,1,m)q[i].l=read(),q[i].r=read(),q[i].id=i;
int lim=ceil(1.0*m/(1.0*blo));
sort(q+1,q+m+1,cmp2);
rep(i,0,lim-1){int l=i*blo+1,r=min((i+1)*blo,m);sort(q+l,q+r+1,cmp1);}
num[a[1]]=1,num2[1]++,nowl=nowr=1,nowans=1;
rep(i,1,m)
{
while(nowl>q[i].l)nowl--,push(a[nowl]);
while(nowr<q[i].r)nowr++,push(a[nowr]);
while(nowl<q[i].l)del(a[nowl]),nowl++;
while(nowr>q[i].r)del(a[nowr]),nowr--;
q[i].ans=-nowans;
}
sort(q+1,q+m+1,cmp3);
rep(i,1,m)write(q[i].ans);
return 0;
}
难度在于读题