結果

問題 No.1675 Strange Minimum Query
ユーザー 👑 KazunKazun
提出日時 2021-09-10 21:48:07
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,589 ms / 2,000 ms
コード長 9,969 bytes
コンパイル時間 1,279 ms
コンパイル使用メモリ 86,608 KB
実行使用メモリ 135,244 KB
最終ジャッジ日時 2023-09-02 16:50:32
合計ジャッジ時間 35,156 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
71,292 KB
testcase_01 AC 81 ms
71,432 KB
testcase_02 AC 81 ms
71,392 KB
testcase_03 AC 957 ms
105,492 KB
testcase_04 AC 880 ms
111,160 KB
testcase_05 AC 354 ms
94,336 KB
testcase_06 AC 1,041 ms
110,556 KB
testcase_07 AC 1,177 ms
120,036 KB
testcase_08 AC 90 ms
75,844 KB
testcase_09 AC 145 ms
78,444 KB
testcase_10 AC 516 ms
114,208 KB
testcase_11 AC 338 ms
104,980 KB
testcase_12 AC 563 ms
114,572 KB
testcase_13 AC 562 ms
127,516 KB
testcase_14 AC 740 ms
131,480 KB
testcase_15 AC 669 ms
131,116 KB
testcase_16 AC 79 ms
71,260 KB
testcase_17 AC 506 ms
90,808 KB
testcase_18 AC 598 ms
92,680 KB
testcase_19 AC 678 ms
110,508 KB
testcase_20 AC 1,143 ms
121,540 KB
testcase_21 AC 1,036 ms
120,440 KB
testcase_22 AC 1,267 ms
125,912 KB
testcase_23 AC 1,092 ms
105,524 KB
testcase_24 AC 992 ms
108,204 KB
testcase_25 AC 820 ms
97,276 KB
testcase_26 AC 576 ms
96,088 KB
testcase_27 AC 961 ms
117,024 KB
testcase_28 AC 660 ms
110,460 KB
testcase_29 AC 541 ms
107,916 KB
testcase_30 AC 552 ms
97,420 KB
testcase_31 AC 1,300 ms
116,884 KB
testcase_32 AC 1,553 ms
132,292 KB
testcase_33 AC 1,589 ms
132,924 KB
testcase_34 AC 1,585 ms
133,232 KB
testcase_35 AC 1,524 ms
134,820 KB
testcase_36 AC 1,523 ms
135,244 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

        calc:演算
        unit:モノイドcalcの単位元 (xe=ex=xを満たすe)
        op:作用素
        comp:作用素の合成
        id:恒等写像

        [条件] M:Monoid,F={f:F x M→ M:作用素}に対して,以下が成立する.
        Fは恒等写像 id を含む.つまり,任意の x in M に対して id(x)=x
        Fは写像の合成に閉じている.つまり,任意の f,g in F に対して, comp(f,g) in F
        任意の f in F, x,y in M に対して,f(xy)=f(x)f(y)である.

        [注記]
        作用素は左から掛ける.更新も左から.
        """

        self.calc=calc
        self.unit=unit
        self.op=op
        self.comp=comp
        self.id=id
        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.lazy=[self.id]*(2*k)
        self.N=k
        self.depth=d

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

    def _eval_at(self,m):
        if self.lazy[m]==self.id:
            return self.data[m]
        return self.op(self.lazy[m],self.data[m])

    #配列の第m要素を下に伝搬
    def _propagate_at(self,m):
        self.data[m]=self._eval_at(m)

        if m<self.N and self.lazy[m]!=self.id:
            self.lazy[m<<1]=self.comp(
                self.lazy[m],
                self.lazy[m<<1]
                )

            self.lazy[m<<1|1]=self.comp(
                self.lazy[m],
                self.lazy[m<<1|1]
                )

        self.lazy[m]=self.id

    #配列の第m要素より上を全て伝搬
    def _propagate_above(self,m):
        H=m.bit_length()
        for h in range(H-1,0,-1):
            self._propagate_at(m>>h)

    #配列の第m要素より上を全て再計算
    def _recalc_above(self,m):
        while m>1:
            m>>=1
            self.data[m]=self.calc(
                self._eval_at(m<<1),
                self._eval_at(m<<1|1)
            )

    def get(self,k):
        index=self.index
        m=k-index+self.N
        self._propagate_above(m)
        self.data[m]=self._eval_at(m)
        self.lazy[m]=self.id
        return self.data[m]

    #作用
    def operate(self,From,To,alpha,left_closed=True,right_closed=True):
        index=self.index
        L=(From-index)+self.N+(not left_closed)
        R=(To-index)+self.N+(right_closed)

        L0=R0=-1
        X,Y=L,R-1
        while X<Y:
            if X&1:
                L0=max(L0,X)
                X+=1

            if Y&1==0:
                R0=max(R0,Y)
                Y-=1

            X>>=1
            Y>>=1

        L0=max(L0,X)
        R0=max(R0,Y)

        self._propagate_above(L0)
        self._propagate_above(R0)

        while L<R:
            if L&1:
                self.lazy[L]=self.comp(alpha,self.lazy[L])
                L+=1

            if R&1:
                R-=1
                self.lazy[R]=self.comp(alpha,self.lazy[R])

            L>>=1
            R>>=1

        self._recalc_above(L0)
        self._recalc_above(R0)

    def update(self,k,x):
        """ 第k要素をxに変更する.
        """
        index=self.index
        m=k-index+self.N
        self._propagate_above(m)
        self.data[m]=x
        self.lazy[m]=self.id
        self._recalc_above(m)

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

        L0=R0=-1
        X,Y=L,R-1
        while X<Y:
            if X&1:
                L0=max(L0,X)
                X+=1

            if Y&1==0:
                R0=max(R0,Y)
                Y-=1

            X>>=1
            Y>>=1

        L0=max(L0,X)
        R0=max(R0,Y)

        self._propagate_above(L0)
        self._propagate_above(R0)

        vL=vR=self.unit

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

            if R&1:
                R-=1
                vR=self.calc(self._eval_at(R),vR)

            L>>=1
            R>>=1

        return self.calc(vL,vR)

    def all_product(self):
        return self.product(1,self.N,1)

    #リフレッシュ
    def refresh(self):
        for m in range(1,2*self.N):
            self.data[m]=self._eval_at(m)

            if m<self.N and self.lazy[m]!=self.id:
                self.lazy[m<<1]=self.comp(
                    self.lazy[m],
                    self.lazy[m<<1]
                    )

                self.lazy[m<<1|1]=self.comp(
                    self.lazy[m],
                    self.lazy[m<<1|1]
                    )

            self.lazy[m]=self.id

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

    def __setitem__(self,k,x):
        self.update(k,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
from heapq import heapify,heappop,heappush
input=sys.stdin.readline
N,Q=map(int,input().split())
P=[]

for _ in range(Q):
    l,r,b=map(int,input().split())
    P.append((l-1,r-1,b))

S=Lazy_Evaluation_Tree([1]*N,max,1,max,max,-1,0)

for l,r,b in P:
    S.operate(l,r,b)

S.refresh()
X=[S[i] for i in range(N)]

T=Segment_Tree(X,min,10**9,0)

for l,r,b in P:
    if T.product(l+1,r+1)!=b:
        exit(print(-1))

print(*X)
0