题解:AT_abc440_d [ABC440D] Forbidden List 2
场上 T3、T4 挂了好几发,噗嗤。 看着有题解的空位,水一波题解。
1. 题目意思
是 个互不相同的整数。
次询问,第 个问题会给你两个整数 :
- 你需要先生成大于或等于 的所有整数。
- 找出其中不在 里的第 小的数。
2. 思路
首先,我们可以猜出来是二分(很容易)。 具体思路:
- 输入 后,把 排序。
- 每次读入 ,二分每一次分到 ,我们可以用二分算出来在 到 中间有多少个 的值,记为 。如果 到 的区间长度减去 即 ,大于等于 ,就往左边找,否则往右边找。
- 找到答案,输出。
时间复杂度为 。
3. 代码
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
#define st first
#define nd second
using namespace std;
int a[300005];
int n,q;
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
while(q--){
int x,y;
cin>>x>>y;
int Cnt=lower_bound(a+1,a+n+1,x)-a-1;
int l=1,r=5000000000ll,ans=0;
while(l<=r){
int mid=(l+r)/2;
int cnt=upper_bound(a+1,a+n+1,mid)-a-1;
cnt-=Cnt;
if(mid-x+1-cnt>=y){
ans=mid;
r=mid-1;
}else{
l=mid+1;
}
}
cout<<ans<<"\n";
}
return 0;
}码风极为丑陋,大佬勿喷。