Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 2363   Accepted: 881

Description

Like so many others, the cows have developed very haughty tastes and will no longer graze on just any grass. Instead, Farmer John must purchase gourmet organic grass at the Green Grass Grocers store for each of his N (1 ≤ N ≤ 100,000) cows.

Each cow i demands grass of price at least Ai (1 ≤ Ai ≤ 1,000,000,000) and with a greenness score at least Bi (1 ≤ Bi ≤ 1,000,000,000). The GGG store has M (1 ≤ M ≤ 100,000) different types of grass available, each with a price Ci (1 ≤ Ci ≤ 1,000,000,000) and a greenness score of Di (1 ≤ Di ≤ 1,000,000,000). Of course, no cow would sacrifice her individuality, so no two cows can have the same kind of grass.

Help Farmer John satisfy the cows' expensive gourmet tastes while spending as little money as is necessary.

Input

* Line 1: Two space-separated integers: N and M.
* Lines 2..N+1: Line i+1 contains two space-separated integers: Ai and Bi 
* Lines N+2..N+M+1: Line i+N+1 contains two space-separated integers: Ci and Di

Output

* Line 1: A single integer which is the minimum cost to satisfy all the cows. If that is not possible, output -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

Source

 
 
 
一开始一眼二分图,但是时间复杂度太高了
我们考虑贪心
因为他让着求最小的钱数
所以我们按照钱数排序,
然后枚举牧草,对于每一个牧草,我们统计一下它可以满足哪些奶牛
然后在能满足的奶牛中取一个需要的新鲜度离他最近的
这个过程显然可以用平衡树维护。
平衡树我用的是FHQ Treap
 
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
using namespace std;
#define ls T[now].ch[0]
#define rs T[now].ch[1]
const int MAXN=*1e5+;
inline char nc()
{
static char buf[MAXN],*p1=buf,*p2=buf;
return p1==p2&&(p2=(p1=buf)+fread(buf,,MAXN,stdin),p1==p2)?EOF:*p1++;
}
inline int read()
{
char c=nc();int x=,f=;
while(c<''||c>''){if(c=='-')f=-;c=nc();}
while(c>=''&&c<=''){x=x*+c-'',c=nc();}
return x*f;
}
struct node
{
int val,ch[],pri,siz;
}T[MAXN];
int x,y,z,root=,pos;
int tot=;
void update(int now)
{
T[now].siz=T[ls].siz+T[rs].siz+;
}
inline int newnode(int v)
{
T[++tot].val=v;
T[tot].pri=rand();
T[tot].siz=;
return tot;
}
int merge(int x,int y)
{
if(!x||!y) return x+y;
if(T[x].pri<T[y].pri)
{
T[x].ch[]=merge(T[x].ch[],y);
update(x);
return x;
}
else
{
T[y].ch[]=merge(x,T[y].ch[]);
update(y);
return y;
}
}
void split(int now,int k,int &x,int &y)
{
if(!now) {x=y=;return ;}
if(T[now].val<=k) x=now,split(rs,k,rs,y);
else y=now,split(ls,k,x,ls);
update(now);
}
struct N
{
int x,y;
}a[MAXN],b[MAXN];
int comp(const N &a,const N &b)
{
return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
void insert(int val)
{
split(root,val,x,y);
root=merge( merge(x,newnode(val)) , y );
}
int kth(int now,int x)
{
while(now)
{
if(T[ls].siz+==x) return now;
else if(T[ls].siz>=x) now=ls;
else x-=T[ls].siz+,now=rs;
}
}
void Delet(int now)
{
split(root,now,x,z);
split(x,now-,x,y);
y=merge(T[y].ch[] , T[y].ch[] );
root=merge( merge(x,y) ,z );
}
int main()
{
#ifdef WIN32
freopen("a.in","r",stdin);
#else
#endif
int n,m;
scanf("%d%d",&n,&m);
if (m<n){puts("-1");return ;}
for(int i=;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y);
for(int i=;i<=m;i++) scanf("%d%d",&b[i].x,&b[i].y);
sort(a+,a+n+,comp);
sort(b+,b+m+,comp);
long long int cur=,ans=,num=;//在第一个中找到了几个
for(int i=;i<=m;i++)
{
while(a[cur].x<=b[i].x&&cur<=n)
insert(a[cur].y),cur++;
split(root,b[i].y,x,y);
pos=kth(x,T[x].siz);
if(pos==) continue;
num++;
root=merge(x,y);
Delet(T[pos].val);
ans+=b[i].x;
}
if(num==n) printf("%lld",ans);
else printf("-1");
return ;
}
 

POJ3622 Gourmet Grazers(FHQ Treap)的更多相关文章

  1. 可持久化treap(FHQ treap)

    FHQ treap 的整理 treap = tree + heap,即同时满足二叉搜索树和堆的性质. 为了使树尽可能的保证两边的大小平衡,所以有一个key值,使他满足堆得性质,来维护树的平衡,key值 ...

  2. BZOJ3159: 决战(FHQ Treap)

    传送门: 解题思路: 算是补坑了,这题除了Invert以外就可以树剖线段树解决了. 考虑Invert操作,延续先前树链剖分的做法,考虑先前算法的瓶颈. 最暴力的方法是暴力交换权值,然而这种方法忽略了当 ...

  3. LOJ#105. 文艺平衡树(FHQ Treap)

    题面 传送门 题解 \(FHQ\ Treap\)比起\(Splay\)还是稍微好写一点--就是老是忘了要下穿标记-- //minamoto #include<bits/stdc++.h> ...

  4. 洛谷P3369 【模板】普通平衡树(FHQ Treap)

    题面 传送门 题解 写了一下\(FHQ\ Treap\) //minamoto #include<bits/stdc++.h> #define R register #define inl ...

  5. 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)

    题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...

  6. bzoj千题计划222:bzoj2329: [HNOI2011]括号修复(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2329 需要改变的括号序列一定长这样 :)))((( 最少改变次数= 多余的‘)’/2 [上取整] + ...

  7. bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...

  8. LOJ#120. 持久化序列(FHQ Treap)

    题面 传送门 题解 可持久化\(Treap\)搞一搞 //minamoto #include<bits/stdc++.h> #define R register #define inlin ...

  9. 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)

    题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...

随机推荐

  1. ubuntu中开启、关闭防火墙

    1.关闭ubuntu的防火墙        ufw disable 开启防火墙 ufw enable 2.卸载了iptables        apt-get remove iptables 3.关闭 ...

  2. F - Humidex(1.4.2)

    Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Descr ...

  3. rgba

    正反两面展示效果 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head& ...

  4. Centos7 zabbix3.4.6的安装部署 (一)

    部署zabbix主要为了监控日常主机.服务器.Web服务器.数据库.路由器.交换机等日常设备,功能强大,稳定性好 现在通过使用虚拟机VM搭建的Centos7部署zabbix服务 实现简单监控功能 本章 ...

  5. How Hystrix Works?--官方

    https://github.com/Netflix/Hystrix/wiki/How-it-Works Contents Flow Chart Circuit Breaker Isolation T ...

  6. 传说用户发来的请求是在JIoEndpoint的accept函数中接收的,是tomact与外界交互的分界点

    传说用户发来的请求是在JIoEndpoint的accept函数中接收的, 这是tomact与外界交互的分界点,所以来研究一下, >>>>>>>>> ...

  7. OPENCV(3) —— 对XML和YAML文件实现I/O 操作

    XML\YAML文件在OpenCV中的数据结构为FileStorage string filename = "I.xml"; FileStorage fs(filename, Fi ...

  8. Jesse's Code

    题目描述 Jesse是个数学迷,他最喜欢研究“哥德巴赫猜想”,因此他的计算机密码也都采用素数. 但一直用同一个密码是不安全的,所以他要经常更换他的密码.但他只允许自己的密码中出现某些数字,且密码的每一 ...

  9. Linux常用系统管理软件

    1.BleachBit是一款开源的系统清理工具,它可以释放磁盘空间,保护您的隐私,清除缓存,删除cookies.internet历史.临时文件.日志和丢弃的垃圾文件等,支持清除应用的残留数据,切碎文件 ...

  10. Linux常用Office办公软件

    1.WPS Office是由金山软件股份有限公司自主研发的一款办公软件套件,可以实现办公最常用的文字.表格.演示等多种功能.免费提供海量的在线存储空间及文档模板.支持阅读和输出PDF文件.全面兼容Mi ...