結果

問題 No.230 Splarraay スプラレェーイ
ユーザー rpy3cpprpy3cpp
提出日時 2015-06-21 12:56:35
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 3,668 bytes
コンパイル時間 291 ms
コンパイル使用メモリ 87,084 KB
実行使用メモリ 79,288 KB
最終ジャッジ日時 2023-09-21 22:33:15
合計ジャッジ時間 8,765 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
71,404 KB
testcase_01 AC 71 ms
71,480 KB
testcase_02 AC 69 ms
71,140 KB
testcase_03 AC 73 ms
71,420 KB
testcase_04 AC 69 ms
71,320 KB
testcase_05 AC 118 ms
76,360 KB
testcase_06 AC 259 ms
79,288 KB
testcase_07 AC 90 ms
75,496 KB
testcase_08 AC 133 ms
78,572 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

class SplayArray(object):
    def __init__(self, n, cell_size = 32):
        self.n = n
        self.cell_size = cell_size
        self.full = (1 << self.cell_size) - 1
        self.length = n // cell_size + (1 if n % cell_size else 0)
        self.cells = [0] * self.length
        self.all_off = True
        self.all_on = False
        self.total = 0

    def _clear_lazy_eval(self):
        if self.all_on:
            self.cells = [self.full] * self.length
            self.all_on = False
        if self.all_off:
            self.cells = [0] * self.length
            self.all_off = False        
    
    def _get_range(self, L, R):
        idxL, posL = divmod(L, self.cell_size)
        idxR, posR = divmod(R, self.cell_size)
        return idxL, posL, idxR, posR
        
    def turn_on(self, L, R):
        '''[l, r] の範囲を on にする。
        '''
        if L == 0 and R == self.n - 1:
            self.all_on = True
            self.all_off = False
            self.total = self.n
            return
        self._clear_lazy_eval()
        self.total = -1
        idxL, posL, idxR, posR = self._get_range(L, R)
        if idxL == idxR:
            self.cells[idxL] |= (1 << (posR + 1)) - (1 << posL)
        else:
            self.cells[idxL] |= (1 << self.cell_size) - (1 << posL)
            self.cells[idxR] |= (1 << (posR + 1)) - 1
            self.cells[idxL + 1 : idxR] = [self.full] * (idxR - idxL - 1)            
    
    def turn_off(self, L, R):
        '''[l, r] の範囲を off にする。
        '''
        if L == 0 and R == self.n - 1:
            self.all_off = True
            self.all_on = False
            self.total = 0
            return
        self._clear_lazy_eval()
        self.total = -1
        idxL, posL, idxR, posR = self._get_range(L, R)
        if idxL == idxR:
            self.cells[idxL] &= ~((1 << (posR + 1)) - (1 << posL))
        else:
            self.cells[idxL] &= ~((1 << self.cell_size) - (1 << posL))
            self.cells[idxR] &= ~((1 << (posR + 1)) - 1)
            self.cells[idxL + 1 : idxR] = [0] * (idxR - idxL - 1)
    
    def count(self, L, R):
        '''[l, r] の範囲の on の個数を返す。
        '''
        is_all_range = (L == 0 and R == self.n - 1)
        if self.all_on:
            return R - L + 1
        if self.all_off:
            return 0
        if is_all_range and self.total != -1:
            return self.total
        idxL, posL, idxR, posR = self._get_range(L, R)
        if idxL == idxR:
            return bin(self.cells[idxL] & ((1 << (posR + 1)) - (1 << posL))).count('1')
        counts = bin(self.cells[idxL] & ((1 << self.cell_size) - (1 << posL))).count('1')
        counts += bin(self.cells[idxR] & ((1 << (posR + 1)) - 1)).count('1')
        for idx in range(idxL + 1, idxR):
            counts += bin(self.cells[idx]).count('1')
        if is_all_range:
            self.total = counts
        return counts


if __name__ == '__main__':
    N = int(input())
    Q = int(input())
    teamA = SplayArray(N)
    teamB = SplayArray(N)
    scoreA = 0
    scoreB = 0
    for q in range(Q):
        x, l, r = map(int, input().split())
        if x == 0:
            bonusA = teamA.count(l, r)
            bonusB = teamB.count(l, r)
            if bonusA > bonusB:
                scoreA += bonusA
            elif bonusA < bonusB:
                scoreB += bonusB
        elif x == 1:
            teamA.turn_on(l, r)
            teamB.turn_off(l, r)
        elif x == 2:
            teamA.turn_off(l, r)
            teamB.turn_on(l, r)
    scoreA += teamA.count(0, N-1)
    scoreB += teamB.count(0, N-1)
    print('{} {}'.format(scoreA, scoreB))
0