Codeforces 221 D. Little Elephant and Array
4 seconds
256 megabytes
standard input
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.
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).
In m lines print m integers — the answers to the queries. The j-th line should contain the answer to the j-th query.
7 2
3 1 2 2 3 3 7
1 7
3 4
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的更多相关文章
- Codeforces 221d D. Little Elephant and Array
二次联通门 : Codeforces 221d D. Little Elephant and Array /* Codeforces 221d D. Little Elephant and Array ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- CodeForces 220B(B. Little Elephant and Array)
http://codeforces.com/contest/220/problem/B 题意:给出一个数组,给出m组询问,问区间中出现a[i] 次的有多少个. sl: 很显然的离线问题了. 大视野菜花 ...
- CodeForces 221D Little Elephant and Array
Little Elephant and Array Time Limit: 4000ms Memory Limit: 262144KB This problem will be judged on C ...
- AC日记——Little Elephant and Array codeforces 221d
221D - Little Elephant and Array 思路: 莫队: 代码: #include <cmath> #include <cstdio> #include ...
- 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 ...
随机推荐
- Scrum立会报告+燃尽图(十月二十日总第十一次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2246 项目地址:https://git.coding.net/zhang ...
- 20181023-9 Alpha阶段第2周/共2周 Scrum立会报告+燃尽图 06
作业要求参见: https://edu.cnblogs.com/campus/nenu/2018fall/homework/2289 Scrum master:赵佳璐 一.小组介绍 组长:王一可 组员 ...
- c# 修改exe.config文件并且及时更新
1.config文件地址:AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 注意:如果是在调试程序中运行,此地址指代的是vhost. ...
- Right-BICEP测试四则运算2
根据Right-BICEP单元测试的方法,我对我写的四则运算2的程序进行了测试: 1.测试能否控制使用乘除 有乘除 无乘除 2.测试是否能加括号 不加括号 加括号 3.能否控制结果没有负数 无负数 4 ...
- 每日Scrum--No.3
Yesterday:帮着队友一起打开地图 Today:学习迪杰斯特拉算法,试着编写程序代码 Problem:语法逻辑出错,在执行的时候,有的时候出现死循环,有的时候屏幕出现null和乱码.语句的编写有 ...
- weblogic下JNDI及JDBC连接测试(weblogic环境)
JNDI的专业解释,大家自行去网络搜索吧,这里就不啰嗦了. 单纯从使用角度看,可以简称把它看成一个key-value的“哈希资源”容器.给定一个string类型的key,可以把任何类型的value,放 ...
- javascript 排序
// 插入排序 将第一待排序序列第一个元素看做一个有序序列,把第二个元素到最后一个元素当成是未排序序列. 从头到尾依次扫描未排序序列,将扫描到的每个元素插入有序序列的适当位置.(如果待插入的元素与有序 ...
- phaser2 微信小游戏入手
phaser2小游戏基本没什么什么问题,可以下常开发游戏.如果遇到什么问题, 可以提出来共同讨论. 下面来个例子 import './lib/weapp-adapter'; import Phaser ...
- 第198天:js---内置对象的原型链和其他知识
一.内置对象的原型链 1.Object原型 function test() {} alert(test.toString()); //新增属性 Object.prototype.mytest = fu ...
- kettle、Oozie、camus、gobblin
kettle简介 http://www.cnblogs.com/limengqiang/archive/2013/01/16/KettleApply1.html Oozie介绍 http://blog ...