Little Elephant and Array

莫队模板题
题意:区间查询,每次查询指定区间内出现次数与其值相等的数的个数。

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#include<math.h>
#define debug cout
const int maxn=1e5+100;
int in[maxn],app[maxn],bel[maxn],ans[maxn];
struct node
{
	int l,r,id;
	friend bool operator<(const node &a,const node &b) 
	{
		return bel[a.l]!=bel[b.l]?bel[a.l]<bel[b.l]:a.r<b.r;
	}
}s[maxn];
int n,m,ll,rr,now;
inline void update(int pos,int x)
{
	if(pos>n)return;
	if(app[pos]==pos&&app[pos]+x!=pos)--now;
	if(app[pos]!=pos&&app[pos]+x==pos)++now;
	app[pos]+=x;
}
inline void move(int tarl,int tarr,int id)
{
	while(ll<tarl)update(in[ll++],-1);
	while(tarl<ll)update(in[--ll],1);
	while(tarr<rr)update(in[rr--],-1);
	while(tarr>rr)update(in[++rr],1);
	ans[id]=now;
}
inline void gen()
{
	int blk=sqrt(n),cnt=0;
	for(int l=1,r;l<=n;l=r+1)
	{
		r=min(l+blk-1,n),++cnt;
		for(int i=1;i<=r;i++)
		bel[i]=cnt;
	}
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	scanf("%d",in+i);
	for(int i=1;i<=m;i++)
	scanf("%d%d",&s[i].l,&s[i].r),s[i].id=i;
	gen();
	sort(s+1,s+1+m);
	app[0]=2;
	for(int i=1;i<=m;i++)
	move(s[i].l,s[i].r,s[i].id);
	for(int i=1;i<=m;i++)
	printf("%d\n",ans[i]);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值