結果

問題 No.1675 Strange Minimum Query
ユーザー lloyzlloyz
提出日時 2021-11-24 00:22:44
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,699 ms / 2,000 ms
コード長 8,291 bytes
コンパイル時間 176 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 120,704 KB
最終ジャッジ日時 2024-06-25 22:15:12
合計ジャッジ時間 37,098 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
53,632 KB
testcase_01 AC 44 ms
53,632 KB
testcase_02 AC 45 ms
53,632 KB
testcase_03 AC 906 ms
98,908 KB
testcase_04 AC 916 ms
100,000 KB
testcase_05 AC 453 ms
87,076 KB
testcase_06 AC 985 ms
101,536 KB
testcase_07 AC 1,098 ms
105,980 KB
testcase_08 AC 67 ms
66,560 KB
testcase_09 AC 165 ms
77,752 KB
testcase_10 AC 512 ms
102,164 KB
testcase_11 AC 371 ms
94,208 KB
testcase_12 AC 549 ms
103,368 KB
testcase_13 AC 704 ms
115,328 KB
testcase_14 AC 899 ms
119,296 KB
testcase_15 AC 798 ms
118,692 KB
testcase_16 AC 42 ms
53,888 KB
testcase_17 AC 673 ms
87,732 KB
testcase_18 AC 761 ms
90,052 KB
testcase_19 AC 840 ms
101,280 KB
testcase_20 AC 1,264 ms
110,092 KB
testcase_21 AC 1,181 ms
108,860 KB
testcase_22 AC 1,363 ms
112,080 KB
testcase_23 AC 1,164 ms
99,736 KB
testcase_24 AC 1,112 ms
101,460 KB
testcase_25 AC 985 ms
94,732 KB
testcase_26 AC 744 ms
92,780 KB
testcase_27 AC 1,100 ms
107,784 KB
testcase_28 AC 831 ms
99,740 KB
testcase_29 AC 711 ms
98,856 KB
testcase_30 AC 739 ms
92,756 KB
testcase_31 AC 1,404 ms
107,348 KB
testcase_32 AC 1,684 ms
118,788 KB
testcase_33 AC 1,699 ms
119,360 KB
testcase_34 AC 1,697 ms
119,208 KB
testcase_35 AC 1,649 ms
120,704 KB
testcase_36 AC 1,659 ms
119,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class lazy_segtree():
    def update(self,k):self.d[k]=self.op(self.d[2*k],self.d[2*k+1])
    def all_apply(self,k,f):
        self.d[k]=self.mapping(f,self.d[k])
        if (k<self.size):self.lz[k]=self.composition(f,self.lz[k])
    def push(self,k):
        self.all_apply(2*k,self.lz[k])
        self.all_apply(2*k+1,self.lz[k])
        self.lz[k]=self.identity
    def __init__(self,V,OP,E,MAPPING,COMPOSITION,ID):
        self.n=len(V)
        self.log=(self.n-1).bit_length()
        self.size=1<<self.log
        self.d=[E for i in range(2*self.size)]
        self.lz=[ID for i in range(self.size)]
        self.e=E
        self.op=OP
        self.mapping=MAPPING
        self.composition=COMPOSITION
        self.identity=ID
        for i in range(self.n):self.d[self.size+i]=V[i]
        for i in range(self.size-1,0,-1):self.update(i)
    def set(self,p,x):
        assert 0<=p and p<self.n
        p+=self.size
        for i in range(self.log,0,-1):self.push(p>>i)
        self.d[p]=x
        for i in range(1,self.log+1):self.update(p>>i)
    def get(self,p):
        assert 0<=p and p<self.n
        p+=self.size
        for i in range(self.log,0,-1):self.push(p>>i)
        return self.d[p]
    def prod(self,l,r):
        assert 0<=l and l<=r and r<=self.n
        if l==r:return self.e
        l+=self.size
        r+=self.size
        for i in range(self.log,0,-1):
            if (((l>>i)<<i)!=l):self.push(l>>i)
            if (((r>>i)<<i)!=r):self.push(r>>i)
        sml,smr=self.e,self.e
        while(l<r):
            if l&1:
                sml=self.op(sml,self.d[l])
                l+=1
            if r&1:
                r-=1
                smr=self.op(self.d[r],smr)
            l>>=1
            r>>=1
        return self.op(sml,smr)
    def all_prod(self):return self.d[1]
    def apply_point(self,p,f):
        assert 0<=p and p<self.n
        p+=self.size
        for i in range(self.log,0,-1):self.push(p>>i)
        self.d[p]=self.mapping(f,self.d[p])
        for i in range(1,self.log+1):self.update(p>>i)
    def apply(self,l,r,f):
        assert 0<=l and l<=r and r<=self.n
        if l==r:return
        l+=self.size
        r+=self.size
        for i in range(self.log,0,-1):
            if (((l>>i)<<i)!=l):self.push(l>>i)
            if (((r>>i)<<i)!=r):self.push((r-1)>>i)
        l2,r2=l,r
        while(l<r):
            if (l&1):
                self.all_apply(l,f)
                l+=1
            if (r&1):
                r-=1
                self.all_apply(r,f)
            l>>=1
            r>>=1
        l,r=l2,r2
        for i in range(1,self.log+1):
            if (((l>>i)<<i)!=l):self.update(l>>i)
            if (((r>>i)<<i)!=r):self.update((r-1)>>i)
    def max_right(self,l,g):
        assert 0<=l and l<=self.n
        assert g(self.e)
        if l==self.n:return self.n
        l+=self.size
        for i in range(self.log,0,-1):self.push(l>>i)
        sm=self.e
        while(1):
            while(i%2==0):l>>=1
            if not(g(self.op(sm,self.d[l]))):
                while(l<self.size):
                    self.push(l)
                    l=(2*l)
                    if (g(self.op(sm,self.d[l]))):
                        sm=self.op(sm,self.d[l])
                        l+=1
                return l-self.size
            sm=self.op(sm,self.d[l])
            l+=1
            if (l&-l)==l:break
        return self.n
    def min_left(self,r,g):
        assert (0<=r and r<=self.n)
        assert g(self.e)
        if r==0:return 0
        r+=self.size
        for i in range(self.log,0,-1):self.push((r-1)>>i)
        sm=self.e
        while(1):
            r-=1
            while(r>1 and (r%2)):r>>=1
            if not(g(self.op(self.d[r],sm))):
                while(r<self.size):
                    self.push(r)
                    r=(2*r+1)
                    if g(self.op(self.d[r],sm)):
                        sm=self.op(self.d[r],sm)
                        r-=1
                return r+1-self.size
            sm=self.op(self.d[r],sm)
            if (r&-r)==r:break
        return 0

class SegmentTree:

    __all__ = ['setval', 'pointupdate', 'segquery', 'segsearch_right', 'pointgetval']

    def __init__(self, n=10**6, idetify_elt=-10**9, func=max):
        assert (func(idetify_elt, idetify_elt) == idetify_elt)
        self._n = n
        self._seg_length_half = 2**(n-1).bit_length()
        self._idetify_elt = idetify_elt
        self._seg = [idetify_elt]*(2*self._seg_length_half)
        self._func = func

    def setval(self, x_list):
        '''Set value : A = x_list'''
        assert (len(x_list) == self._n)
        # Set value at the bottom
        for i in range(self._n):
            self._seg[i+self._seg_length_half-1] = x_list[i]    
        # Build value
        for i in range(self._seg_length_half-2, -1, -1):
            self._seg[i] = self._func(self._seg[2*i+1], self._seg[2*i+2])
    
    def pointupdate(self, k, x):
        '''Update : A[k] = x '''
        pos = k + self._seg_length_half - 1
        # Set value at k-th
        self._seg[pos] = x
        # Build bottom-up
        while pos:
            pos = (pos-1)//2
            self._seg[pos] = self._func(self._seg[pos*2+1], self._seg[pos*2+2])
    
    def pointgetval(self, k):
        ''' Return A[k] '''
        return self._seg[k + self._seg_length_half - 1]

    def segquery(self, left, right):
        ''' Return func(A[left], ... , A[right-1]) '''
        # if not left < right
        if right <= left:
            return self._idetify_elt
        
        func_value = self._idetify_elt
        leftpos = left + self._seg_length_half - 1 # leftmost segment
        rightpos = right + self._seg_length_half - 2 # rightmost segment

        while leftpos < rightpos-1:
            if leftpos&1 == 0:
                # if leftpos is right-child
                func_value = self._func(func_value, self._seg[leftpos])
            if rightpos&1 == 1:
                # if rightpos is leftchild
                func_value = self._func(func_value, self._seg[rightpos])
                rightpos -= 1
            # move up
            leftpos = leftpos//2
            rightpos = (rightpos-1)//2
        
        func_value = self._func(func_value, self._seg[leftpos])
        if leftpos != rightpos:
            func_value = self._func(func_value, self._seg[rightpos])
        return func_value

    def segsearch_right(self, condfunc, left=0):
        ''' Return min_i satisfying condfunc( func( A[left], ... , A[i])) 
        if impossible : return n
        '''
        # if impossible (ie. condfunc( func( A[left], ... , A[-1])) is False)
        if not condfunc(self._segquery(left, self._n)):
            return self._n
        
        # possible
        func_value = self._idetify_elt
        rightpos = left + self._seg_length_half - 1
        while True: 
            # while rightpos is the left-child, move bottom-up
            while rightpos&1 == 1:
                rightpos //= 2
            # try
            up_value_trial = self._func(func_value, self._seg[rightpos])
            if not condfunc(up_value_trial):
                # move up and right
                func_value = up_value_trial
                rightpos = (rightpos-1)//2 + 1
            else:
                # move top-down
                while rightpos < self._seg_length_half-1:
                    down_value_trial = self._func(func_value, self._seg[rightpos*2 + 1])
                    if condfunc(down_value_trial):
                        # move left-child
                        rightpos = rightpos*2 + 1
                    else:
                        # move right-child
                        func_value = down_value_trial
                        rightpos = rightpos*2 + 2
                return rightpos - self._seg_length_half + 1

n, q = map(int, input().split())
INF = 10**9
lazysegtree = lazy_segtree([1 for _ in range(n)], max, 1, max, max, 0)

Q = [list(map(int, input().split())) for _ in range(q)]
for l, r, b in Q:
    lazysegtree.apply(l - 1, r, b)
ans = [lazysegtree.get(i) for i in range(n)]

segtree = SegmentTree(n=n, idetify_elt=INF, func=min)
for i in range(n):
    segtree.pointupdate(i, ans[i])
for l, r, b in Q:
    if segtree.segquery(l - 1, r) != b:
        print(-1)
        exit()
print(*ans)
0