結果
問題 | No.674 n連勤 |
ユーザー | Kiri8128 |
提出日時 | 2020-09-06 17:27:44 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 477 ms / 2,000 ms |
コード長 | 3,270 bytes |
コンパイル時間 | 296 ms |
コンパイル使用メモリ | 82,424 KB |
実行使用メモリ | 117,652 KB |
最終ジャッジ日時 | 2024-05-06 21:16:51 |
合計ジャッジ時間 | 5,222 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
54,200 KB |
testcase_01 | AC | 35 ms
53,908 KB |
testcase_02 | AC | 36 ms
53,784 KB |
testcase_03 | AC | 37 ms
53,736 KB |
testcase_04 | AC | 37 ms
53,360 KB |
testcase_05 | AC | 37 ms
54,736 KB |
testcase_06 | AC | 41 ms
54,904 KB |
testcase_07 | AC | 38 ms
54,264 KB |
testcase_08 | AC | 35 ms
53,356 KB |
testcase_09 | AC | 36 ms
54,052 KB |
testcase_10 | AC | 36 ms
54,156 KB |
testcase_11 | AC | 195 ms
79,480 KB |
testcase_12 | AC | 226 ms
80,696 KB |
testcase_13 | AC | 197 ms
81,604 KB |
testcase_14 | AC | 357 ms
117,068 KB |
testcase_15 | AC | 378 ms
105,812 KB |
testcase_16 | AC | 477 ms
117,604 KB |
testcase_17 | AC | 470 ms
117,348 KB |
testcase_18 | AC | 458 ms
105,660 KB |
testcase_19 | AC | 452 ms
117,652 KB |
ソースコード
class SegmentTreeDual(): def __init__(self, init, unitX, unitA, g, h): self.g = g # (X, A, size) -> X self.h = h # (A, A) -> A self.unitA = unitA if type(init) == int: self.n = init self.n = 1 << (self.n - 1).bit_length() self.X = [unitX] * (self.n * 2) self.size = [1] * (self.n * 2) else: self.n = len(init) self.n = 1 << (self.n - 1).bit_length() self.X = [unitX] * self.n + init + [unitX] * (self.n - len(init)) self.size = [0] * self.n + [1] * len(init) + [0] * (self.n - len(init)) for i in range(self.n - 1, 0, -1): self.size[i] = self.size[i*2] + self.size[i*2|1] self.A = [unitA] * (self.n * 2) def calc(self, i): return self.g(self.X[i], self.A[i], self.size[i]) def propagate(self, i): self.X[i] = self.g(self.X[i], self.A[i], self.size[i]) self.A[i*2] = self.h(self.A[i*2], self.A[i]) self.A[i*2|1] = self.h(self.A[i*2|1], self.A[i]) self.A[i] = self.unitA def propagate_above(self, i): H = i.bit_length() for h in range(H, 0, -1): self.propagate(i >> h) def propagate_all(self): for i in range(1, self.n): self.propagate(i) def getvalue(self, i): i += self.n self.propagate_above(i) return self.calc(i) def operate_range(self, l, r, a): l += self.n r += self.n l0, r0 = l // (l & -l), r // (r & -r) - 1 self.propagate_above(l0) self.propagate_above(r0) while l < r: if l & 1: self.A[l] = self.h(self.A[l], a) l += 1 if r & 1: r -= 1 self.A[r] = self.h(self.A[r], a) l >>= 1 r >>= 1 def debug(self): print("self.n =", self.n) deX = [] deA = [] deS = [] a, b = self.n, self.n * 2 while b: deX.append(self.X[a:b]) deA.append(self.A[a:b]) deS.append(self.size[a:b]) a, b = a//2, a print("--- debug ---") for d in deX[::-1]: print(d) print("--- ---") for d in deA[::-1]: print(d) print("--- ---") for d in deS[::-1]: print(d) print("--- ---") d, Q = map(int, input().split()) S = set() X = [] for _ in range(Q): a, b = map(int, input().split()) S.add(a) S.add(b + 1) S.add(a-1) S.add(b + 2) X.append((a, b+1)) I = sorted(list(S)) D = {a: i for i, a in enumerate(I)} Y = [] for a, b in X: Y.append((D[a], D[b])) N = len(I) inf = 1 << 18 + 1 g = lambda x, a, s: min(x, a) h = lambda a, b: min(a, b) unitA = inf unitX = inf st_min = SegmentTreeDual(N, unitX, unitA, g, h) g = lambda x, a, s: max(x, a) h = lambda a, b: max(a, b) unitA = -1 unitX = -1 st_max = SegmentTreeDual(N, unitX, unitA, g, h) ans = 0 for l, r in Y: mi = min(st_min.getvalue(l-1), l) ma = max(st_max.getvalue(r), r) ans = max(ans, I[ma] - I[mi]) st_min.operate_range(mi, ma, mi) st_max.operate_range(mi, ma, ma) print(ans)