結果

問題 No.274 The Wall
ユーザー ygd.ygd.
提出日時 2022-01-13 19:58:13
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 4,778 bytes
コンパイル時間 264 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 14,080 KB
最終ジャッジ日時 2024-11-17 11:53:23
合計ジャッジ時間 2,161 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 33 ms
11,136 KB
testcase_02 AC 33 ms
11,008 KB
testcase_03 AC 46 ms
12,160 KB
testcase_04 AC 30 ms
11,008 KB
testcase_05 AC 30 ms
11,136 KB
testcase_06 AC 31 ms
11,136 KB
testcase_07 AC 31 ms
11,008 KB
testcase_08 AC 31 ms
11,136 KB
testcase_09 AC 30 ms
11,136 KB
testcase_10 AC 30 ms
11,008 KB
testcase_11 AC 61 ms
14,080 KB
testcase_12 AC 42 ms
11,520 KB
testcase_13 AC 31 ms
11,008 KB
testcase_14 AC 35 ms
11,136 KB
testcase_15 AC 38 ms
11,264 KB
testcase_16 AC 45 ms
12,032 KB
testcase_17 AC 45 ms
11,904 KB
testcase_18 AC 46 ms
11,904 KB
testcase_19 AC 41 ms
11,520 KB
testcase_20 AC 43 ms
11,520 KB
testcase_21 AC 42 ms
11,520 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 42 ms
11,520 KB
testcase_25 AC 42 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
#input = sys.stdin.readline #文字列につけてはダメ
#input = sys.stdin.buffer.readline #文字列につけてはダメ
#sys.setrecursionlimit(1000000)
#import bisect
#import itertools
#import random
#from heapq import heapify, heappop, heappush
#from collections import defaultdict 
#from collections import deque
#import copy
#import math
#from functools import lru_cache
#MOD = pow(10,9) + 7
#MOD = 998244353

#https://atcoder.jp/contests/practice2/submissions/17674275
class scc_graph:
 
    def __init__(self, N):
        self.N = N
        self.edges = []
 
    def csr(self):
        start = [0]*(self.N+1)
        elist = [0]*len(self.edges)
        for v, w in self.edges:
            start[v+1] += 1
        for i in range(1, self.N+1):
            start[i] += start[i-1]
        counter = start.copy()
        for v, w in self.edges:
            elist[counter[v]] = w
            counter[v] += 1
        self.start = start
        self.elist = elist
 
    def add_edge(self, v, w):
        self.edges.append((v, w))
 
    def scc_ids(self):
        self.csr()
        N = self.N
        now_ord = group_num = 0
        visited = []
        low = [0]*N
        order = [-1]*N
        ids = [0]*N
        parent = [-1]*N
        stack = []
        for i in range(N):
            if order[i] == -1:
                stack.append(~i)
                stack.append(i)
                while stack:
                    v = stack.pop()
                    if v >= 0:
                        if order[v] == -1:
                            low[v] = order[v] = now_ord
                            now_ord += 1
                            visited.append(v)
                            for i in range(self.start[v], self.start[v+1]):
                                to = self.elist[i]
                                if order[to] == -1:
                                    stack.append(~to)
                                    stack.append(to)
                                    parent[to] = v
                                else:
                                    low[v] = min(low[v], order[to])
                    else:
                        v = ~v
                        if low[v] == order[v]:
                            while True:
                                u = visited.pop()
                                order[u] = N
                                ids[u] = group_num
                                if u == v:
                                    break
                            group_num += 1
                        if parent[v] != -1:
                            low[parent[v]] = min(low[parent[v]], low[v])
        for i, x in enumerate(ids):
            ids[i] = group_num-1-x
 
        return group_num, ids
 
    def scc(self):
        group_num, ids = self.scc_ids()
        groups = [[] for _ in range(group_num)]
        for i, x in enumerate(ids):
            groups[x].append(i)
        return groups
 
 
class two_sat:
 
    def __init__(self, N):
        self.N = N
        self.answer = []
        self.scc = scc_graph(2*N)
 
    def add_clause(self, i, f, j, g): #f,g = 0/1 (True/False)
        # assert 0 <= i < self.N
        # assert 0 <= j < self.N
        self.scc.add_edge(2*i+(f == 0), 2*j+(g == 1))
        self.scc.add_edge(2*j+(g == 0), 2*i+(f == 1))
 
    def satisfiable(self): #これがTrueならts.answerで満たす場合の組み合わせを出力可能
        _, ids = self.scc.scc_ids()
        self.answer.clear()
        for i in range(self.N):
            if ids[2 * i] == ids[2 * i + 1]:
                self.answer.clear()
                return False
            self.answer.append(ids[2*i] < ids[2*i+1])
        return True
 
def overlap(a,b,c,d): #Trueは被る
    if b < c: return False
    if d < a: return False
    return True


def main():
    N,M = map(int,input().split())
    L = []; R = []
    for i in range(N):
        l,r = map(int,input().split())
        L.append(l)
        R.append(r)

    ts = two_sat(N)
    for i in range(N-1):
        li0, ri0 = L[i], R[i]
        lj0, rj0 = L[i+1], R[i+1]
        li1, ri1 = M+1 - ri0, M+1 - li0
        lj1, rj1 = M+1 - rj0, M+1 - lj0
        #print(li0, ri0, li1, ri1)
        if overlap(li0, ri0, lj0, rj0): #0,0が被るのでどちらか
            ts.add_clause(i, 0, i+1, 0)
        if overlap(li0, ri0, lj1, rj1): #0,1が被るのでどちらか
            ts.add_clause(i, 0, i+1, 1)
        if overlap(li1, ri1, lj0, rj0): #1,0が被るのでどちらか
            ts.add_clause(i, 1, i+1, 0)
        if overlap(li1, ri1, lj1, rj1): #1,1が被るのでどちらか
            ts.add_clause(i, 1, i+1, 1)
    
    if ts.satisfiable():
        print('YES')
    else:
        print('NO')


if __name__ == '__main__':
    main()
0