描述
Mr. Wysuperfly uses a messenger tool called NKQQ to communicate with his friends on the net. The NKQQ accumulates 1 point score every hour automatically. When the score reach certain level, there will be a level-up. The relationship between the score and the level is listed as below:
Level 1 0 - 100
Level 2 101 - 500
Level 3 501 - 2000
Level 4 2001 - 10000
Level 5 10001 - 50000
Level 6 50001 - 200000
Level 7 200001 - infinity
Mr. Wysuperfly finds that other ACM teammates also use NKQQ everyday like him. He wants to know that after several hours, how many teammates reach a certain level. Can you help him?
输入
The first line is an integer N (N<=10^5) which represents the amount of the teammates to be considered. The second line includes N numbers which represent the scores of the teammates. The third line is a positive integer Q (Q<=10^5) which represents how many questions in the input data. In the following Q lines, each line which includes two integers D (0<=D<=10^9) and L (1<=L<=7) stands for a question: ‘After D hours, how many teammates will reach level L?
输出
For every question, output one line to show the answer to the question (one nonnegative integer).
样例输入
3
1 2 3
2
98 1
98 2
样例输出
2
1
题目大意:
给出人数N,每个人都有起始经验值,接着在抛出Q个问题,每个问题问过D小时后到达L级的人有多少
思路:
一开始想太多了用了线状数组去做,想着这样会快点,但却一直TLE,后来转念一想线状数组优点是更新快,如果数组内值不变,那其效率还没直接算高,失策失策啊!!!归根结底还是对树状数组不够了解。
因为1~6级经验值最大为2e5,而7级经验值为大于2e5,所以可以把所有超过2e5的值转化成2e5+1;
故建立一个大小为2e5+2的数组re,先统计出每个数有几个;然后计算出0~i(i<2e5+2)的区间和;
L级的上下限分别减去D后进行分类讨论(7级的上限始终是2e5+1)
若上限小于0则输出0;
若下限小于等于0则输出re[上限];
否则输出re[上限]-re[下限-1]
代码:
#include<bits/stdc++.h>
using namespace std;
int dj[7]={0,101,501,2001,10001,50001,200001},re[200002];
int main()
{
int n,q,d,l,x;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&x);
if(x>200001)x=200001;
re[x]++;
}
re[0];
for(int i=1;i<=200001;i++)re[i]=re[i-1]+re[i];
cin>>q;
for(int i=0;i<q;i++)
{
scanf("%d%d",&d,&l);
if(l==7)
{
int head=200001-d,tail=200001;
if(head<=0)printf("%d\n",re[tail]);
else printf("%d\n",re[tail]-re[head-1]);
}
else
{
int head=dj[l-1]-d,tail=dj[l]-1-d;
if(tail<0)printf("0\n");
else if(head<=0)printf("%d\n",re[tail]);
else printf("%d\n",re[tail]-re[head-1]);
}
}
return 0;
}