D. Little Elephant and Array
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 numbersalj, 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]内出现次数等于它本身的数的个数 莫队算法
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,siz,ans;
int a[],hash[];
int sum[];
struct node
{
int bl,id;
int l,r,k;
}e[];
bool cmp(node p,node q)
{
if(p.bl!=q.bl) return p.bl<q.bl;
return p.r<q.r;
}
bool cmp2(node p,node q)
{
return p.id<q.id;
}
void update(int pos,int w)
{
if(sum[a[pos]]!=hash[a[pos]]&&sum[a[pos]]+w==hash[a[pos]]) ans++;
else if(sum[a[pos]]==hash[a[pos]]&&sum[a[pos]]+w!=hash[a[pos]]) ans--;
sum[a[pos]]+=w;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]),hash[i]=a[i];
siz=sqrt(n);
sort(hash+,hash+n+);
int cnt=unique(hash+,hash+n+)-(hash+);
for(int i=;i<=n;i++) a[i]=lower_bound(hash+,hash+cnt+,a[i])-hash;
int ll,rr;
for(int i=;i<=m;i++)
{
scanf("%d%d",&ll,&rr);
e[i].id=i;
e[i].bl=(ll-)/siz+;
e[i].l=ll;
e[i].r=rr;
}
sort(e+,e+m+,cmp);
int l=,r=,opl,opr;
for(int i=;i<=m;i++)
{
opl=e[i].l; opr=e[i].r;
while(l>opl) update(--l,);
while(l<opl) update(l++,-);
while(r<opr) update(++r,);
while(r>opr) update(r--,-);
e[i].k=ans;
}
sort(e+,e+m+,cmp2);
for(int i=;i<=m;i++) printf("%d\n",e[i].k);
}

Codeforces 221 D. Little Elephant and Array的更多相关文章

  1. Codeforces 221d D. Little Elephant and Array

    二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...

  2. Codeforces 221 C. Little Elephant and Problem

    C. Little Elephant and Problem time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  3. Codeforces 221 E. Little Elephant and Shifts

    E. Little Elephant and Shifts time limit per test 2 seconds memory limit per test 256 megabytes inpu ...

  4. Codeforces 221 B. Little Elephant and Numbers

    B. Little Elephant and Numbers time limit per test 2 seconds memory limit per test 256 megabytes inp ...

  5. Codeforces 221 A. Little Elephant and Function

    A. Little Elephant and Function time limit per test 2 seconds memory limit per test 256 megabytes in ...

  6. CodeForces 220B(B. Little Elephant and Array)

    http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ...

  7. CodeForces 221D Little Elephant and Array

    Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...

  8. AC日记——Little Elephant and Array codeforces 221d

    221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...

  9. Codeforces Round #136 (Div. 1) B. Little Elephant and Array

    B. Little Elephant and Array time limit per test 4 seconds memory limit per test 256 megabytes input ...

随机推荐

  1. OOP 学习笔记汇总

    1.1 引用 1.2 const关键字 1.3 动态内存分配 1.4 内联函数和重载函数函数参数缺省值 1.5 类和对象的基本概念与用法1 2.1 类和对象的基本概念2

  2. UITableViewCell contentView layoutSubviews 死循环

    发现一个问题,当在UITableViewCell 的 layoutSubviews 中修改 contentView 的frame时会产生死循环.该问题只会出现在iOS8中,iOS7与iOS9均没有问题 ...

  3. app测试更多机型系统解决方法

    手头上测试机有限,不可能每个机型每个系统都 有一部手机,此时寻求一个什么都有的测试平台就显得尤为重要了. 作为小白的我刚刚使用了一波腾讯优测,简单粗暴有效给力,而且新注册认证用户还有60min免费使用 ...

  4. The goal you specified requires a project to execute but there is no POM in this directory

    [INFO] Scanning for projects... [INFO] ------------------------------------------------------------- ...

  5. webgl 初识1

    1.  webgl是什么?   WebGL其实是一个非常简单的API.好吧,“简单”可能是一个不恰当的描述. 它做的是一件简单的事,它仅仅运行用户提供的两个方法,一个顶点着色器和一个片断着色器, 去绘 ...

  6. 在Eclipse中开发WEB项目

    本文的演示是从本地文件创建dynamic web project,从svn检出的同时创建dynamic web project于此类似.我们推荐使用解压版的tomcat6.x版本,来作为服务器.可以到 ...

  7. IE 之 userData 模拟 localStorage

    引 chrome,  safari, firefox, ie 9都支持 localStorage.  但可恶的是,中国 ie 6 占有最大的比例. 使用 cookie 不但容量有限,而且给我们增加了不 ...

  8. 【EF】Entity Framework实现属性映射约定

    Entity Framework Code First属性映射约定中“约定”一词,在原文版中为“Convention”,翻译成约定或许有些不好理解,这也是网上比较大多数的翻译,我们就当这是Entity ...

  9. hive表信息查询:查看表结构、表操作等--转

    原文地址:http://www.aboutyun.com/forum.PHP?mod=viewthread&tid=8590&highlight=Hive 问题导读:1.如何查看hiv ...

  10. 25个Java机器学习工具&库--转载

    本列表总结了25个Java机器学习工具&库: 1. Weka集成了数据挖掘工作的机器学习算法.这些算法可以直接应用于一个数据集上或者你可以自己编写代码来调用.Weka包括一系列的工具,如数据预 ...