Codeforces Round #136 (Div. 1) B. 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 numbers alj, 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内,一个数的出现次数等于他本身的数的个数。莫队板子。
错因:函数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的更多相关文章
- 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 ...
- Codeforces Round #136 (Div. 2)
A. Little Elephant and Function 逆推. B. Little Elephant and Numbers \(O(\sqrt n)\)枚举约数. C. Little Ele ...
- 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 ...
- 字符串(后缀自动机):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 ...
- Codeforces Round #157 (Div. 2) D. Little Elephant and Elections(数位DP+枚举)
数位DP部分,不是很难.DP[i][j]前i位j个幸运数的个数.枚举写的有点搓... #include <cstdio> #include <cstring> using na ...
- Codeforces Round #129 (Div. 1)E. Little Elephant and Strings
题意:有n个串,询问每个串有多少子串在n个串中出现了至少k次. 题解:sam,每个节点开一个set维护该节点的字符串有哪几个串,启发式合并set,然后在sam上走一遍该串,对于每个可行的串,所有的fa ...
- 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 ...
- Codeforces Round #533(Div. 2) C.Ayoub and Lost Array
链接:https://codeforces.com/contest/1105/problem/C 题意: 给n,l,r. 一个n长的数组每个位置可以填区间l-r的值. 有多少种填法,使得数组每个位置相 ...
- Codeforces Round #533 (Div. 2) C. Ayoub and Lost Array(递推)
题意: 长为 n,由 l ~ r 中的数组成,其和模 3 为 0 的数组数目. 思路: dp[ i ][ j ] 为长为 i,模 3 为 j 的数组数目. #include <bits/stdc ...
随机推荐
- MySQL数据库——存储和函数
一.存储过程1 基本语法CREATE PROCEDURE sp_name ([proc_parameter[,...]]) [characteristic ...] routine_body 格式[I ...
- ScrollViewer滚动究竟来触发载入数据的Behavior
近期项目中遇到载入数据的性能问题, 原因是.net4.0的虚拟化支持不够完毕,有好多bug在4.5才修复. 我们仅仅能利用大家通用的做法来延迟载入数据: 每次载入固定少量的数据.当拖动究竟后.继续载入 ...
- luogu2441 角色属性树
题目大意:维护一个可查询.修改的树,查询的是一个节点的:离它距离最近的.组成两个节点Key值的质因数存在交集的.祖先节点:修改是修改一个节点的key值. 如果组成两个Key值的质因数存在交集,则两个数 ...
- POJ1837 Balance 背包
题目大意: 有一个天平,天平左右两边各有若干个钩子,总共有C个钩子(每个钩子有相对于中心的距离,左负右正),有G个钩码,求将钩码全部挂到钩子上使天平平衡的方法的总数. 将每个砝码看作一组,组内各个物品 ...
- <% %> in html
$(document).on('click', '.invoiceNumber', function () { var string = <%= StaticHelper.GetCurrentC ...
- [JavaEE] Apache Maven 入门篇(下)
http://www.oracle.com/technetwork/cn/community/java/apache-maven-getting-started-2-405568-zhs.html 作 ...
- kindoreditor上传图片
<!doctype html><html> <head> <meta charset="utf-8" /> <title> ...
- 3、Collection接口中的功能概述
package cn.itcast_01; import java.util.ArrayList; import java.util.Collection; /** * 集合: * 由于我们使用的是面 ...
- win7利用tsmmc.msc远程管理工具
win7和xp默认不带tsmmc.msc远程管理工具.我们只需要从2003的windows\system32目录下复制 mstsmhst.dll ,mstsmmc.dll 和tsmmc.msc文件放到 ...
- Struts2学习笔记 - Part.01
1.关于Struts2中的struts.xml文件中action设置 <!-- 它是一个通用action,此处的*表明它可以处理任意的请求--> <action name=" ...