結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
56,252 KB
testcase_01 AC 48 ms
56,252 KB
testcase_02 AC 71 ms
72,980 KB
testcase_03 AC 92 ms
76,812 KB
testcase_04 AC 113 ms
78,688 KB
testcase_05 AC 75 ms
72,856 KB
testcase_06 AC 116 ms
78,808 KB
testcase_07 AC 59 ms
68,576 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 73 ms
73,488 KB
testcase_11 WA -
testcase_12 AC 544 ms
112,748 KB
testcase_13 AC 153 ms
78,900 KB
testcase_14 AC 1,382 ms
187,916 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 1,394 ms
190,468 KB
testcase_19 AC 1,164 ms
139,764 KB
testcase_20 AC 1,321 ms
156,220 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 637 ms
124,840 KB
testcase_25 AC 1,136 ms
160,168 KB
testcase_26 AC 1,459 ms
199,332 KB
testcase_27 AC 1,452 ms
200,328 KB
testcase_28 AC 879 ms
171,476 KB
testcase_29 AC 802 ms
168,268 KB
testcase_30 AC 836 ms
166,532 KB
testcase_31 AC 565 ms
138,340 KB
testcase_32 AC 235 ms
90,796 KB
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 AC 729 ms
122,728 KB
testcase_39 AC 144 ms
78,788 KB
testcase_40 AC 532 ms
110,160 KB
testcase_41 AC 828 ms
122,832 KB
testcase_42 AC 654 ms
116,476 KB
testcase_43 AC 930 ms
120,348 KB
testcase_44 AC 414 ms
97,424 KB
testcase_45 AC 150 ms
78,916 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

"""
結婚定理で考える
区間の和集合が区間になるような組のみ考えればいい
(区間の長さ)< (区間の数) である組があれば答えは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))

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)

m = min(val_set)
for i,(l,r) in enumerate(lr):
    if val_set[r]-m < i+1:
        print(0)
        break
else:
    if R<L:
        print(0)
    else:
        print(R-L+1)
0