牛客小白月赛18 G Forsaken的三维数点
思路:
这是一道树状数组和二分的题,用线段树空间直接爆,时间也会超
然后这道题我犯了一个很低级的错误,导致我wa了十发左右,一个int型变量用lld输入,然后他给的提示是运行错误,我哭了,我一直以为是是空间爆了
代码:
#include<bits/stdc++.h> using namespace std; typedef long long ll; #define lowbit(x) x&(-x) ll x,y,z,mx,cnt; const int maxn = 2e5; ll tree[maxn+5]; void add(ll x,ll k){ for(ll i=x;i<=maxn;i+=lowbit(i)){ tree[i]+=k; } } ll query(ll x){ ll res = 0; for(ll i = x;i>0;i-=lowbit(i)) res +=tree[i]; return res; } int main(){ int n; cin>>n; int op; for(int i=1;i<=n;i++){ cin>>op; if(op==1){ scanf("%lld%lld%lld",&x,&y,&z); double d=sqrt(x*x+y*y+z*z); ll p = ceil(d); mx = max(mx,p); cnt++; add(p,1); } else if(op==2){ int k; scanf("%d",&k); if(k>cnt){ cout<<"-1"<<endl; continue; } ll l=0,r = mx,mid; while(l<=r){ mid = (l+r)>>1; if(query(mid)>=k) r =mid-1; else l = mid+1; } cout<<l<<endl; } } return 0; }