[Luo Gu P4602] CTSC2018 mixed fruit juice

Problem Description

R keen to do small dark dishes, especially mixed fruit juice. The store had n fruit juices, numbered 0, 1, 2,, n -... 1. I number of delicious juice is di, per liter price of pi. Small R in the production of mixed fruit juice, there are some special provisions, namely mixed in a bottle of juice, i can only add up numbers juice li liters. Now there are m looking for little children come to R mixed fruit juice drink, they all want to make a little R store juice mixed into a bottle of juice. Wherein the j-th his children desirable to mix juice obtained is not larger than the total price GJ, volume of not less than Lj. Under these restrictions, the children also want a delicious mix of fruit juice as high as possible, a bottle of delicious mixed fruit juice is equal to the minimum value of all participating juice mixed delicious degrees. Please calculate the most delicious taste of fruit juice mixed drink of each child.

Input Format

Read data from files juice.in in.

Input of the first line contains two positive integers n, m, represents the number of children and the number of kinds of fruit juice.

Next n lines, each line three positive integers di, pi, li, i represents deliciousness of fruit juice DI number, the price per liter of pi, adding an upper limit in a bottle of juice is from li.

Next m lines sequentially describe all children: two in each row gj number of positive integers, Lj describe a child, said he paid up to gj dollars, he wants at least Lj liters of juice.

Output Format

Juice.out the output to a file.

Sequentially output for all children: For each child, output one line containing an integer that represents the most delicious mix of delicious juice drink his degree. If you can not meet his needs, then output -1.

Sample input

3 4
1 3 5
2 1 3
3 2 5
6 3
5 3
10 10
20 10

Sample Output

3
2
-1
1

Explanation

For all of the test data to ensure that n, m ≤ 100000,1 ≤ di, pi, li ≤ 10 ^ 5,1 ≤ gj, Lj ≤ 10 ^ 18.

Resolve

By playing table and a series of metaphysics ways to find answers monotonic. Half a d, then we can only choose greater than or equal d juice. Then established for all d greater than or equal to the price of juice a target weight for the next segment tree, each node also saved about the total volume and price. This way we can use the value of the tree line in the right half of Lim.

But not once again the achievements of each half. We can build a Chairman of the tree can use persistence to solve this problem.

Code

#include <iostream>
#include <cstdio>
#include <algorithm>
#define int long long
#define N 100002
#define T 100000
using namespace std;
struct ChairmanTree{
    int l,r,suml,sump;
}t[N*40];
struct juice{
    int d,p,l;
}a[N];
int n,m,i,g[N],L[N],root[N],p;
int read()
{
    char c=getchar();
    int w=0;
    while(c<'0'||c>'9') c=getchar();
    while(c<='9'&&c>='0'){
        w=w*10+c-'0';
        c=getchar();
    }
    return w;
}
int my_comp(const juice &x,const juice &y)
{
    return x.d<y.d;
}
int insert(int pre,int l,int r,int L,int P)
{
    p++;
    int num=p;
    t[p]=t[pre];
    t[p].suml+=L;t[p].sump+=L*P;
    if(l<r){
        int mid=(l+r)/2;
        if(P<=mid) t[p].l=insert(t[pre].l,l,mid,L,P);
        else t[p].r=insert(t[pre].r,mid+1,r,L,P);
    }
    return num;
}
int ask(int p,int l,int r,int L)
{
    if(l==r) return l*L;
    int mid=(l+r)/2;
    if(L<=t[t[p].l].suml) return ask(t[p].l,l,mid,L);
    return t[t[p].l].sump+ask(t[p].r,mid+1,r,L-t[t[p].l].suml);
}
signed main()
{
    n=read();m=read();
    for(i=1;i<=n;i++) a[i].d=read(),a[i].p=read(),a[i].l=read();
    for(i=1;i<=m;i++) g[i]=read(),L[i]=read();
    a[0].d=-1;
    sort(a+1,a+n+1,my_comp);
    for(i=n;i>=1;i--) root[i]=insert(root[i+1],1,T,a[i].l,a[i].p);
    for(i=1;i<=m;i++){
        int l=1,r=T,mid,ans=0;
        while(l<=r){
            mid=(l+r)/2;
            if(L[i]<=t[root[mid]].suml&&ask(root[mid],1,T,L[i])<=g[i]){
                ans=mid;
                l=mid+1;
            }
            else r=mid-1;
        }
        printf("%lld\n",a[ans].d);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/LSlzf/p/11878957.html