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. thinkphp5项目--企业单车网站(四)

    thinkphp5项目--企业单车网站(四) 项目地址 fry404006308/BicycleEnterpriseWebsite: Bicycle Enterprise Websitehttps:/ ...

  2. 一张图了解javaJwt

    1.什么是javaJwt? JSON Web Tokens are an open, industry standard RFC 7519 method for representing claims ...

  3. JavaScript Debug调试技巧

    收藏于:https://blog.fundebug.com/2017/12/04/javascript-debugging-for-beginners/

  4. 本地用户 vsftpd 配置文件

    # 禁止匿名用户anonymous登录 anonymous_enable=NO # 允许本地用户登录 local_enable=YES local_root=/data/wwwroot/ # 让登录的 ...

  5. 快速安装Nginx及配置详解(未完待续)

    导读: Nginx (engine x) 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器,从2007年被德国人开发出来后可以说在市场的占有率一路飙升,因为它支持高并 ...

  6. 【Uva 10118】Free Candies

    [Link]: [Description] 有4堆书; 每本书编号从1..20 每堆书都是N本; 然后每次只能从任意一堆的堆顶拿一本书装到自己的口袋里; 你的口袋最多容纳5本书; 当你的口袋里有两本一 ...

  7. CCF模拟 I’m stuck!

    I’m stuck! 时间限制: 1.0s 内存限制: 256.0MB   问题描述 给定一个R行C列的地图,地图的每一个方格可能是'#', '+', '-', '|', '.', 'S', 'T'七 ...

  8. 洛谷 P3913 车的攻击

    P3913 车的攻击 题目描述 N \times NN×N 的国际象棋棋盘上有KK 个车,第ii个车位于第R_iRi​行,第C_iCi​ 列.求至少被一个车攻击的格子数量. 车可以攻击所有同一行或者同 ...

  9. UVa 10101 - Bangla Numbers

    题目:将数字数转化成数字加单词的表示形式输出. 分析:数论.简单题.直接分成两部分除10000000的商和余数,分别输出就可以. 说明:注意输入为数字0的情况,还有long long类型防止溢出. # ...

  10. es63块级作用域

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...