This page is still under construction.

Parts of this page are still being built. What you see may change.

Sunscreen

Interview

Time limit1sMemory limit128 MB

Summary
Each cow accepts an SPF interval, each bottle has an SPF value and capacity; assign bottles to maximize the number of cows covered.
Level

Medium5 of 10

Topics
Greedy, Sorting, Intervals, Two pointers
Solved
No attempts yet

Problem

To avoid an unsightly burn while tanning at the beach, each of the CC cows (1≤C≤25001 \le C \le 2500) must cover her hide with sunscreen. Cow ii works only with a range of SPF ratings, given by a minimum minSPFi\text{minSPF}_i and a maximum maxSPFi\text{maxSPF}_i (1≤minSPFi≤10001 \le \text{minSPF}_i \le 1000, minSPFi≤maxSPFi≤1000\text{minSPF}_i \le \text{maxSPF}_i \le 1000). If the SPF rating is too low the cow gets sunburned; if it is too high the cow does not tan at all.

The cows share a picnic basket holding LL bottles of sunscreen lotion (1≤L≤25001 \le L \le 2500). Bottle ii has an SPF rating SPFi\text{SPF}_i (1≤SPFi≤10001 \le \text{SPF}_i \le 1000) and can be applied to at most coveri\text{cover}_i cows. Each cow may use lotion from only one bottle.

Cow ii is protected by bottle jj if and only if minSPFi≤SPFj≤maxSPFi\text{minSPF}_i \le \text{SPF}_j \le \text{maxSPF}_i. What is the maximum number of cows that can protect themselves while tanning with the available lotion?

Input

  • Line 1: Two space-separated integers, CC and LL.
  • Lines 2 to C+1C+1: Line i+1i+1 describes cow ii with two integers, minSPFi\text{minSPF}_i and maxSPFi\text{maxSPF}_i.
  • Lines C+2C+2 to C+L+1C+L+1: Each line describes one lotion bottle with two space-separated integers, SPFi\text{SPF}_i and coveri\text{cover}_i.

Output

Print a single integer: the maximum number of cows that can be protected while tanning.

Examples4

  1. Example 1

    Input
    3 2
    3 10
    2 5
    1 5
    6 2
    4 1
    
    Expected output
    2
    
  2. Example 2

    Input
    1 1
    5 10
    3 1
    
    Expected output
    0
    
  3. Example 3

    Input
    1 1
    1 4
    5 1
    
    Expected output
    0
    
  4. Example 4

    Input
    3 1
    1 10
    1 10
    1 10
    5 3
    
    Expected output
    3