Inverse RMQ
Time limit2sMemory limit512 MB
Given query intervals and their maximum answers over a hidden permutation of 1..N, decide whether some permutation of 1..N satisfies all queries.
Problem
The range maximum query (RMQ) problem is stated as follows.
A permutation of the integers through is given. A query has the form with , and it asks for the largest value among the -th through -th entries of .
For , the answer to query is , the answer to query is , and the answer to query is .
This problem runs RMQ backwards. You are given an integer , the number of queries , and for each query the pair together with its answer . Write a program that decides whether some permutation satisfies every given query.
Input
The first line contains and the number of queries . (, )
Each of the next lines contains , , and the answer . (, )
Output
Print 1 if a permutation satisfying every given query exists, and 0 otherwise.