C - Chat Ban 注意二分边界条件吧
#include<bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
#define lowbit(x) (x&-x)
#define pf(a) printf("%d\n",a)
#define mem(x,y) memset(x,y,sizeof(x))
#define dbg(x) cout << #x << " = " << x << endl
#define rep(i,l,r) for(int i = l; i <= r; i++)
#define fep(i,a,b) for(int i=b; i>=a; --i)
#define PII pair<int,int>
typedef long long ll;
const int N = 1e6+10;
ll k,x;
void solve()
{
scanf("%lld %lld",&k, &x);
ll ans = 0;
if(x>(1+k)*k/2) {
ans += k;
x -= (1+k)*k/2;
ll l = 1, r = k-1;
while(l<=r){
ll mid = (l+r)>>1;
if((k-1+mid)*(k-mid)/2>=x) l = mid + 1;
else r = mid-1;
}
ans += (k-r);
}else{
ll l = 1, r = k;
while(l<=r){
ll mid = (l+r)>>1;
if((1+mid)*mid/2>=x) r = mid - 1;
else l = mid + 1;
}
ans = l;
}
printf("%lld\n",min(ans,2*k-1));
}
int main()
{
int t;
scanf("%d", &t);
while(t--)
solve();
return 0;
}
|