二分 - Matrix - POJ - 3685
題意:
數據範圍:
Sample Input
12
1 1
2 1
2 2
2 3
2 4
3 1
3 2
3 8
3 9
5 1
5 25
5 10
Sample Output
3
-99993
3
12
100007
-199987
-99993
100019
200013
-399969
400031
-99939
分析:
代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define ll long long
#define P pair<int,int>
#define x first
#define y second
using namespace std;
ll T,n,m;
ll f(ll i,ll j)
{
return i*i+100000*i+j*j-100000*j+i*j;
}
ll cal(ll t)
{
ll l,r,res=0;
for(ll j=1;j<=n;j++)
{
l=0,r=n;
while(l<r)
{
ll mid=l+r+1>>1;
if(f(mid,j)<=t) l=mid;
else r=mid-1;
}
res+=l;
}
return res;
}
int main()
{
cin>>T;
while(T--)
{
cin>>n>>m;
ll l=-1e12,r=1e12;
while(l<r)
{
ll mid=l+r>>1;
if(cal(mid)>=m) r=mid;
else l=mid+1;
}
printf("%lld\n",l);
}
return 0;
}