This page is still under construction.

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

Disco

Time limit1sMemory limit1024 MB

Summary
Given N disjoint lit intervals on a line of L lamps and M switches that each flip a range, decide if some subset of switches turns every lamp off.
Level

Medium7 of 10

Topics
Intervals, Greedy, Prefix sum, Sorting
Solved
No attempts yet

Problem

Juss recently became very rich. As a passionate disco fan, he decided to build himself a disco room. A disco room needs a lot of colorful lamps, so Juss installed LL lamps, numbered from 11 to LL.

Turning the lamps on and off one by one is tedious, so Juss also installed MM switches, numbered from 11 to MM. Pressing switch ii flips the state of every lamp numbered from CiC_i to DiD_i (each such lamp turns off if it was on, and on if it was off).

After a party, Juss wanted to turn all the lamps off, but they were left in such a strange state that he could not figure out how to turn them off with the switches he has. Precisely, the lamps that are currently on form NN groups. Group ii contains every lamp numbered from AiA_i to BiB_i, and for every i>1i > 1 we have Bi−1+1<AiB_{i-1} + 1 < A_i (so the groups are given in increasing order and are pairwise disjoint and non-adjacent).

Each switch may be pressed at most once (pressing the same switch twice is the same as not pressing it). Write a program that decides whether it is possible to turn off all the lamps by pressing a suitable subset of the switches.

Constraints:

  • 1≤L≤1091 \le L \le 10^9
  • 1≤N≤1051 \le N \le 10^5
  • 1≤M≤1051 \le M \le 10^5
  • 1≤Ai≤Bi≤L1 \le A_i \le B_i \le L, and Bi−1+1<AiB_{i-1} + 1 < A_i for every i>1i > 1
  • 1≤Ci≤Di≤L1 \le C_i \le D_i \le L

Input

The first line contains the integer LL. The second line contains the integer NN. Each of the next NN lines contains two integers AiA_i and BiB_i. The next line contains the integer MM. Each of the last MM lines contains two integers CiC_i and DiD_i.

Output

Print YES if it is possible to turn off all the lamps, and NO otherwise.

Examples2

  1. Example 1

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

    Input
    5
    1
    2 2
    1
    3 3
    
    Expected output
    NO