(eval-when (:compile-toplevel :load-toplevel :execute)
#+swank '(optimize (speed 3) (safety 2))
#-swank '(optimize (speed 3) (safety 0) (debug 0)))
#+swank (ql:quickload '(:cl-debug-print :fiveam :cp/util) :silent t)
#+swank (use-package :cp/util :cl-user)
#-swank (set-dispatch-macro-character
#\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t))))
#+sbcl (setq *random-state* (seed-random-state (nth-value 1 (get-time-of-day)))))
#+swank (set-dispatch-macro-character #\# #\> #'cl-debug-print:debug-print-reader)
`(progn (deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))
(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))))
(define-int-types (&rest bits) `(progn ,@(mapcar (lambda (b) `(def ,b)) bits))))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64))
(defconstant +mod+ 1000000007)
(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*
(if (typep obj 'double-float) 'double-float *read-default-float-format*)))
(prog1 (princ obj stream) (terpri stream))))
(defpackage :cp/read-fixnum
(in-package :cp/read-fixnum)
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
"NOTE: cannot read -2^62"
(macrolet ((%read-byte ()
#+swank (char-code (read-char in nil #\Nul))
#-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
(result (loop (let ((byte (%read-byte)))
(error "Read EOF or #\Nul."))
((= byte #.(char-code #\-))
(declare ((integer 0 #.most-positive-fixnum) result))
(let* ((byte (%read-byte)))
(setq result (+ (- byte 48)
(* 10 (the (integer 0 #.(floor most-positive-fixnum 10))
(return (if minus (- result) result))))))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(use-package :cp/read-fixnum :cl-user))
(points (make-array n :element-type '(cons int32 int32)))
(setf (aref points i) (cons (read-fixnum) (read-fixnum))))
(setq points (sort points #'< :key #'cdr))
(loop for p across points
(destructuring-bind (prev-r . prev-c) (car stack)
(cond ((<= (+ c r) (+ prev-c prev-r))
((<= (- c r) (- prev-c prev-r))
(loop for (r . c) in stack
(destructuring-bind (r . c) (car stack)
(destructuring-bind ((r . c)) (last stack)
(loop for (p1 p2) on (reverse stack)
for mid-r = (max 0 (ceiling (+ (+ r1 r2) (- c1 c2)) 2))
do (incf res (* mid-r (+ 1 (- c2 c1))))
(incf res (calc (- r1 mid-r)))
(incf res (calc (- r2 mid-r))))
(defparameter *lisp-file-pathname* (uiop:current-lisp-file-pathname))
(setq *default-pathname-defaults* (uiop:pathname-directory-pathname *lisp-file-pathname*))
(uiop:chdir *default-pathname-defaults*)
(defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *lisp-file-pathname*))
(defparameter *problem-url* "https://csacademy.com/contest/virtual36718/task/pyramids/"))
(uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
(defun bench (&optional (out (make-broadcast-stream)))
(time (run *dat-pathname* out)))
(eval-when (:compile-toplevel)
(when (or (> sb-c::*compiler-warning-count* 0)
sb-c::*undefined-warnings*)
(error "count: ~D, undefined warnings: ~A"
sb-c::*compiler-warning-count*
sb-c::*undefined-warnings*)))