Now, n people are checking the problems. There are 101010 problems in total and numbered from 1 to 101010. The i-th person starts the check from the ai problem. It takes one minute to check a problem. More formally, the problem with the number ai+j−1 will be checked by the i-th person at the j-th minute.
Now, your task is to answer q queries. Each query will give you an integer t, please calculate the number of problems that have been tested at least once after t minutes.
Input
The first line contains a single integer n(1≤n≤5⋅105) indicating the number of people.
The second line contains n integers a1,a2,⋯,an (1≤a1≤a2≤⋯≤an≤1018) indicating the problem number of the first check for the i-th person.
The next line contains a single integer q (1≤q≤5⋅105) indicating the number of queries.
Each of the next q lines contains an integer t (0≤t≤1018).
it is guaranteed that ai+1−ai≤ai+2−ai+1 for every 1≤i≤n−2.
Output
For each query, output the total number of problems that have been tested at least once after t minutes in one line.
这道题目里保证了两个数之间的差值是越来越大的,那这样子题目就会简单很多,我们就可以很自然的想到二分查找。
upper_bound(a+m,a+n,b):二分查找a当中从m到n中大于b的第一项
lower_bound(a+m,a+n,b):二分查找a当中从m到n中大于等于b的第一项
upper_bound和lower_bound在使用前务必!务必要进行排序!
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
#define int long long
const int maxn = 500100;
int n;
int a[maxn];
int sum[maxn];
int q;
signed main(){
scanf("%lld", &n);
for(int i = 1; i <= n; ++i) scanf("%lld", &a[i]);
sum[1] = 0;
for(int i = 2; i <= n; ++i) sum[i] = a[i] - a[i - 1];
scanf("%lld", &q);
int x;
int ans = 0;
for(int i = 1; i <= q; ++i){
scanf("%lld", &x);
int now = upper_bound(sum + 1, sum + n + 1, x) - sum - 1;
ans = a[now] - a[1] + x;//前面的
ans += (n - now) * x;//后面并没有填满的
printf("%lld\n",ans);
}
}