BZOJ5343: [Ctsc2018]混合果汁 二分答案+主席树
分析:
整体二分或二分答案+主席树,反正没有要求强制在线,两个都可以做...
贪心还是比较显然的,那么就是找前K大的和...和CQOI的任务查询系统很像
附上代码:
#include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <iostream> #include <queue> #include <algorithm> using namespace std; #define N 100005 #define ll long long #define lson l,m,tr[rt].ls #define rson m+1,r,tr[rt].rs struct node{int ls,rs;long long sum,siz;}tr[N*20]; struct A{int d,p,l;}a[N]; int n,Q,rot[N],cnt; bool cmp(const A &a,const A &b){return a.d<b.d;} void insert(int x,int v,int c,int l,int r,int &rt) { rt=++cnt;tr[rt].siz=tr[x].siz+c;tr[rt].sum=tr[x].sum+1ll*v*c;if(l==r)return;int m=(l+r)>>1; if(v<=m)tr[rt].rs=tr[x].rs,insert(tr[x].ls,v,c,lson);else tr[rt].ls=tr[x].ls,insert(tr[x].rs,v,c,rson); } ll query(int x,ll k,int l,int r,int rt) { // printf("%d %d %lld %lld %lld\n",l,r,k,tr[tr[x].ls].sum,tr[tr[rt].ls].sum); if(l==r)return k*l;int m=(l+r)>>1;ll sizls=tr[tr[rt].ls].siz-tr[tr[x].ls].siz; if(sizls>=k)return query(tr[x].ls,k,lson);return query(tr[x].rs,k-sizls,rson)+tr[tr[rt].ls].sum-tr[tr[x].ls].sum; } int check(int m,ll x,ll y) { if(!m)return 0; if(tr[rot[100000]].siz-tr[rot[m-1]].siz<y)return 0; ll t1=query(rot[m-1],y,1,100000,rot[100000]); // printf("%d %lld\n",m,t1); return t1<=x; } int main() { scanf("%d%d",&n,&Q); for(int i=1;i<=n;i++)scanf("%d%d%d",&a[i].d,&a[i].p,&a[i].l);sort(a+1,a+n+1,cmp); for(int i=1,h=1;i<=100000;i++) { rot[i]=rot[i-1]; while(a[h].d==i)insert(rot[i],a[h].p,a[h].l,1,100000,rot[i]),h++; } while(Q--) { ll x,y;scanf("%lld%lld",&x,&y); int l=0,r=100001; while(l<r) { int m=(l+r)>>1; if(check(m,x,y))l=m+1; else r=m; } printf("%d\n",l-1); } }