1691: [Usaco2007 Dec]挑剔的美食家

时间:2023-03-08 23:52:57
1691: [Usaco2007 Dec]挑剔的美食家

Time Limit: 5 Sec  Memory Limit: 64 MB
Submit: 621  Solved: 280
[Submit][Status][Discuss]

Description

与很多奶牛一样,Farmer John那群养尊处优的奶牛们对食物越来越挑剔,随便拿堆草就能打发她们午饭的日子自然是一去不返了。现在,Farmer John不得不去牧草专供商那里购买大量美味多汁的牧草,来满足他那N(1 <= N <= 100,000)头挑剔的奶牛。 所有奶牛都对FJ提出了她对牧草的要求:第i头奶牛要求她的食物每份的价钱不低于A_i(1 <= A_i <= 1,000,000,000),并且鲜嫩程度不能低于B_i(1 <= B_i <= 1,000,000,000)。商店里供应M(1 <= M <= 100,000)种不同的牧草,第i 种牧草的定价为C_i(1 <= C_i <= 1,000,000,000),鲜嫩程度为D_i (1 <= D_i <= 1,000,000,000)。 为了显示她们的与众不同,每头奶牛都要求她的食物是独一无二的,也就是说,没有哪两头奶牛会选择同一种食物。 Farmer John想知道,为了让所有奶牛满意,他最少得在购买食物上花多少钱。

Input

* 第1行: 2个用空格隔开的整数:N 和 M

* 第2..N+1行: 第i+1行包含2个用空格隔开的整数:A_i、B_i * 第N+2..N+M+1行: 第j+N+1行包含2个用空格隔开的整数:C_i、D_i

Output

* 第1行: 输出1个整数,表示使所有奶牛满意的最小花费。如果无论如何都无法 满足所有奶牛的需求,输出-1

Sample Input

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

12

输出说明:
给奶牛1吃价钱为2的2号牧草,奶牛2吃价钱为4的3号牧草,奶牛3分到价钱
为2的6号牧草,奶牛4选择价钱为4的7号牧草,这种分配方案的总花费是12,为
所有方案中花费最少的。

Source Gold

题解

  让牛和草按照鲜嫩度排序,然后对于第i头奶牛,把所有新鲜度大于它要求的价值塞到一个伸展树里,每次ANS加上当前伸展树中它要求的价值的后继,但一定要先判断一下有没有和它要求的价值正好相等的草。

 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long LL;
const LL maxn=;
LL key[maxn],lc[maxn],rc[maxn],fa[maxn],siz[maxn];
LL tot,root;
LL N,M,ANS;
struct COW{
LL a,b;
}cow[maxn];
struct G{
LL a,b;
}gra[maxn];
bool cmp(const COW&w,const COW &e){
if(w.b>e.b) return ;
return ;
}
bool cmp2(const G&w,const G &e){
if(w.b>e.b) return ;
return ;
}
void update(LL x){
siz[x]=siz[lc[x]]++siz[rc[x]];
}
void r_rotate(LL x){
LL y=fa[x];
lc[y]=rc[x];
if(rc[x]!=) fa[rc[x]]=y;
fa[x]=fa[y];
if(y==lc[fa[y]]) lc[fa[y]]=x;
else rc[fa[y]]=x;
fa[y]=x; rc[x]=y;
update(x); update(y);
}
void l_rotate(LL x){
LL y=fa[x];
rc[y]=lc[x];
if(lc[x]!=) fa[lc[x]]=y;
fa[x]=fa[y];
if(y==lc[fa[y]]) lc[fa[y]]=x;
else rc[fa[y]]=x;
fa[y]=x; lc[x]=y;
update(x); update(y);
}
void splay(LL x,LL s){
LL p;
while(fa[x]!=s){
p=fa[x];
if(fa[p]==s){
if(x==lc[p]) r_rotate(x);
else l_rotate(x);
break;
}
if(x==lc[p]){
if(p==lc[fa[p]]) r_rotate(p),r_rotate(x);
else r_rotate(x),l_rotate(x);
}
else{
if(p==rc[fa[p]]) l_rotate(p),l_rotate(x);
else l_rotate(x),r_rotate(x);
}
}
if(s==) root=x;
update(x);
}
LL find(LL v){//查找在这棵树中键值为v的节点
LL x=root;
while(x!=){
if(v<key[x]) x=lc[x];
else if(v>key[x]) x=rc[x];
else if(v==key[x]){
splay(x,);
return x;
}
}
return -;
}
void New_node(LL &x,LL fath,LL v){//建立新节点
x=++tot;
lc[x]=rc[x]=; siz[x]=;
fa[x]=fath;
key[x]=v;
}
void insert(LL v){//插入新节点
if(root==){
New_node(rc[],,v);
root=tot;
return ;
}
LL p,x=root;
while(x!=){
p=x;
if(v<=key[x]) siz[x]++,x=lc[x];
else siz[x]++,x=rc[x];
}
if(v<=key[p]) New_node(lc[p],p,v);
else New_node(rc[p],p,v);
splay(tot,);
}
LL getmax(LL x){//找到以x为根的最大值
if(rc[x]!=) return getmax(rc[x]);
return x;
}
LL getmin(LL x){//找到以x为根的最小值
if(lc[x]!=) return getmin(lc[x]);
return x;
}
void Delete(LL v){
LL x=find(v);
LL pp=getmax(lc[x]);
LL nn=getmin(rc[x]);
if(lc[x]==||rc[x]==){
if(lc[x]==&&rc[x]==){
root=; rc[]=;
return ;
}
if(lc[x]==){
rc[]=rc[x]; fa[rc[x]]=; root=rc[x]; rc[x]=;
siz[x]=;
return ;
}
else{
rc[]=lc[x]; fa[lc[x]]=; root=lc[x]; lc[x]=;
siz[x]=;
return ;
}
}
splay(pp,);
splay(nn,root);
fa[lc[nn]]=; siz[lc[nn]]=; lc[nn]=;
update(nn); update(pp);
}
LL succ(LL rt,LL v){//返回比 v大的最小的数
if(rt==) return v;
if(v>=key[rt]) return succ(rc[rt],v);
else{
LL ans=succ(lc[rt],v);
if(ans==v) return key[rt];
return ans;
}
}
int main(){
scanf("%lld%lld",&N,&M);
if(M<N){
printf("-1");
return ;
}
for(LL i=;i<=N;i++) scanf("%lld%lld",&cow[i].a,&cow[i].b);
for(LL i=;i<=M;i++) scanf("%lld%lld",&gra[i].a,&gra[i].b);
sort(cow+,cow+N+,cmp); sort(gra+,gra+M+,cmp2);
for(LL i=,j=;i<=N;i++){
while(gra[j].b>=cow[i].b&&j<=M)
insert(gra[j++].a);
if(siz[root]==){printf("-1"); return ;}
if(find(cow[i].a)!=-){
ANS+=cow[i].a;
Delete(cow[i].a);
}
else{
LL num=succ(root,cow[i].a);
ANS+=num;
Delete(num);
}
}
printf("%lld",ANS);
return ;
}