This page is still under construction.

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

Trips

Interview

Time limit1sMemory limit128 MB

Summary
Match group sizes to trip intervals so that each interval gets at most one group and the number of matched intervals is maximized.
Level

Medium7 of 10

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

Problem

During the upcoming holiday season, many people want to take an unforgettable trip, and everyone prefers to travel together with a group of friends. A travel agency offers several group trips. For each trip the group size is restricted: a minimum and a maximum number of people are given. Each group may choose at most one trip, and each trip may be assigned to at most one group. A group can be assigned to a trip only if the group's size lies within that trip's allowed range.

The agency wants to organize as many trips as possible. Given the group sizes and the size limits of every trip, determine the maximum number of trips that can be arranged.

Input

The first line contains two integers nn and mm (1≤n≤4000001 \le n \le 400000, 1≤m≤4000001 \le m \le 400000): the number of groups and the number of trips. Groups are numbered from 11 to nn, and trips are numbered from 11 to mm.

Each of the next nn lines contains one integer sis_i (1≤si≤1091 \le s_i \le 10^9), the size of the ii-th group.

Each of the following mm lines contains two integers ljl_j and uju_j (1≤lj≤uj≤1091 \le l_j \le u_j \le 10^9), the minimum and the maximum group size that trip jj can accept.

Output

Output a single integer kk (k≥0k \ge 0): the maximum number of trips that can be arranged.

Examples3

  1. Example 1

    Input
    5 4
    54
    6
    9
    42
    15
    6 6
    20 50
    2 8
    7 20
    
    Expected output
    3
    
  2. Example 2

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

    Input
    3 3
    1
    2
    3
    1 1
    2 2
    3 3
    
    Expected output
    3