首页 技术 正文
技术 2022年11月10日
0 收藏 487 点赞 3,762 浏览 2540 个字

The Little Elephant loves playing with arrays. He has array a, consisting of npositive 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, rjthe Little Elephant has to count, how many numbers x exist, such that number xoccurs 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 a1a2, …, 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题意:
问区间内含有出现次数等于其值的数字个数
思路:
莫队。
没有什么值得解释的,只不过预处理离散化将时间由3790 ms优化到186 ms,必须写一个博客纪念一下。
 #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime>
#define fuck(x) cout<<#x<<" = "<<x<<endl;
#define debug(a,i) cout<<#a<<"["<<i<<"] = "<<a[i]<<endl;
#define ls (t<<1)
#define rs ((t<<1)+1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 2.1e9;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-);
int num[maxn];
struct node{
int l,r;
int id;
}a[maxm];
int anss[maxm];
int block; bool cmp(node a,node b){
return (a.l/block!=b.l/block)?a.l<b.l:a.r<b.r;
}
int vis[];
int rem[maxn],cnt;
int pos[maxn];
int get_id(int x){
return lower_bound(rem+,rem++cnt,x)-rem;
} int main()
{
int n;
scanf("%d",&n);
int m;
scanf("%d",&m);
for(int i=;i<=n;i++){
scanf("%d",&num[i]);
rem[i]=num[i];
}
block=sqrt(n);
for(int i=;i<=m;i++){
scanf("%d%d",&a[i].l,&a[i].r);
a[i].id=i;
} sort(a+,a++m,cmp);
sort(rem+,rem++n); cnt=unique(rem+,rem++n)-rem-;
for(int i=;i<=n;i++){
pos[i]=get_id(num[i]);
}
int L=,R=;
int ans=;
num[]=-;
for(int i=;i<=m;i++){
while(R<a[i].r){
R++;
if(vis[pos[R]]==num[R]){ans--;}
vis[pos[R]]++;
if(vis[pos[R]]==num[R]){ans++;}
}
while(L<a[i].l){
if(vis[pos[L]]==num[L]){ans--;}
vis[pos[L]]--;
if(vis[pos[L]]==num[L]){ans++;}
L++; }
while(R>a[i].r){
if(vis[pos[R]]==num[R]){ans--;}
vis[pos[R]]--;
if(vis[pos[R]]==num[R]){ans++;}
R--;
}
while(L>a[i].l){
L--;
if(vis[pos[L]]==num[L]){ans--;}
vis[pos[L]]++;
if(vis[pos[L]]==num[L]){ans++;}
}
anss[a[i].id]=ans;
}
for(int i=;i<=m;i++){
printf("%d\n",anss[i]);
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,087
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,562
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,412
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,185
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,821
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905