結果

問題 No.1675 Strange Minimum Query
ユーザー 👑 KazunKazun
提出日時 2021-09-11 00:58:55
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 6,498 bytes
コンパイル時間 300 ms
コンパイル使用メモリ 86,644 KB
実行使用メモリ 175,360 KB
最終ジャッジ日時 2023-09-03 15:52:50
合計ジャッジ時間 20,928 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

class Heap_Dict:
    def __init__(self,Mode=True):
        """

        Mode:True→最小値,False→最大値
        """
        self.heap=[]
        self.dict={}
        self.Mode=Mode

    def __bool__(self):
        return bool(self.heap)

    def insert(self,x):
        if self.Mode and not self.is_exist(x):
            heapq.heappush(self.heap,x)
        elif not self.Mode and not self.is_exist(x):
            heapq.heappush(self.heap,-x)

        if x in self.dict:
            self.dict[x]+=1
        else:
            self.dict[x]=1

    def erase(self,x):
        assert (x in self.dict) and (self.dict[x])

        self.dict[x]-=1

        while self.heap:
            y=self.heap[0]
            if not self.Mode:y=-y
            if self.dict[y]==0:
                heapq.heappop(self.heap)
            else:
                break

    def is_exist(self,x):
        return (x in self.dict) and (self.dict[x])

    def get_min(self):
        assert self.Mode

        if self.heap:
            return self.heap[0]
        else:
            return float("inf")

    def get_max(self):
        assert not self.Mode

        if self.heap:
            return -self.heap[0]
        else:
            return -float("inf")

    def count(self,x):
        if x not in self.dict:
            return 0
        else:
            return self.dict[x]

class Segment_Tree():
    """
    このプログラム内は1-index
    """

    def __init__(self,L,calc,unit,index):
        """calcを演算とするリストLのSegment Treeを作成

        calc:演算(2変数関数,モノイド)
        unit:モノイドcalcの単位元 (xe=ex=xを満たすe)
        index:数列の第1要素のindex
        """
        self.calc=calc
        self.unit=unit
        self.index=index

        N=len(L)
        d=max(1,(N-1).bit_length())
        k=1<<d

        self.data=[unit]*k+L+[unit]*(k-len(L))
        self.N=k
        self.depth=d

        for i in range(k-1,0,-1):
            self.data[i]=self.calc(self.data[i<<1],self.data[i<<1|1])

    def get(self,k,index=1):
        """第k要素を取得
        """
        assert 0<=k-index<self.N,"添字が範囲外"
        return self.data[k-index+self.N]

    def update(self,k,x,index=1):
        """第k要素をxに変え,更新を行う.

        k:数列の要素
        x:更新後の値
        """
        assert 0<=k-index<self.N,"添字が範囲外"
        m=(k-index)+self.N
        self.data[m]=x

        while m>1:
            m>>=1
            self.data[m]=self.calc(self.data[m<<1],self.data[m<<1|1])

    def product(self,From,To,index=1,left_closed=True,right_closed=True):
        L=(From-index)+self.N+(not left_closed)
        R=(To-index)+self.N+(right_closed)

        vL=self.unit
        vR=self.unit

        while L<R:
            if L&1:
                vL=self.calc(vL,self.data[L])
                L+=1

            if R&1:
                R-=1
                vR=self.calc(self.data[R],vR)

            L>>=1
            R>>=1

        return self.calc(vL,vR)

    def all_product(self):
        return self.data[1]

    def max_right(self,left,cond,index=1):
        """以下の2つをともに満たすxの1つを返す.\n
        (1) x=left or cond(data[left]*data[left+1]*...*data[x-1]):True
        (2) x=N+index or cond(data[left]*data[left+1]*...*data[x]):False
        ※condが単調減少の時,cond(data[left]*...*data[x-1])を満たす最大のxとなる.

        cond:関数(引数が同じならば結果も同じ)
        cond(unit):True
        index<=left<=r<n+index
        """
        left-=index

        assert 0<=left<=self.N,"添字が範囲外"
        assert cond(self.unit),"単位元が条件を満たさない."

        if left==self.N:
            return self.N+index

        left+=self.N-(index-1)
        sm=self.unit

        calc=self.calc
        first=True
        while first or (left & (-left))!=left:
            first=False
            while left%2==0:
                left>>=1
            if not cond(calc(sm,self.data[left])):
                while left<self.N:
                    left<<=1
                    if cond(self.calc(sm,self.data[left])):
                        sm=self.calc(sm,self.data[left])
                        left+=1
                return left-self.N+index
            sm=self.calc(sm,self.data[left])
            left+=1
        return self.N+index

    def min_left(self,right,cond,index=1):
        """以下の2つをともに満たすyの1つを返す.\n
        (1) y=right or cond(data[y]*data[y+1]*...*data[right]):True
        (2) y=index or cond(data[y-1]*data[y]*...*data[right]):False
        ※condが単調減少の時,cond(data[y]*...*data[right-1])を満たす最大のyとなる.

        cond:関数(引数が同じならば結果も同じ)
        cond(unit):True
        index<=left<=r<n+index
        """
        right-=index

        assert 0<=right<=self.N,"添字が範囲外"
        assert cond(self.unit),"単位元が条件を満たさない."

        if right==0:
            return index

        right+=self.N
        sm=self.unit

        calc=self.calc
        first=1
        while first or (right & (-right))!=right:
            first=0
            right-=1
            while right>1 and right&1:
                right>>=1

            if not cond(calc(self.data[right],sm)):
                while right<self.N:
                    right=2*right+1
                    if cond(calc(self.data[right],sm)):
                        sm=calc(self.data[right],sm)
                        right-=1
                return right+1-self.N+index
            sm=calc(self.data[right],sm)
        return index

    def __getitem__(self,k):
        return self.get(k,self.index)

    def __setitem__(self,k,x):
        return self.update(k,x,self.index)
#==================================================
import sys
input=sys.stdin.readline
write=sys.stdout.write

N,Q=map(int,input().split())
X=[[] for _ in range(N+2)]
M=[]
for _ in range(Q):
    L,R,B=map(int,input().split())
    M.append((L,R,B))
    X[L].append(B)
    X[R+1].append(-B)

H=Heap_Dict(False)
H.insert(1)

A=[1]*(N+2)
for i in range(1,N+1):
    for x in X[i]:
        if x>0:
            H.insert(x)
        else:
            H.erase(-x)
    A[i]=H.get_max()

S=Segment_Tree(A,min,10**9,0)
for i in range(N+1):
    print(i,S[i],S.product(i,i,0))

for L,R,B in M:
    if S.product(L,R,0)!=B:
        exit(print(-1))

print(*A[1:N+1])
0