結果

問題 No.674 n連勤
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2024-04-11 22:20:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,205 ms / 2,000 ms
コード長 2,799 bytes
コンパイル時間 387 ms
コンパイル使用メモリ 82,096 KB
実行使用メモリ 132,152 KB
最終ジャッジ日時 2024-04-11 22:20:58
合計ジャッジ時間 8,786 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
56,320 KB
testcase_01 AC 45 ms
58,192 KB
testcase_02 AC 45 ms
57,144 KB
testcase_03 AC 45 ms
57,452 KB
testcase_04 AC 45 ms
58,128 KB
testcase_05 AC 45 ms
56,144 KB
testcase_06 AC 46 ms
56,328 KB
testcase_07 AC 45 ms
56,604 KB
testcase_08 AC 45 ms
56,304 KB
testcase_09 AC 46 ms
56,436 KB
testcase_10 AC 45 ms
56,664 KB
testcase_11 AC 391 ms
83,748 KB
testcase_12 AC 261 ms
82,884 KB
testcase_13 AC 124 ms
83,824 KB
testcase_14 AC 655 ms
121,344 KB
testcase_15 AC 736 ms
115,548 KB
testcase_16 AC 1,162 ms
131,444 KB
testcase_17 AC 1,205 ms
132,152 KB
testcase_18 AC 1,072 ms
128,308 KB
testcase_19 AC 878 ms
121,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# import pypyjit
# pypyjit.set_param("max_unroll_recursion=-1")
from collections import *
from functools import *
from itertools import *
from heapq import *
import sys, math
# sys.setrecursionlimit(3*10**5)
input = sys.stdin.readline

class SegTree:
    """
    Segment Tree
    """

    def __init__(self, init_val, segfunc, ide_ele):
        """
        初期化

        init_val: 配列の初期値
        """
        n = len(init_val)
        self.segfunc = segfunc
        self.ide_ele = ide_ele
        self.num = 1 << (n - 1).bit_length()
        self.tree = [ide_ele] * 2 * self.num
        # 配列の値を葉にセット
        for i in range(n):
            self.tree[self.num + i] = init_val[i]
        # 構築していく
        for i in range(self.num - 1, 0, -1):
            self.tree[i] = segfunc(self.tree[2 * i], self.tree[2 * i + 1])

    def update(self, k, x):
        """
        k番目の値をxに更新

        k: index(0-index)
        x: update value
        """
        k += self.num
        self.tree[k] = x
        while k > 1:
            tk = k>>1
            self.tree[tk] = self.segfunc(self.tree[tk<<1], self.tree[(tk<<1)+1])
            k >>= 1

    def query(self, l, r):
        """
        [l, r)のsegfuncしたものを得る

        l: index(0-index)
        r: index(0-index)
        """
        res_l = self.ide_ele
        res_r = self.ide_ele
        
        l += self.num
        r += self.num
        while l < r:
            if l & 1:
                res_l = self.segfunc(res_l, self.tree[l])
                l += 1
            if r & 1:
                res_r = self.segfunc(self.tree[r - 1], res_r)
            l >>= 1
            r >>= 1
        res = self.segfunc(res_l,res_r)
        return res
    


class LargestSubarray:

    ide_ele = (0,0,0,0)
    def segfunc(self,x,y):

        z = [0,0,0,0]
        z[0] = max(x[0],y[0],x[2]+y[1])
        z[1] = max(x[1],x[3]+y[1])
        z[2] = max(y[2],x[2]+y[3])
        z[3] = x[3]+y[3]

        return tuple(z)

    def __init__(self,A):

        self.T = SegTree([(a,a,a,a) for a in A],self.segfunc,self.ide_ele)

    def update(self,k,x):
        
        self.T.update(k,(x,x,x,x))


    def query(self,l,r):

        return self.T.query(l,r)[0]
    

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

AB = [tuple(map(int,input().split())) for _ in range(Q)]
X = []
for a,b in AB:
    X.append(a)
    X.append(b+1)
X = list(set(X))
X.sort()
inv = {x:i for i,x in enumerate(X)}
N = len(X)
I = [X[i+1]-X[i] for i in range(N-1)]+[0]
S = SegTree([(0,i) for i in range(N)],min,(1,N))
T = LargestSubarray([-D]*N)
for a,b in AB:
    v,idx = S.query(inv[a],inv[b+1])
    while v==0:
        T.update(idx,I[idx])
        S.update(idx,(1,idx))
        v,idx = S.query(inv[a],inv[b+1])
    print(T.query(0,N))
0