POJ3622 Gourmet Grazers(FHQ Treap)
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
#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)的更多相关文章
- 可持久化treap(FHQ treap)
FHQ treap 的整理 treap = tree + heap,即同时满足二叉搜索树和堆的性质. 为了使树尽可能的保证两边的大小平衡,所以有一个key值,使他满足堆得性质,来维护树的平衡,key值 ...
- BZOJ3159: 决战(FHQ Treap)
传送门: 解题思路: 算是补坑了,这题除了Invert以外就可以树剖线段树解决了. 考虑Invert操作,延续先前树链剖分的做法,考虑先前算法的瓶颈. 最暴力的方法是暴力交换权值,然而这种方法忽略了当 ...
- LOJ#105. 文艺平衡树(FHQ Treap)
题面 传送门 题解 \(FHQ\ Treap\)比起\(Splay\)还是稍微好写一点--就是老是忘了要下穿标记-- //minamoto #include<bits/stdc++.h> ...
- 洛谷P3369 【模板】普通平衡树(FHQ Treap)
题面 传送门 题解 写了一下\(FHQ\ Treap\) //minamoto #include<bits/stdc++.h> #define R register #define inl ...
- 洛谷P3391 【模板】文艺平衡树(Splay)(FHQ Treap)
题目背景 这是一道经典的Splay模板题——文艺平衡树. 题目描述 您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1, ...
- bzoj千题计划222:bzoj2329: [HNOI2011]括号修复(fhq treap)
http://www.lydsy.com/JudgeOnline/problem.php?id=2329 需要改变的括号序列一定长这样 :)))((( 最少改变次数= 多余的‘)’/2 [上取整] + ...
- bzoj千题计划221:bzoj1500: [NOI2005]维修数列(fhq treap)
http://www.lydsy.com/JudgeOnline/problem.php?id=1500 1.覆盖标记用INF表示无覆盖标记,要求可能用0覆盖 2.代表空节点的0号节点和首尾的两个虚拟 ...
- LOJ#120. 持久化序列(FHQ Treap)
题面 传送门 题解 可持久化\(Treap\)搞一搞 //minamoto #include<bits/stdc++.h> #define R register #define inlin ...
- 洛谷P5055 【模板】可持久化文艺平衡树(FHQ Treap)
题面 传送门 题解 日常敲板子.jpg //minamoto #include<bits/stdc++.h> #define R register #define inline __inl ...
随机推荐
- FPGA设计中的电源管理(转载)
过去,FPGA设计者主要关心时序和面积使用率问题.但随着FPGA不断取代ASSP和ASIC器件,设计者们现正期望能够开发低功耗设计,在设计流程早期就能对功耗进行正确估算,以及管理和对与FPGA相关的各 ...
- 125.C++输入小结
#include <iostream> #include <iomanip> #include <cstring> #include <cstdlib> ...
- Redis的安装与启动(doc和本地客户端)
官网 安装都是老生长谈了(这个也不错),这里推荐俩个文章看看把.:打开一个cmd窗口 使用cd命令切换目录到 C:\redis 运行 redis-server.exe redis.conf(安装的关键 ...
- 硬件时间,操作系统时间,Windows 和linux 双系统时间差8小时问题说明
1.硬件时间:硬件时钟是存储在主板上CMOS里的时间即BIOS时间,关机后该时钟依然运行,主板的电池为它供电.对应于嵌入式设备有一个RTC模块.硬件时钟即RTC时钟.信息比较少没时区.夏令时的概念. ...
- ES6学习笔记(一)新的变量定义命令let和const
1.一些历史 ES6(ECMAScript 6.0)是 JavaScript 语言的新一代标准,于2015 年 6 月正式发布,距今已经4年了,它的目标,是使得 JavaScript 语言可以用来编写 ...
- Java main方法中的String[] args
-- Java 命令行参数 -- 关于其中的args以及public static / static public Java 命令行参数 前面已经看到多个使用Java数组的示例,每一个Java应用程序 ...
- Linux下java/bin目录下的命令集合
Linux下JAVA命令(1.7.0_79) 命令 详解 参数列表 示例 重要程度 资料 appletviewer Java applet 浏览器.appletviewer 命令可在脱离万维网浏览器环 ...
- 统计学习:《贝叶斯思维统计建模的Python学习法》中文PDF+英文PDF+代码
用数学工具解决实际问题仅有的要求可能就是懂一点概率知识和程序设计.而贝叶斯方法是一种常见的利用概率学知识去解决不确定性问题的数学方法,对于一个计算机专业的人士,应当熟悉其应用在诸如机器翻译,语音识别, ...
- 学习参考《矩阵分析与应用(第二版)张贤达》PDF
要想深入理解机器学习,或者对人工智能的某个领域有所研究,都必须掌握矩阵及其应用. 学习<矩阵分析与应用第2版>时,会发现总结了大量线性代数的知识,主要是给工科生用的.归纳了不少论文中的解法 ...
- Python, Django 性能分析工具的使用
最近接手的 Apache HUE 项目性能出现了问题,线上经常出现响应时间过长或因为时间过长而无法服务等问题.老大让我准备弄个性能分析工具,便于追踪和分析平台当前的瓶颈出现在哪里. 那就搞起吧!先从代 ...