結果

問題 No.2667 Constrained Permutation
ユーザー chineristACchineristAC
提出日時 2024-03-08 23:02:33
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,957 bytes
コンパイル時間 187 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 216,596 KB
最終ジャッジ日時 2024-03-08 23:03:08
合計ジャッジ時間 35,257 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
58,348 KB
testcase_01 AC 46 ms
58,348 KB
testcase_02 AC 78 ms
74,384 KB
testcase_03 AC 100 ms
76,932 KB
testcase_04 AC 163 ms
78,896 KB
testcase_05 AC 74 ms
72,984 KB
testcase_06 AC 131 ms
78,760 KB
testcase_07 AC 61 ms
68,568 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 84 ms
76,548 KB
testcase_11 WA -
testcase_12 AC 694 ms
112,496 KB
testcase_13 AC 154 ms
79,028 KB
testcase_14 AC 1,729 ms
185,376 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1,889 ms
207,684 KB
testcase_19 AC 1,772 ms
162,536 KB
testcase_20 AC 1,910 ms
178,688 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 780 ms
131,256 KB
testcase_25 AC 1,505 ms
187,816 KB
testcase_26 AC 1,847 ms
216,596 KB
testcase_27 AC 1,833 ms
210,772 KB
testcase_28 AC 1,048 ms
180,984 KB
testcase_29 AC 957 ms
158,304 KB
testcase_30 AC 1,153 ms
174,920 KB
testcase_31 AC 734 ms
134,992 KB
testcase_32 AC 258 ms
90,544 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 1,023 ms
125,424 KB
testcase_39 AC 170 ms
79,132 KB
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 756 ms
117,244 KB
testcase_43 AC 1,043 ms
129,428 KB
testcase_44 AC 546 ms
98,328 KB
testcase_45 AC 150 ms
78,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from itertools import permutations
from heapq import heappop,heappush
from collections import deque
import random
import bisect

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

class SegmentTree:
    def __init__(self, init_val, segfunc, ide_ele):
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        self.size = n
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = self.segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        k += self.num
        self.tree[k] = x
        while k > 1:
            k >>= 1
            self.tree[k] = self.segfunc(self.tree[2*k], self.tree[2*k+1])

    def query(self, l, r):
        if r==self.size:
            r = self.num

        res = self.ide_ele

        l += self.num
        r += self.num
        right = []
        while l < r:
            if l & 1:
                res = self.segfunc(res, self.tree[l])
                l += 1
            if r & 1:
                right.append(self.tree[r-1])
            l >>= 1
            r >>= 1

        for e in right[::-1]:
            res = self.segfunc(res,e)
        return res
    
def checK_zero(N,_lr):
    lr = _lr[:]
    lr.sort(key=lambda x:x[0],reverse=True)
    nxt_num = 0
    pq = []
    while lr:
        while lr and lr[-1][0] <= nxt_num:
            l,r = lr.pop()
            heappush(pq,r)
        if pq:
            r = heappop(pq)
            if r <= nxt_num:
                print(pq,lr,nxt_num)
                return False
            nxt_num += 1
        else:
            l,r = lr[-1]
            nxt_num = l
    return True
        

"""
結婚定理で考える
区間の和集合が区間になるような組のみ考えればいい
(区間の長さ)< (区間の数) である組があれば答えは0 = 相異なる整数を割り当てられるか?
そうでないなら条件を満たすkの範囲は区間になっているので、端点を考えればいい
"""

    
N = int(input())
lr = []
val_set = set()
for i in range(N):
    l,r = mi()
    val_set.add(l)
    val_set.add(r+1)
    lr.append((l,r+1))

if not checK_zero(N,lr):
    exit(print(0))

val_set = sorted(val_set)
comp = {e:i for i,e in enumerate(val_set)}
lr = [(comp[l],comp[r]) for l,r in lr]
lr.sort(key=lambda x:x[0],reverse=True)
n = len(comp)

seg = SegmentTree([0]*n,max,0)
L,R = -10**9,10**9
for l,r in lr:
    t = seg.query(l,r) + 1
    ll = val_set[l]
    L = max(L,t-N+ll-1)
    seg.update(l,t)

seg = SegmentTree([0]*n,max,0)
lr.sort(key=lambda x:x[1],reverse=False)
for l,r in lr:
    t = seg.query(l,r) + 1
    rr = val_set[r] - 1
    R = min(R,rr-t)
    seg.update(r-1,t)

if L <= R:
    print(R-L+1)
else:
    print(0)
0