CodeForces - 221D:Little Elephant and Array(莫队算法)

Description
The Little Elephant loves playing with arrays. He has array a, consisting of n positive integers, indexed from 1 to n. Let’s denote the number with index i as ai.

Additionally the Little Elephant has m queries to the array, each query is characterised by a pair of integers lj and rj (1 ≤ lj ≤ rj ≤ n). For each query lj, rj the Little Elephant has to count, how many numbers x exist, such that number x occurs exactly x times among numbers alj, alj + 1, …, arj.

Help the Little Elephant to count the answers to all queries.

Input
The first line contains two space-separated integers n and m (1 ≤ n, m ≤ 105) — the size of array a and the number of queries to it. The next line contains n space-separated positive integers a1, a2, …, an (1 ≤ ai ≤ 109). Next m lines contain descriptions of queries, one per line. The j-th of these lines contains the description of the j-th query as two space-separated integers lj and rj (1 ≤ lj ≤ rj ≤ n).

Output
In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.

Examples
Input

7 2
3 1 2 2 3 3 7
1 7
3 4

Output

3
1

题目大意
给出一行数,要求执行m次这样的操作,每次从中取一个区间的数,求出在此区间内数x的出现次数等于x的数的个数。

思路
开始我不知道莫队算法,不会优化,老超时。后来在csdn上一搜发现大家都是用莫队算法做的,就去搜了一下莫队。
莫对算法就是用来计算在一个区间内某个数出现次数的算法。
他很好的将分块和暴力结合到了一起。

AC代码
法一(莫队算法):(这是来自一个老学长的代码,因为我还不是很了解莫队算法,就先研究学长的代码了,说实话现在也不是很懂

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define rep(i,a,b) for(register int i=(a);i<=(b);i++)
#define dep(i,a,b) for(register int i=(a);i>=(b);i--)
using namespace std;
const int maxn=3e5+5;
const ll mo=998244353;
ll gcd(ll x,ll y){return y==0?x:gcd(y,x%y);}
ll power(ll a,ll n){ll sum=1; while(n){if(n&1) sum=sum*a%mo; n>>=1; a=a*a%mo;} return sum;}
int n,m,k;
int a[maxn],c[maxn],num[maxn],tmp[maxn];
int id[maxn];
int sz,cnt,sum;
void add(int i,int v)
{
	if(a[i]<=100000){
		if(v<0)
		{
			if(num[a[i]]==a[i]) sum--;
			num[a[i]]--;
			if(num[a[i]]==a[i]) sum++;
		}
		else 
		{
			if(num[a[i]]==a[i]) sum--;
			num[a[i]]++;
			if(num[a[i]]==a[i]) sum++;
		}
	}
}
struct ac
{
    ll x,y;
}ans[maxn];
struct node
{
    int l,r,id;
}q[maxn];
bool cmp(node a,node aa)
{
    return id[a.l]==id[aa.l]?a.r<aa.r:a.l<aa.l;
}
int main()
{
    int T,cas=1;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        sz=sqrt(n+0.5);
        memset(tmp,0,sizeof(tmp));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            id[i]=(i-1)/sz+1;
        }
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&q[i].l,&q[i].r);
            q[i].l--;
            q[i].id=i;
        }
        sort(q,q+m,cmp);
        int l=0,r=0;
		sum=0;
        for(int i=0;i<m;i++)
        {
            while(l<q[i].l){add(l+1,-1);l++;}
            while(l>q[i].l){add(l,1);l--;}
            while(r<q[i].r){add(r+1,1);r++;}
            while(r>q[i].r){add(r,-1);r--;}
    
            ans[q[i].id].x=sum;
        }
        for(int i=0;i<m;i++)
        {
            printf("%lld\n",ans[i].x);
        }
    }
    return 0;
}

法二(暴力剪枝):(这是我在codeforces看到一个大神的代码,写的超级巧妙,此代码不是我等小菜鸡能写出来的,放到博客里供我瞻仰)

#include <bits/stdc++.h>
#define mn 1000010
using namespace std;
int n,m,l[mn],r[mn],ans[mn],a[mn],s[mn],b[mn];
int main()
{
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        cin>>a[i],b[a[i]<=n?a[i]:0]++;//剪枝一:记录小于n的数的个数,因为大于n的数一定不可能出现和它本身数量相同的次数,直接舍去
    for(int i=1; i<=m; i++)
        cin>>l[i]>>r[i];
    for(int i=1; i<=n; i++)
        if(i<=b[i])//剪枝二:i的数量要大于等于它本身才可能成立,如果小于它本身则一定不成立,舍去不成立的条件
        {
            for(int j=1; j<=n; j++)
                s[j]=s[j-1]+(a[j]==i);//求前j个数里面等于i的数的个数
            for(int j=1; j<=m; j++)
                if(s[r[j]]-s[l[j]-1]==i)
                    ans[j]++;//成立则加一
        }
    for(int i=1; i<=m; i++)
       cout<<ans[i]<<endl;
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值