abc440_D题解
原题链接
原题。
大概题意
已知 个数,有 个询问,每个询问给出 和 求出 大于等于 且不存在 数组中的第 个元素。
Solution
First
这道题 不需要顺序。
考虑 不变,则随着 的增加答案肯定也会增加 (即 和 同增减性)。
故此题可以二分答案。
Second
我们二分 的值(不开____ ____,见……)。
check函数我们需要返回当前 及其以前有多少个不在数组中的数,然后就常规二分即可。
Third
代码如下:
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
const int N = 310000;
int n, q;
ll a[N];
inline ll count(ll x, ll pos) {
if (pos < x)
return 0;
ll tot = pos - x + 1;
int idx1 = lower_bound(a + 1, a + n + 1, x) - a;
int idx2 = upper_bound(a + 1, a + n + 1, pos) - a;
return tot - idx2 + idx1;
}
int main() {
scanf("%d%d", &n, &q);
for (int i = 1; i <= n; i++)
scanf("%lld", &a[i]);
sort(a + 1, a + n + 1);
for (; q--; ) {
ll x, y;
scanf("%lld%lld", &x, &y);
ll l = x, r = 3e18;
while (l + 1 < r) {
ll m = (l + r) / 2;
ll mc = count(x, m);
if (mc >= y)
r = m;
else
l = m;
}
if (count(x, l) >= y)
printf("%lld\n", l);
else
printf("%lld\n", r);
}
return 0;
}END
求赞+通过 qwq。