time limit per test

4 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

思路:求区间l,r内,一个数的出现次数等于他本身的数的个数。莫队板子。

错因:函数hash冲突。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 100001
using namespace std;
int n,m,S,ans;
int sum[MAXN],num[MAXN],has[MAXN];
struct nond{
int l,r,id,pos,ans;
}edge[MAXN];
int cmp1(nond a,nond b){
return a.id<b.id;
}
int cmp(nond a,nond b){
if(a.pos==b.pos) return a.r<b.r;
else return a.pos<b.pos;
}
void up(int x,int k){
if(sum[num[x]]!=has[num[x]]&&sum[num[x]]+k==has[num[x]]) ans++;
else if(sum[num[x]]==has[num[x]]&&sum[num[x]]+k!=has[num[x]]) ans--;
sum[num[x]]+=k;
}
void mode(){
int l=,r=;
for(int i=;i<=m;i++){
while(l<edge[i].l) up(l++,-);
while(l>edge[i].l) up(--l,);
while(r<edge[i].r) up(++r,);
while(r>edge[i].r) up(r--,-);
edge[i].ans=ans;
}
}
int main(){
scanf("%d%d",&n,&m);
S=sqrt(n);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
has[i]=num[i];
}
sort(has+,has++n);
int cnt=unique(has+,has++n)-(has+);
for(int i=;i<=n;i++)
num[i]=lower_bound(has+,has+cnt+,num[i])-has;
for(int i=;i<=m;i++){
scanf("%d%d",&edge[i].l,&edge[i].r);
edge[i].id=i;
edge[i].pos=(edge[i].l-)/S+;
}
sort(edge+,edge++m,cmp);
mode();
sort(edge+,edge++m,cmp1);
for(int i=;i<=m;i++)
cout<<edge[i].ans<<endl;
}

Codeforces Round #136 (Div. 1) B. Little Elephant and Array的更多相关文章

  1. Codeforces Round #136 (Div. 1)C. Little Elephant and Shifts multiset

    C. Little Elephant and Shifts Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/pro ...

  2. Codeforces Round #136 (Div. 2)

    A. Little Elephant and Function 逆推. B. Little Elephant and Numbers \(O(\sqrt n)\)枚举约数. C. Little Ele ...

  3. Codeforces Round #157 (Div. 1) B. Little Elephant and Elections 数位dp+搜索

    题目链接: http://codeforces.com/problemset/problem/258/B B. Little Elephant and Elections time limit per ...

  4. 字符串(后缀自动机):Codeforces Round #129 (Div. 1) E.Little Elephant and Strings

    E. Little Elephant and Strings time limit per test 3 seconds memory limit per test 256 megabytes inp ...

  5. Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)

    数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...

  6. Codeforces Round #129 (Div. 1)E. Little Elephant and Strings

    题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...

  7. Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array 【dp】

    传送门:http://codeforces.com/contest/1105/problem/C C. Ayoub and Lost Array time limit per test 1 secon ...

  8. Codeforces Round #533(Div. 2) C.Ayoub and Lost Array

    链接:https://codeforces.com/contest/1105/problem/C 题意: 给n,l,r. 一个n长的数组每个位置可以填区间l-r的值. 有多少种填法,使得数组每个位置相 ...

  9. Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array(递推)

    题意: 长为 n,由 l ~ r 中的数组成,其和模 3 为 0 的数组数目. 思路: dp[ i ][ j ] 为长为 i,模 3 为 j 的数组数目. #include <bits/stdc ...

随机推荐

  1. POJ3185 The Water Bowls 反转(开关)

    Description The cows have a line of 20 water bowls from which they drink. The bowls can be either ri ...

  2. luogu3799 妖梦拼木棒

    题目大意 有n根木棒,现在从中选4根,想要组成一个正三角形,问有几种选法?木棒长度都<=5000. 题解 根据容斥原理,三角形两条边分别由长度相等的单根木棒组成,另一条边由两条小于该边长的木棒构 ...

  3. c18---数组和指针

    // // main.c // day09 #include <stdio.h> int main(int argc, const char * argv[]) { ; int *numP ...

  4. android.mk中LOCAL_MODULE_TAGS说明【转】

    转自http://blog.csdn.net/evilcode/article/details/6459299 LOCAL_MODULE_TAGS :=user eng tests optional ...

  5. [POJ 1934] Trip

    [题目链接] http://poj.org/problem?id=1934 [算法] 先用dp求出LCS,然后搜索即可,注意加上一些剪枝 [代码] #include <algorithm> ...

  6. SpringBoot之表单验证@Valid

    转自:https://www.cnblogs.com/chenlove/p/8708627.html SpringBoot提供了强大的表单验证功能实现,给我们省去了写验证的麻烦: 这里我们给下实例,提 ...

  7. jupyter在特定环境特定目录中启动

    代码如下: @echo off start %windir%\System32\cmd.exe "/c" D:\Anaconda\Scripts\activate.bat # 启动 ...

  8. 2015 多校赛 第二场 1004 hdu(5303)

    Problem Description There are n apple trees planted along a cyclic road, which is L metres long. You ...

  9. jquery选择器(可见对象,不可见对象) +判断,对象(逆序)

    //可见对象: $("li:visible ") //可见对象下的 隐藏对象 $("li:visible [type='hidden']") //获得 可见 的 ...

  10. ArcGIS Javascript API 加载高德在线地图扩展

    利用ArcGIS JavaScript API加载高德在线地图的扩展 /** * Created by WanderGIS on 2015/7/15. */ define(["dojo/_b ...