;; -*- coding: utf-8 -*- (eval-when (:compile-toplevel :load-toplevel :execute) (sb-int:defconstant-eqx OPT #+swank '(optimize (speed 3) (safety 2)) #-swank '(optimize (speed 3) (safety 0) (debug 0)) #'equal) #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t) #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil nil t)))) #+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax) #-swank (disable-debugger) ; for CS Academy ;; BEGIN_INSERTED_CONTENTS (defmacro define-int-types (&rest bits) `(progn ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits) ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits))) (define-int-types 2 4 7 8 15 16 31 32 62 63 64) ;;; ;;; Disjoint set by Union-Find algorithm ;;; (defstruct (disjoint-set (:constructor make-disjoint-set (size &aux (data (make-array size :element-type 'int32 :initial-element -1)))) (:conc-name ds-)) (data nil :type (simple-array int32 (*)))) (declaim (ftype (function * (values (mod #.array-total-size-limit) &optional)) ds-root)) (defun ds-root (x disjoint-set) "Returns the root of X." (declare #.OPT ((mod #.array-total-size-limit) x)) (let ((data (ds-data disjoint-set))) (if (< (aref data x) 0) x (setf (aref data x) (ds-root (aref data x) disjoint-set))))) (declaim (inline ds-unite!)) (defun ds-unite! (x1 x2 disjoint-set) "Destructively unites X1 and X2 and returns true iff X1 and X2 become connected for the first time." (let ((root1 (ds-root x1 disjoint-set)) (root2 (ds-root x2 disjoint-set))) (unless (= root1 root2) (let ((data (ds-data disjoint-set))) ;; ensure the size of root1 >= the size of root2 (when (> (aref data root1) (aref data root2)) (rotatef root1 root2)) (incf (aref data root1) (aref data root2)) (setf (aref data root2) root1))))) (declaim (inline ds-connected-p)) (defun ds-connected-p (x1 x2 disjoint-set) "Returns true iff X1 and X2 have the same root." (= (ds-root x1 disjoint-set) (ds-root x2 disjoint-set))) (declaim (ftype (function * (values fixnum &optional)) read-fixnum)) (defun read-fixnum (&optional (in *standard-input*)) (declare #.OPT) (macrolet ((%read-byte () `(the (unsigned-byte 8) #+swank (char-code (read-char in nil #\Nul)) #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil)))) (let* ((minus nil) (result (loop (let ((byte (%read-byte))) (cond ((<= 48 byte 57) (return (- byte 48))) ((zerop byte) ; #\Nul (error "Read EOF or #\Nul.")) ((= byte #.(char-code #\-)) (setf minus t))))))) (declare ((integer 0 #.most-positive-fixnum) result)) (loop (let* ((byte (%read-byte))) (if (<= 48 byte 57) (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result)))) (return (if minus (- result) result)))))))) ;;; ;;; Strongly connected components of directed graph ;;; (defstruct (scc (:constructor %make-scc (graph revgraph posts components sizes count))) (graph nil :type (simple-array list (*))) ;; reversed graph (revgraph nil :type (simple-array list (*))) ;; vertices by post-order DFS posts ;; components[i] := strongly connected component of the i-th vertex (components nil :type (simple-array (unsigned-byte 32) (*))) ;; sizes[k] := size of the k-th strongly connected component (sizes nil :type (simple-array (unsigned-byte 32) (*))) ;; the total number of strongly connected components (count 0 :type (unsigned-byte 32))) (declaim (inline %make-revgraph)) (defun %make-revgraph (graph) (let* ((n (length graph)) (revgraph (make-array n :element-type 'list :initial-element nil))) (dotimes (i n) (dolist (dest (aref graph i)) (push i (aref revgraph dest)))) revgraph)) (defun make-scc (graph &optional revgraph) "GRAPH := vector of adjacency lists REVGRAPH := NIL | reversed graph of GRAPH" (declare #.OPT ((simple-array list (*)) graph) ((or null (simple-array list (*))) revgraph)) (let* ((revgraph (or revgraph (%make-revgraph graph))) (n (length graph)) (visited (make-array n :element-type 'bit :initial-element 0)) (posts (make-array n :element-type '(unsigned-byte 32))) (components (make-array n :element-type '(unsigned-byte 32))) (sizes (make-array n :element-type '(unsigned-byte 32) :initial-element 0)) (pointer 0) (ord 0) ; ordinal number for a strongly connected component ) (declare ((unsigned-byte 32) pointer ord)) (assert (= n (length revgraph))) (labels ((dfs (v) (setf (aref visited v) 1) (dolist (neighbor (aref graph v)) (when (zerop (aref visited neighbor)) (dfs neighbor))) (setf (aref posts pointer) v) (incf pointer)) (reversed-dfs (v ord) (setf (aref visited v) 1 (aref components v) ord) (incf (aref sizes ord)) (dolist (neighbor (aref revgraph v)) (when (zerop (aref visited neighbor)) (reversed-dfs neighbor ord))))) (dotimes (v n) (when (zerop (aref visited v)) (dfs v))) (fill visited 0) (loop for i from (- n 1) downto 0 for v = (aref posts i) when (zerop (aref visited v)) do (reversed-dfs v ord) (incf ord)) (%make-scc graph revgraph posts components sizes ord)))) (defmacro dbg (&rest forms) #+swank (if (= (length forms) 1) `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms)) `(format *error-output* "~A => ~A~%" ',forms `(,,@forms))) #-swank (declare (ignore forms))) (declaim (inline println)) (defun println (obj &optional (stream *standard-output*)) (let ((*read-default-float-format* 'double-float)) (prog1 (princ obj stream) (terpri stream)))) (defconstant +mod+ 1000000007) ;;; ;;; Body ;;; (defun main () (declare #.OPT) (let* ((n (read)) (m (read)) (dset (make-disjoint-set (+ n n m))) (graph (make-array (* 4 n) :element-type 'list :initial-element nil))) (declare (uint16 n m)) (dotimes (i n) (let* ((l (read-fixnum)) (r (read-fixnum))) (loop for j from l to r do (ds-unite! i (+ j (* 2 n)) dset)) (loop for j from (- m r 1) to (- m l 1) do (ds-unite! (+ i n) (+ j (* 2 n)) dset)))) (labels ((negate (x) (declare (uint16 x)) (mod (+ x (* 2 n)) (* 4 n))) (add-clause! (literal1 literal2 bool1 bool2) (unless bool1 (setq literal1 (negate literal1))) (unless bool2 (setq literal2 (negate literal2))) (push literal2 (aref graph (negate literal1))) (push literal1 (aref graph (negate literal2))))) (gc) (dotimes (x (* 2 n)) (loop for y from (+ x 1) below (* 2 n) do (when (and (ds-connected-p x y dset) (/= (+ x n) y)) (add-clause! x y nil nil)))) (dotimes (x n) (add-clause! x (+ x n) t t) (add-clause! x (+ x n) nil nil)) (let* ((scc (make-scc graph)) (comps (scc-components scc))) (write-line (if (loop for x below (* 2 n) thereis (= (aref comps x) (aref comps (+ x (* 2 n))))) "NO" "YES")))))) #-swank (main)