题解
先把 \(d\) 二分,然后按价格插入主席树,主席树中维护当前区间水果的重量和与总价值,大概就是这样子.
#include<bits/stdc++.h>
using namespace std;
#define re register
#define int long long
#define in inline
#define get getchar()
in int read()
{
int t=0,x=1; char ch=get;
while((ch<'0' || ch>'9') && ch!='-') ch=get;
if(ch=='-') ch=get,x=-1;
while(ch<='9' && ch>='0') t=t*10+ch-'0', ch=get;
return t*x;
}
const int _=1e5+6;
struct juice{
int d,p,l;
bool operator < (const juice& other)const
{
return d < other.d;
}
}a[_];
int sumc[_<<6],suml[_<<6],ls[_<<6],rs[_<<6],tot,n,m,G,L,root[_];
in void add(int k,int &now, int l,int r,int p,int v)
{
now=++tot;
ls[now]=ls[k],rs[now]=rs[k],sumc[now]=sumc[k]+p*v,suml[now]=suml[k]+v;
if(l==r)
return;
int mid=l+r>>1;
if(p<=mid) add(ls[k],ls[now],l,mid,p,v);
else add(rs[k],rs[now],mid+1,r,p,v);
return;
}
in int query(int k1,int k2,int l,int r,int x)
{
if(l==r) return min(x/l, suml[k2]-suml[k1]);
int s=sumc[ls[k2]]-sumc[ls[k1]],mid=l+r>>1;
if(x<=s) return query(ls[k1],ls[k2],l,mid,x);
else return suml[ls[k2]]-suml[ls[k1]]+query(rs[k1],rs[k2],mid+1,r,x-s);
}
in bool check(int i)
{
int qwe=query(root[i-1],root[n],1,_-6,G);
// cout<<qwe<<endl;
return qwe>=L;
}
in int getans()
{
int l=0,r=n;
while(l<r)
{
int mid=l+r+1>>1;
if(check(mid)) l=mid;
else r=mid-1;
}
return !l ? -1 :a[l].d;
}
signed main()
{
n=read(),m=read();
for(re int i=1;i<=n;i++)
a[i].d=read(),a[i].p=read(),a[i].l=read();
sort(a+1,a+n+1);
for( int i=1;i<=n;i++) add(root[i-1],root[i],1,_-6,a[i].p,a[i].l);
for(re int i=1;i<=m;i++)
{
G=read(),L=read();
printf("%lld\n",getans());
}
return 0;
}