This page is still under construction.

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

We Need Masks

Time limit3sMemory limit1024 MB

Summary
Each citizen accepts mask prices in a range [L, R], each store sells X masks at price P, and we must match as many citizens to masks as possible.
Level

Medium7 of 10

Topics
Greedy, Sorting, Heap, Intervals
Solved
No attempts yet

Problem

After COVID-19 broke out, the need for masks began to grow. Without a mask, daily life is difficult, so you must always keep masks on hand.

As demand for masks rose, mask prices that had been uniform began to differ from store to store. Because the prices varied, it became hard for every citizen of city A to get a mask. As a civil servant of city A, you asked the store owners to make mask prices uniform in order to resolve the situation, but the store owners ignored you, as expected.

Judging that persuading the store owners is difficult, you changed your plan to letting as many citizens as possible get masks. You found out the range of money each citizen of city A can spend on a mask, and the price and quantity of the masks sold at each store in city A. Each citizen can buy at most one mask. Based on this information, let as many citizens as possible get masks.

Input

The first line gives NN, the number of citizens of city A, and MM, the number of stores in city A. (1≤N,M≤500,0001 \le N, M \le 500{,}000)

Lines 2 through N+1N + 1 give LiL_i and RiR_i, the range of money the ii-th citizen of city A can spend on a mask. That is, the price of a mask the ii-th citizen of city A can buy is at least LiL_i and at most RiR_i. (1≤Li≤Ri≤10181 \le L_i \le R_i \le 10^{18})

Lines N+2N + 2 through N+M+1N + M + 1 give PjP_j, the price of the masks sold by the jj-th store in city A, and XjX_j, the number of masks. (1≤Pj≤10181 \le P_j \le 10^{18}, 1≤Xj≤1,0001 \le X_j \le 1{,}000)

All of LiL_i, RiR_i, PjP_j, and XjX_j are integers.

Output

Print the number of citizens who bought a mask when as many citizens as possible have bought one.

Examples3

  1. Example 1

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

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

    Input
    3 3
    3 5
    10 15
    5 10
    4 1
    5 1
    16 3
    
    Expected output
    2