1371. 期末打分
Description
马上要到期末,助教们准备开始打分,然而毕设DDL要到了,助教只好请丁姐来帮忙。众所周知丁姐大一就把毕设做完了,这时候肯定空的跟狗一样。
翁阿姨让丁姐统计分数落在区间[a,b]内的学生人数,由于学生很多,丁姐很懒,他决定把这个任务交给你。
Input Format
第一行两个整数n, m 表示有n个学生,m次询问
第二行n个正整数表示每个学生的分数
之后m行每行两个整数a, b
Output Format
对每个询问,输出学生人数
Sample Input
10 5
8 1 2 4 10 6 6 6 8 5
1 1
4 5
1 10
7 10
3 10
Sample Output
1 2 10 3 8
因为分数一定是有范围的。。虽然我也不知道范围是多少。
对于有一定范围的数据,可以考虑用桶的思想来存储和处理(排序之类的)
然后求和即可
因为是求连续和 所以考虑 利用前缀和优化即可。
#include <iostream> #include <cstdio> using namespace std; int marks[100000]={0}; int presum[100000]={0}; int main(int argc, char const *argv[]) { int n , m; cin>>n>>m; for (int i = 0; i < n; ++i) { int mark=0; scanf("%d",&mark); ++marks[mark]; } presum[0] = 0; for (int i = 1; i <= n; ++i) presum[i] = presum[i-1] + marks[i]; int a,b; for (int i = 0; i < m; ++i) { scanf("%d %d",&a,&b); printf("%d ",presum[b] - presum[a-1]); } return 0; }