见代码中的注释:
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int mod = 1e9+7;
int n,s,l[200005],r[200005],pl,x,y,p,t[200005],cnt;
bool check(int x){
p = 0,cnt = 0;
for(int i = 1;i<=n;i++){
if(r[i]>=x)t[++p] = max(x-l[i],0LL);
}
if(p<n/2+1)return 0;
sort(t+1,t+p+1);
for(int i = 1;i<=n/2+1;i++)cnt+=t[i];
return cnt<=s-pl;
}
int find(){
int l = 0,r = 1e18;
while(l<r){
int mid = (l+r)/2;
if(check(mid))l = mid+1;
else r = mid;
}
//return r;
return r-1;//这里为什么要减一?
}
signed main(){
int t;
cin >> t;
while(t--){
pl = 0;
cin >> n >> s;
for(int i = 1;i<=n;i++){
cin >> l[i] >> r[i];
pl+=l[i];
}
cout << find() << "\n";
}
return 0;
}