Disco
Time limit1sMemory limit1024 MB
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 lamps, numbered from to .
Turning the lamps on and off one by one is tedious, so Juss also installed switches, numbered from to . Pressing switch flips the state of every lamp numbered from to (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 groups. Group contains every lamp numbered from to , and for every we have (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:
- , and for every
Input
The first line contains the integer . The second line contains the integer . Each of the next lines contains two integers and . The next line contains the integer . Each of the last lines contains two integers and .
Output
Print YES if it is possible to turn off all the lamps, and NO otherwise.