codeforces 221D Little Elephant and Array
来源:互联网 发布:你知我所想我知你所要 编辑:程序博客网 时间:2024/06/05 02:15
题目的意思是说,给定一个区间,其长度为m,然后进行n次查询,每次查询给定区间[l,r],查出区间中出现次数等于其值大小的数的个数。开始做的时候,不是超空间就是超时间,然后借鉴了一下别人的思路。
首先使用一个a数组记录m个元素,期间用b数组记录每个元素出现的次数,然后在用l数组和r数组记录给定的查询区间,因为已经记录每个元素的出现次数,所以那些出现次数小于其值的数根本就不用考虑了,然后就是从1扫到m,扫的时候用s数组记录该值在每个位置及之前出现的次数,然后在逐一对每个区间进行判断就行了。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;int a[100010],b[100010],l[100010],r[100010],s[100010],ans[100010];int main(){ int n,m; scanf("%d%d",&m,&n); s[0]=0; memset(b,0,sizeof(b)); memset(ans,0,sizeof(ans)); for(int i=1; i<=m; i++) { scanf("%d",&a[i]); if(a[i]<=m) b[a[i]]++; } for(int i=1; i<=n; i++) scanf("%d%d",&l[i],&r[i]); for(int i=1; i<=m; i++) if(i<=b[i]) { for(int j=1; j<=m; j++) s[j]=s[j-1]+(a[j]==i); for(int j=1;j<=n;j++) if(s[r[j]]-s[l[j]-1]==i) ans[j]++; } for(int i=1;i<=n;i++) printf("%d\n",ans[i]); return 0;}
0 0
- codeforces 221D Little Elephant and Array
- codeforces 221D Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array
- Codeforces 220B Little Elephant and Array
- CodeForces 220B Little Elephant and Array
- Codeforces Round #136 (Div. 2) D. Little Elephant and Array 线段树 离线处理
- codeforces 221B Little Elephant and Numbers
- codeforces 221C Little Elephant and Problem
- CF #136Div2 D. Little Elephant and Array
- CodeForces Round #136(220B) - Little Elephant and Array
- 线段树 CodeForces 220B - Little Elephant and Array
- Codeforces 220B(Little Elephant and Array)
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- Codeforces 220B - Little Elephant and Array 离线树状数组
- Little Elephant and Array - CodeForces 220 B 树状数组
- Codeforces Round #136 (Div. 2) Little Elephant and Array
- Codeforces Round #136 (Div. 1) B. Little Elephant and Array
- java 图标类Images - ImageIcon
- 关于RSA算法的例子实践(第二周作业)
- 双击.dsw无反应,图标异常,打开方式中找不到vc
- POJ 1308 与 HDU 1325
- CakePHP是什么?为什么要使用它呢?
- codeforces 221D Little Elephant and Array
- Velocity布局(layout)功能介绍
- Zdenek Kalal跟踪算法
- EXCEL与SQL数据库间导入导出之傻瓜闲扯淡
- Adlist 双向链表的实现 redis
- POJ 1141 Brackets Sequence
- 学习笔记 -- NSThread
- leetCode: Word Break I & Word Break II
- Android经典的大牛博客推荐