[Balance Tree] Picky Foodies

Problem Description

Like many cows, Farmer John's pampered cows are becoming increasingly fussy about their food, and the days of just grabbing a pile of hay to pass their lunch are long gone. Now, Farmer John has to go to a forage specialist to buy a lot of delicious, juicy forage to feed his N (1 <= N <= 100,000) picky cows.
All cows make her requirements for pasture to FJ: the i-th cow requires that the price of her food per serving is not less than A_i (1 <= A_i <= 1,000,000,000), and the freshness must not be less than B_i (1 <= B_i <= 1,000,000,000).
M (1 <= M <= 100,000) different grasses are available in the store, and the i-th grass is priced at C_i (1 <= C_i <= 1,000,000,000) and tender at D_i (1 <= D_i <= 1,000,000,000).
To show their difference, each cow requires her food to be unique, which means that no two cows will choose the same food. Farmer John wants to know what the minimum amount of money he has to spend on food to keep all the cows happy.

input format

Line 1: 2 integers separated by spaces: N and M
Lines 2..N+1: Line i+1 contains 2 integers separated by spaces: A_i, B_i
N+2..N +M+1 line: The j+N+1 line contains 2 integers separated by spaces: C_i, D_i

output format

Output 1 integer representing the minimum cost to satisfy all cows. Output -1 if all cows cannot be met anyway

Sample input 1

4 7
1 1
2 3
1 4
4 2
3 2
2 1
4 3
5 2
5 4
2 6
4 4

Sample output 1

12

Sample input 2

10 30
750 639
744 277
156 601
93 165
460 303
664 433
433 430
163 267
844 152
564 603
556 346
423 800
1042 1080
924 254
1001 983
373 852
1019 774
862 113
356 216
1319 185
141 177
674 48
939 202
219 1415
981 801
157 717
959 101
985 453
593 958
579 354
166 396
915 477
1214 1340
102 847
134 568
753 1008
984 665
867 907
1000 644
771 887

Sample output 2

5804

Example 1 Description

Give cow 1 to eat grass No. 2 of price 2, cow 2 to eat grass of No. 3 of price of 4, cow 3 to eat grass of No. 6 of price of 2, and cow 4 to choose grass of No. 7 of price of 4. This allocation scheme The total cost is 12, which is the least cost of all options.


answer

We first sort the requirements of the cows according to their freshness, and also sort the forages according to their freshness. Every time a cow is discussed, add all the forages whose tenderness is not lower than the required level into a balanced tree and arrange them according to the price, and then take out the successor of this point (cow) and add it to the answer. If there is no successor, output -1.


code

#include <cstdio>
#include <algorithm>
#include <iostream>
#define ll long long
using namespace std;
const ll Q=200005;
ll ls[Q],rs[Q],v[Q],f[Q],tot=0,n,root=0,temp[Q];
struct dt{
    ll gr,co;
}a[Q],b[Q];
bool cmp(dt a,dt b)
{return a.gr>b.gr;}
void lx(ll x)
{
    ll y=f[x],z=f[y];
    if(z)if(ls[z]==y)ls[z]=x;
    else rs[z]=x;
    f[x]=z;
    rs[y]=ls[x];
    f[rs[y]]=y;
    f[y]=x;
    ls[x]=y;
}
void rx(ll x)
{
    ll y=f[x],z=f[y];
    if(z)if(ls[z]==y)ls[z]=x;
    else rs[z]=x;
    f[x]=z;
    ls[y]=rs[x];
    f[ls[y]]=y;
    f[y]=x;
    rs[x]=y;
}
void splay(ll x)
{
    while(f[x])
    {
        ll y=f[x],z=f[y];
        if(z)
            if(ls[z]==y)
                if(ls[y]==x)rx(y),rx(x);
                else lx(x),rx(x);
            else if(rs[y]==x)lx(y),lx(x);
                else rx(x),lx(x);
        else
            if(ls[y]==x)rx(x);
            else lx(x);
    }
    root=x;
}
ll ins(ll x)
{
    if(!root){
        v[++tot]=x;temp[tot]=1;
        root=tot;
        f[tot]=0;
        return tot;
    }
    ll p=root;
    while(p){
        if(x==v[p]){++temp[p];splay(p);return p;}
        if(x<v[p])
            if(ls[p])p=ls[p];
            else {ls[p]=++tot;break;}
        else if(rs[p])p=rs[p];
            else {rs[p]=++tot;break;}
    }
    v[tot]=x;
    f[tot]=p;
    temp[tot]=1;
    splay(tot);
    return tot;
}
ll gm(ll p)
{
    if(!p)return -1;
    while(rs[p])p=rs[p];
    return p;
}
ll gs(ll p)
{
    if(!p)return -1;
    while(ls[p])p=ls[p];
    return p;
}
void del(ll x)
{
    splay(x);
    temp[x]--;
    if(temp[x]>0)return;
    ll lls=ls[x],rrs=rs[x];
    f[lls]=f[rrs]=ls[x]=rs[x]=0;
    if(!lls){root=rrs;return;}
    root=lls;
    lls=gm(lls);
    splay(lls);
    rs[lls]=rrs;
    f[rrs]=lls;
}
int main()
{
    ll i,n,m,t=1,ans=0;
    scanf("%lld%lld",&n,&m);
    for(i=1;i<=n;i++)
        scanf("%lld%lld",&a[i].co,&a[i].gr);
    for(i=1;i<=m;i++)
        scanf("%lld%lld",&b[i].co,&b[i].gr);
    sort(a+1,a+n+1,cmp),sort(b+1,b+m+1,cmp);
    for(i=1;i<=n;i++)
    {
        while(t<=m&&b[t].gr>=a[i].gr)ins(b[t].co),++t;
        if(root==0){printf("-1");return 0;}
        ll ha=ins(a[i].co-1);
        if(rs[ha]){
            ll temp=gs(rs[ha]);
            ans+=v[temp];
            del(ha),del(temp);
        }
        else{printf("-1",i,a[i].gr,a[i].co,ha);return 0;}
    }
    printf("%lld",ans);
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325654333&siteId=291194637