This page is still under construction.

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

Demand-Responsive Bus

Interview

Time limit1sMemory limit1024 MB

Summary
Match as many requests (passengers, max wait) to buses (capacity, arrival) as possible, where a bus fits a request if its capacity and arrival time are both at least as small or large as required.
Level

Medium7 of 10

Topics
Greedy, Sorting, Two pointers, Binary search
Solved
No attempts yet

Problem

Hyundai AutoEver is a company that works on software and infrastructure across both the In-Car and Out-Car domains. Hyundai AutoEver is currently developing a demand-responsive bus (MOD). A demand-responsive bus generates the fastest route in real time and dispatches a vehicle when a passenger calls for one. It is a new mobility solution that can improve convenience for residents in the intermediate stage of urban development, when a route system is just beginning to take shape. Because it is dispatched and operated by real-time calls without fixed routes, the waiting time and travel time for citizens are shortened, which greatly improves public transit convenience.

You are developing a system that matches requests simultaneously when they flood in, such as during rush hour. There are NN dispatch requests, and each dispatch request consists of the number of passengers and the maximum waiting time. There are MM buses, and each bus has a capacity and an estimated arrival time. A bus does not wait more than 11 minute after arriving before leaving.

To assign a bus to a dispatch request, the bus capacity must be greater than or equal to the number of passengers, and the estimated arrival time must be less than or equal to the maximum waiting time. For example, if the number of passengers is 44 and the maximum waiting time is 77 minutes, a bus with capacity of at least 44 and an estimated arrival time within 77 minutes can be dispatched.

When matching dispatch requests with buses, there is a constraint that they must correspond one-to-one. That is, a single dispatch request cannot be assigned to two or more buses, and conversely, assigning a single bus to two or more dispatch requests is not permitted by policy. Also, multiple requests can be processed simultaneously at the same time.

You must implement a program that matches buses to as many dispatch requests as possible.

Input

The input is given as follows.

NN MM
a1a_1 b1b_1
…\dots
aNa_N bNb_N
c1c_1 d1d_1
…\dots
cMc_M dMd_M

  • NN is the number of dispatch requests, and MM is the number of buses. (1≤N,M≤200 0001 \le N, M \le 200\ 000)
  • aia_i and bib_i represent the information of a dispatch request. They indicate that the ii-th dispatch request has aia_i passengers and a maximum waiting time of bib_i minutes. (1≤ai,bi≤1091 \le a_i, b_i \le 10^9)
  • cjc_j and djd_j represent the information of a bus. They indicate that the jj-th bus has a capacity of cjc_j people and an estimated arrival time of djd_j minutes. (1≤cj,dj≤1091 \le c_j, d_j \le 10^9)
  • All numbers in the input are integers.

Output

On the first line, print the maximum number of dispatch requests that can have a bus assigned.

Examples3

  1. Example 1

    Input
    2 2
    4 3
    7 4
    5 2
    8 3
    
    Expected output
    2
    
  2. Example 2

    Input
    3 2
    4 3
    1 3
    7 4
    5 2
    8 3
    
    Expected output
    2
    
  3. Example 3

    Input
    1 2
    4 3
    2 3
    2 3
    
    Expected output
    0