# POJ3622 Gourmet Grazers（FHQ Treap）

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

USACO 2007 December Gold

```  1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 #include<ctime>
5 #include<cstdlib>
6 #include<algorithm>
7 using namespace std;
8 #define ls T[now].ch[0]
9 #define rs T[now].ch[1]
10 const int MAXN=3*1e5+10;
11 inline char nc()
12 {
13     static char buf[MAXN],*p1=buf,*p2=buf;
15 }
17 {
18     char c=nc();int x=0,f=1;
19     while(c<'0'||c>'9'){if(c=='-')f=-1;c=nc();}
20     while(c>='0'&&c<='9'){x=x*10+c-'0',c=nc();}
21     return x*f;
22 }
23 struct node
24 {
25     int val,ch[2],pri,siz;
26 }T[MAXN];
27 int x,y,z,root=0,pos;
28 int tot=0;
29 void update(int now)
30 {
31     T[now].siz=T[ls].siz+T[rs].siz+1;
32 }
33 inline int newnode(int v)
34 {
35     T[++tot].val=v;
36     T[tot].pri=rand();
37     T[tot].siz=1;
39 }
40 int merge(int x,int y)
41 {
42     if(!x||!y)    return x+y;
43     if(T[x].pri<T[y].pri)
44     {
45         T[x].ch[1]=merge(T[x].ch[1],y);
46         update(x);
47         return x;
48     }
49     else
50     {
51         T[y].ch[0]=merge(x,T[y].ch[0]);
52         update(y);
53         return y;
54     }
55 }
56 void split(int now,int k,int &x,int &y)
57 {
58     if(!now)    {x=y=0;return ;}
59     if(T[now].val<=k)    x=now,split(rs,k,rs,y);
60     else y=now,split(ls,k,x,ls);
61     update(now);
62 }
63 struct N
64 {
65     int x,y;
66 }a[MAXN],b[MAXN];
67 int comp(const N &a,const N &b)
68 {
69     return a.x<b.x||(a.x==b.x&&a.y<b.y);
70 }
71 void insert(int val)
72 {
73     split(root,val,x,y);
74     root=merge( merge(x,newnode(val)) , y );
75 }
76 int kth(int now,int x)
77 {
78     while(now)
79     {
80         if(T[ls].siz+1==x)    return now;
81         else if(T[ls].siz>=x)    now=ls;
82         else x-=T[ls].siz+1,now=rs;
83     }
84 }
85 void Delet(int now)
86 {
87     split(root,now,x,z);
88     split(x,now-1,x,y);
89     y=merge(T[y].ch[0] , T[y].ch[1] );
90     root=merge( merge(x,y) ,z );
91 }
92 int main()
93 {
94     #ifdef WIN32
95     freopen("a.in","r",stdin);
96     #else
97     #endif
98     int n,m;
99     scanf("%d%d",&n,&m);
100     if (m<n){puts("-1");return 0;}
101     for(int i=1;i<=n;i++)    scanf("%d%d",&a[i].x,&a[i].y);
102     for(int i=1;i<=m;i++)    scanf("%d%d",&b[i].x,&b[i].y);
103     sort(a+1,a+n+1,comp);
104     sort(b+1,b+m+1,comp);
105     long long int cur=1,ans=0,num=0;//在第一个中找到了几个
106     for(int i=1;i<=m;i++)
107     {
108         while(a[cur].x<=b[i].x&&cur<=n)
109             insert(a[cur].y),cur++;
110         split(root,b[i].y,x,y);
111         pos=kth(x,T[x].siz);
112         if(pos==0)    continue;
113         num++;
114         root=merge(x,y);
115         Delet(T[pos].val);
116         ans+=b[i].x;
117     }
118     if(num==n) printf("%lld",ans);
119     else printf("-1");
120     return 0;
121 }```

1811 篇文章118 人订阅

0 条评论

## 相关文章

### 3361: [Usaco2004 Jan]培根距离

3361: [Usaco2004 Jan]培根距离 Time Limit: 10 Sec  Memory Limit: 128 MB Submit: 16  S...

3555

### FZU 2167 大王叫我来巡山呐

Problem 2167 大王叫我来巡山呐 Accept: 931    Submit: 1405 Time Limit: 1000 mSec    Memo...

3609

### Q122 Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on...

3475

### 【学习经验】Java中常用英文

【学习经验】Java中常用英文 第一章： public['pʌblik] 公共的,公用的 static['stætik] 静的;静态的...

36610

### BZOJ1061: [Noi2008]志愿者招募(线性规划)

申奥成功后，布布经过不懈努力，终于成为奥组委下属公司人力资源部门的主管。布布刚上任就遇到了一个难

1464

### carla无人驾驶模拟中文项目 carla_simulator_Chinese

https://github.com/xfqbuaa/carla_simulator_Chinese

1263

### 1934: [Shoi2007]Vote 善意的投票

1934: [Shoi2007]Vote 善意的投票 Time Limit: 1 Sec  Memory Limit: 64 MB Submit: 1174  ...

2847

2021

901

### Akka（41）： Http：DBTable-rows streaming - 数据库表行交换

在前面一篇讨论里我们介绍了通过http进行文件的交换。因为文件内容是以一堆bytes来表示的，而http消息的数据部分也是byte类型的，所以我们可以直接用...

2547