# 【题解】[CTSC2018]混合果汁

[CTSC2018]混合果汁

$$\text{Solution:}$$

#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MAXN=5e5+10;
const int INF=1e5+10;
int rt[INF],n,m;
struct T{int ls,sum,rs,sumv;}tr[MAXN<<5];
struct dk{int d,p,l;}q[INF];
int node;
int s=0,w=1;
char ch=getchar();
while(!isdigit(ch)) {
if(ch==‘-‘)w=-1;
ch=getchar();
}
while(isdigit(ch)) {
s=s*10-48+ch;
ch=getchar();
}
return w*s;
}
int change(int x,int l,int r,int pos,int v){
int p=++node;
tr[p]=tr[x];
tr[p].sum+=pos*v;
tr[p].sumv+=v;
if(l==r)return p;
int mid=(l+r)>>1;
if(pos<=mid)tr[p].ls=change(tr[p].ls,l,mid,pos,v);
else tr[p].rs=change(tr[p].rs,mid+1,r,pos,v);
return p;
}
bool flag;
int query_V(int x,int L,int R,int v){
int mid=(L+R)>>1;
if(L==R) {
if(v>tr[x].sumv) flag=1;
return v*L;
}
if(v<=tr[tr[x].ls].sumv)return query_V(tr[x].ls,L,mid,v);
return query_V(tr[x].rs,mid+1,R,v-tr[tr[x].ls].sumv)+tr[tr[x].ls].sum;
}
void solve(int c,int v){
//	c->prive
//	v->volume
//	Tree was built in c,and should follow the limit of the volume
//	consider checking answer:try to guess a delicious_percent,then we should check if is is can be got.
//	In the Tree,We have known the least price of this situation.
//	We should get the information of the price and volume through this tree.
//	Make sure:(Nowprice,Max) we can get sumv and sum(V and price.)
//	then we first consider volume.Price has been known and it is decrease.
//	we just need get the left position which sumv>V.
//	int L=1,R=n,ans=-1;
//	while(L<=R){
//		int mid=(L+R)>>1;
//		if(check(mid,v,c))ans=mid,R=mid-1;
//		else L=mid+1;
//	}
//	printf("%d\n",ans);
int L=1,R=n,ans=-1,qv=0;
while(L<=R){
int mid=(L+R)>>1;flag=0;
qv=query_V(rt[mid],1,INF,v);
if(qv>c||flag)L=mid+1;
else ans=mid,R=mid-1;
}

ans!=-1?printf("%lld\n",q[ans].d):puts("-1");
}
inline bool cmp(dk a,dk b){return a.d==b.d?a.p<b.p:a.d>b.d;}
signed main(){
sort(q+1,q+n+1,cmp);
for(int i=1;i<=n;++i)rt[i]=change(rt[i-1],1,INF,q[i].p,q[i].l);
for(int i=1;i<=m;++i){
solve(gi,Li);
}
return 0;
}


【题解】[CTSC2018]混合果汁

(0)
(0)