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. ArcGIS api for javascript——加入地图并显示当前地图范围

    描述 这个示例使用Map.extent property属性接收地图范围的左下角和右上角坐标 "书签". 使用下列行创建地图: var map = new esri.Map(&qu ...

  2. apache kafka监控系列-KafkaOffsetMonitor

    apache kafka中国社区QQ群:162272557 概览 近期kafka server消息服务上线了,基于jmx指标參数也写到zabbix中了.但总认为缺少点什么东西.可视化可操作的界面. z ...

  3. 烦人的Facebook分享授权

    开发端授权app权限 facebook要求提交应用到他们平台, 并且还限制100mb, 坑爹死了, 果断使用google drive分享给他们, 最開始不确定分享给他们什么样的程序包, 结果审核没通过 ...

  4. android selector设置button点击效果(具体)以及常见问题

    button的点击效果学习起来其实比較easy,此点对开发人员来说也是使用的比較频繁的一个知识点,与它相关的还有编辑框的获取焦点时改变背景颜色.选择button选择时改变字体颜色等等.这些其实都是用到 ...

  5. Android Support Library 23.2用法简析

    写在前面的几句话 前几天谷歌发布了android-support-library-23.2支持库,这一次23.2版本增加了一些新的支持库以及新的功能.接下来这篇文章,就是对这些新功能部分做简单的用法介 ...

  6. Mysql基础部分,针对以后python使用

    #redis 非关系型数据库#mysql 关系型数据库 表与表之间有数据关系 Oracle Mysql SqlServer DB2#多张表组合在一起就是数据库#冗余 存储两倍数据 可以使系统速度更快 ...

  7. msiexec

    msiexec: runCmd = new String[]{ "msiexec", "/i", exeName, "/quiet", &q ...

  8. c# 读取 excel文件内容,写入txt文档

    1 winform 读取excel文档 1)点击button按钮,弹出上传excel窗口 private void button_headcompany_Click(object sender, Ev ...

  9. make---GNU编译工具

    make命令是GNU的工程化编译工具,用于编译众多相互关联的源代码问价,以实现工程化的管理,提高开发效率. 知识扩展 无论是在linux 还是在Unix环境 中,make都是一个非常重要的编译命令.不 ...

  10. 及格的产品vs优秀的产品

    类似的产品,做了同样的一个功能,但是,我们还是可以很明显的感受到不同,这种不同我们常常把他叫做「用户体验」. 来看看2组类似产品相似功能的设计: 识别到歌名vs还可以滚动展示歌词 在很早的时候就存在一 ...