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 ...
随机推荐
- Composer 很重要很重要 内核 原理
话题先攒着,过来再来写 先来一张原理图 composer的原理和其他的包管理工具都是一样的,只是实现的细节有些不同,例如yum,例如brew,例如apt-get还有packets. 使用自己的comp ...
- adbd cannot run as root in production builds的解决方法
部分手机root后,使用adb root会出现这个提示. 原因是root不彻底. adb shell之后进入到$界面,su一下才进入到#. 这个之后可以使用root功能了. 注意到,这个时候exit的 ...
- devenv.exe 编译Solution
Build https://docs.microsoft.com/en-us/visualstudio/ide/reference/build-devenv-exe Builds a solution ...
- [Plugin] JQuery.uploadify上传文件插件的使用详解For ASP.NET
URL:http://www.cnblogs.com/xiaopin/archive/2010/01/21/1653523.html 今天下午整理文件上传的例子,感觉收集到的例子都很不人性话,后来找到 ...
- Hadoop MapReduce编程 API入门系列之最短路径(十五)
不多说,直接上代码. ======================================= Iteration: 1= Input path: out/shortestpath/input. ...
- CSS浮动相关解决办法
浮动元素引起的问题: 1. 父元素的一级子元素全都为浮动元素的情况下,父元素的高度无法被撑开,影响与父元素同级的元素 2. 与浮动元素同级的非浮动元素(内容)会跟随其后 3. 若非第一个元素浮动,则该 ...
- Android拼图-变形金刚
开篇 学了几个月的Android开发,动手做了一个简单的拼图小游戏,没有使用游戏框架,名字也纯属娱乐,比较粗糙请大家一笑别骂. 游戏界面是一张图片切割的6*6的小图片,并将其中一块拿走,玩家通过不同的 ...
- React 16 服务端渲染的新特性
React 16 服务端渲染的新特性 React 16 中关于服务端渲染的新特性 快速介绍React 16 服务端渲染的新特性,包括数组.性能.流等 React 16 终于来了!
- Android PopupWindow使用方法小结
前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多 ...
- ubuntu 安装 OpenCV-CUDA
参考链接:http://www.cnblogs.com/platero/p/3993877.html 官方指导:https://help.ubuntu.com/community/OpenCV 0.这 ...