結果

問題 No.674 n連勤
ユーザー convexineqconvexineq
提出日時 2023-05-09 12:45:00
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 358 ms / 2,000 ms
コード長 3,993 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 86,332 KB
実行使用メモリ 99,960 KB
最終ジャッジ日時 2023-08-17 05:55:49
合計ジャッジ時間 4,504 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 72 ms
70,848 KB
testcase_01 AC 72 ms
70,856 KB
testcase_02 AC 73 ms
71,064 KB
testcase_03 AC 72 ms
70,856 KB
testcase_04 AC 72 ms
70,836 KB
testcase_05 AC 75 ms
70,952 KB
testcase_06 AC 71 ms
71,068 KB
testcase_07 AC 74 ms
70,744 KB
testcase_08 AC 73 ms
70,956 KB
testcase_09 AC 72 ms
70,844 KB
testcase_10 AC 72 ms
70,944 KB
testcase_11 AC 145 ms
77,984 KB
testcase_12 AC 186 ms
79,580 KB
testcase_13 AC 136 ms
78,512 KB
testcase_14 AC 233 ms
99,572 KB
testcase_15 AC 252 ms
95,640 KB
testcase_16 AC 314 ms
99,668 KB
testcase_17 AC 313 ms
99,796 KB
testcase_18 AC 358 ms
95,044 KB
testcase_19 AC 251 ms
99,960 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class BIT: #0-indexed
    def __init__(self, n):
        self.size = n
        self.tree = [0]*(n+1)
        self.depth = n.bit_length()
        self.n0 = 1<<self.depth
#        self.element = [0]*(n+1)
    def get_sum(self, i): #a_0 + ... + a_{i} #閉区間
        s = 0; i += 1
        while i > 0:
            s += self.tree[i]
            i -= i & -i
        return s
    def query(self,l,r): #a_l + ... + a_r 閉区間
        return self.get_sum(r) - self.get_sum(l-1) 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            i += i & -i
        # self.element[i] += x
    #def get(self,i): return element[i]        
    def bisect_left(self,w):
        #和が w 以上になる最小の index
        #w が存在しない場合 self.size を返す
        if w <= 0: return 0
        x,k = 0,self.n0
        for _ in range(self.depth):
            k >>= 1
            if x+k <= self.size and self.tree[x+k] < w:
                w -= self.tree[x+k]
                x += k
        return x

class interval_manager_unique: #0-indexed
    def __init__(self, n, A=None):
        self.n = n
        self.isL = BIT(n)
        INIT_DATA() # グローバル関数でグローバル変数を初期化しちゃう
        if A is not None:
            assert len(A) == n
            self.val = A[::]
            self.nxt = list(range(1,n+1))
            self.prv = list(range(-1,n))
            for i in range(1,n+1): # isL[i] = 1 となるように初期化
                self.isL.tree[i] = i&-i
        else: # 全て 0 で初期化
            self.val = [0]*n
            self.nxt = [0]*n
            self.nxt[0] = n
            self.prv = [-1]*(n+1)
            self.prv[-1] = 0
            self.isL.add(0,1)
    
    def left(self,i): # i を含む半開区間の左端
        return self.isL.bisect_left(self.isL.get_sum(i))
    
    def right(self,i): # i を含む半開区間の右端
        return self.nxt[self.left(i)]
    
    def get(self,i): # A[i] の値を返す
        return self.val[self.left(i)]
        
    def _cut(self,L,i): # 区間 [L,nxt[L]) を i で切る.
        R = self.nxt[L]
        self.prv[R] = self.nxt[L] = i
        self.prv[i] = L
        self.nxt[i] = R
        self.val[i] = self.val[L]
        self.isL.add(i,1)
    
    def update(self,L,R,x):
        # cut
        LL = self.left(L)
        if L != LL:
            self._cut(LL,L)
        RL = self.left(R-1)
        if R != self.nxt[RL]:
            self._cut(RL,R)
        ### connect
        if L and self.val[self.prv[L]] == x:
            L = self.prv[L]
        if R != self.n and self.val[R] == x:
            R = self.nxt[R]
        ### delete and add
        LL = L
        while True:
            nxt = self.nxt[L]
            DELETE(L,nxt,self.val[L])
            L = nxt
            if L == R: break
            self.isL.add(L,-1)
        ADD(LL,R,x)
        self.nxt[LL] = R
        self.prv[R] = LL
        self.val[LL] = x

    def print_allranges(self):
        L = 0
        while L != self.n:
            print(f"{self.val[L]} for [{L},{self.nxt[L]}),", end=" ")
            L = self.nxt[L]
        print()

    def debug_info(self):
        print("sa ",sa)
        print("isL",[self.isL.query(i,i) for i in range(self.n)])
        print("val",self.val)
        print("nxt",self.nxt)
        print("prv",self.prv)

#####################################
import sys
readline = sys.stdin.readline

D,Q = map(int,readline().split())

def INIT_DATA():
    return

def DELETE(L,R,x):
    return

def ADD(L,R,x):
    global ans
    ans = max(ans,x*(sa[R]-sa[L]))

a = [0]*Q
b = [0]*Q
for i in range(Q):
    a[i],b[i] = map(int,readline().split())
    b[i] += 1

INF = 1<<60
sa = sorted(set(a+b))
za = {v:i for i,v in enumerate(sa)}

data = interval_manager_unique(len(sa))

ans = 0
for l,r in zip(a,b):
    il = za[l]
    ir = za[r]
    data.update(il,ir,1)
    print(ans)
    #data.print_allranges()
    #data.debug_info()
0