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 ...
 
随机推荐
- 三 概要模式 2) MR倒排索引、性能分析、搜索干扰词。
			
二 倒排索引 倒排索引(英语:Inverted index),也常被称为反向索引.置入档案或反向档案,是一种索引方法,被用来存储在全文搜索下某个单词在一个文档或者一组文档中的存储位置的映射. ...
 - ArcGIS api for javascript——显示一个信息窗口
			
描述 这个示例展示了在用户单击地图时如何在InfoWindow中显示信息.信息窗口是一个dijit (Dojo widget).信息窗口能够包含文本,字符,图片和任何通过HTML表示的事物.这个例子在 ...
 - CF Mike and Feet (求连续区间内长度为i的最小值)单调栈
			
Mike and Feet time limit per test 1 second memory limit per test 256 megabytes input standard input ...
 - poj--1237--Drainage Ditches(最大流)
			
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %I64d & %I64u Sub ...
 - springboot 统一管理异常信息
			
新建ResponseEntityExceptionHandler的继承类:(依然,需要入口类扫描) /** * @author sky * @version 1.0 */ @ControllerAdv ...
 - POJ 3263 差分+set判重
			
题意: 思路: 对于每一个区间 [a,b] [a+1,b-1]肯定是比a,b低至少1的 因为题目要求最大值 所以就直接差分一下 搞之 (复杂度 O(n)) Discuss里说有重复的数据 用set判一 ...
 - JS对浏览器Cookie的操作,查询、设置以及删除
			
JavaScript是运行在客户端的脚本,因此一般是不能够设置Session的,因为Session是运行在服务器端的. 而cookie是运行在客户端的,所以可以用JS来设置cookie. 假设有这样一 ...
 - opengl绘制三维人物luweiqi
			
素材中有四个.bmp格式的纹理文件和一个.txt的模型参数文件 文件格式说明: 纹理文件数量 纹理文件1(字符串)//.bmp 纹理文件2(字符串) 纹理文件3(字符串) . . . 材质数量 amb ...
 - 摄像头驱动——V4L2框架分析
			
一.概述 Video for Linux 2,简称V4l2,是Linux内核中关于视频设备的内核驱动框架,为上层的访问底层的视频设备提供了统一的接口. 摄像头驱动是属于字符设备驱动程序.(分析linu ...
 - uniq---报告或忽略文件中的重复行
			
uniq命令用于报告或忽略文件中的重复行,一般与sort命令结合使用. 语法 uniq(选项)(参数) 选项 -c或——count:在每列旁边显示该行重复出现的次数: -d或--repeated:仅显 ...