Submission #63841454


Source Code Expand

Copy
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
class LazySegTree:
def __init__(self, n):
self.n = n
self.size = 1
while self.size < n:
self.size *= 2
self.data = [-10**18] * (2 * self.size)
self.lazy = [0] * (2 * self.size)
def build(self, arr):
for i in range(self.n):
self.data[self.size + i] = arr[i]
for i in range(self.n, self.size):
self.data[self.size + i] = -10**18
for i in range(self.size - 1, 0, -1):
self.data[i] = max(self.data[2 * i], self.data[2 * i + 1])
def _apply(self, idx, val):
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
import sys
sys.setrecursionlimit(10**6)
input = sys.stdin.readline
class LazySegTree:
    def __init__(self, n):
        self.n = n
        self.size = 1
        while self.size < n:
            self.size *= 2
        self.data = [-10**18] * (2 * self.size)
        self.lazy = [0] * (2 * self.size)
    
    def build(self, arr):
        for i in range(self.n):
            self.data[self.size + i] = arr[i]
        for i in range(self.n, self.size):
            self.data[self.size + i] = -10**18
        for i in range(self.size - 1, 0, -1):
            self.data[i] = max(self.data[2 * i], self.data[2 * i + 1])
    
    def _apply(self, idx, val):
        self.data[idx] += val
        self.lazy[idx] += val

    def _push(self, idx):
        if self.lazy[idx]:
            self._apply(2 * idx, self.lazy[idx])
            self._apply(2 * idx + 1, self.lazy[idx])
            self.lazy[idx] = 0

    def _update(self, idx, l, r, ql, qr, val):
        if ql >= r or qr <= l:
            return
        if ql <= l and r <= qr:
            self._apply(idx, val)
            return
        self._push(idx)
        mid = (l + r) // 2
        self._update(2 * idx, l, mid, ql, qr, val)
        self._update(2 * idx + 1, mid, r, ql, qr, val)
        self.data[idx] = max(self.data[2 * idx], self.data[2 * idx + 1])
    
    def update_range(self, l, r, val):
        self._update(1, 0, self.size, l, r + 1, val)
    
    def _query(self, idx, l, r, ql, qr):
        if ql >= r or qr <= l:
            return -10**18
        if ql <= l and r <= qr:
            return self.data[idx]
        self._push(idx)
        mid = (l + r) // 2
        left_max = self._query(2 * idx, l, mid, ql, qr)
        right_max = self._query(2 * idx + 1, mid, r, ql, qr)
        return max(left_max, right_max)
    
    def query_range(self, l, r):
        return self._query(1, 0, self.size, l, r + 1)
    
    def update_point(self, pos, new_val):
        cur = self.query_range(pos, pos)
        diff = new_val - cur
        self.update_range(pos, pos, diff)

N = int(input())
A = list(map(int, input().split()))

prefix = [0] * N
seen = {}
cnt = 0
for i in range(N):
    if A[i] not in seen:
        seen[A[i]] = True
        cnt += 1
    prefix[i] = cnt

suffix = [0] * N
seen = {}
cnt = 0
for i in range(N - 1, -1, -1):
    if A[i] not in seen:
        seen[A[i]] = True
        cnt += 1
    suffix[i] = cnt

INF_NEG = -10**18
F = [INF_NEG] * N
F[0] = prefix[0]

seg = LazySegTree(N)
seg.build(F)
last_occ = {}
ans = 0
for j in range(1, N):
    v = A[j]
    prev = last_occ.get(v, -1)

    if max(prev, 0) <= j - 1:
        seg.update_range(max(prev, 0), j - 1, 1)
    seg.update_point(j, prefix[j])
    dp = seg.query_range(0, j - 1)
    if j <= N - 2:
        candidate = dp + suffix[j + 1]
        ans = max(ans, candidate)
    last_occ[v] = j

print(ans)

Submission Info

Submission Time
Task F - Variety Split Hard
User juten
Language Python (PyPy 3.10-v7.3.12)
Score 0
Code Size 2949 Byte
Status TLE
Exec Time 2219 ms
Memory 184468 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 550
Status
AC × 2
AC × 12
TLE × 32
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt
All 00_sample_00.txt, 00_sample_01.txt, 01_test_00.txt, 01_test_01.txt, 01_test_02.txt, 01_test_03.txt, 01_test_04.txt, 01_test_05.txt, 01_test_06.txt, 01_test_07.txt, 01_test_08.txt, 01_test_09.txt, 01_test_10.txt, 01_test_11.txt, 01_test_12.txt, 01_test_13.txt, 01_test_14.txt, 01_test_15.txt, 01_test_16.txt, 01_test_17.txt, 01_test_18.txt, 01_test_19.txt, 01_test_20.txt, 01_test_21.txt, 01_test_22.txt, 01_test_23.txt, 01_test_24.txt, 01_test_25.txt, 01_test_26.txt, 01_test_27.txt, 01_test_28.txt, 01_test_29.txt, 01_test_30.txt, 01_test_31.txt, 01_test_32.txt, 01_test_33.txt, 01_test_34.txt, 01_test_35.txt, 01_test_36.txt, 01_test_37.txt, 01_test_38.txt, 01_test_39.txt, 01_test_40.txt, 01_test_41.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 57 ms 76472 KB
00_sample_01.txt AC 57 ms 76468 KB
01_test_00.txt AC 112 ms 83640 KB
01_test_01.txt AC 99 ms 83292 KB
01_test_02.txt AC 115 ms 84684 KB
01_test_03.txt AC 129 ms 84528 KB
01_test_04.txt AC 107 ms 85168 KB
01_test_05.txt AC 886 ms 113444 KB
01_test_06.txt TLE 2215 ms 167004 KB
01_test_07.txt TLE 2071 ms 161292 KB
01_test_08.txt TLE 2215 ms 166876 KB
01_test_09.txt AC 1918 ms 161136 KB
01_test_10.txt TLE 2218 ms 166324 KB
01_test_11.txt AC 1074 ms 121084 KB
01_test_12.txt TLE 2215 ms 166604 KB
01_test_13.txt TLE 2215 ms 180444 KB
01_test_14.txt TLE 2215 ms 166544 KB
01_test_15.txt TLE 2214 ms 142328 KB
01_test_16.txt TLE 2214 ms 141900 KB
01_test_17.txt TLE 2214 ms 149960 KB
01_test_18.txt TLE 2214 ms 149820 KB
01_test_19.txt TLE 2214 ms 153132 KB
01_test_20.txt TLE 2217 ms 146252 KB
01_test_21.txt TLE 2217 ms 158812 KB
01_test_22.txt TLE 2215 ms 156140 KB
01_test_23.txt TLE 2215 ms 163096 KB
01_test_24.txt TLE 2215 ms 163080 KB
01_test_25.txt TLE 2213 ms 152312 KB
01_test_26.txt TLE 2216 ms 153836 KB
01_test_27.txt TLE 2213 ms 154432 KB
01_test_28.txt TLE 2213 ms 154052 KB
01_test_29.txt TLE 2213 ms 153484 KB
01_test_30.txt TLE 2213 ms 153548 KB
01_test_31.txt TLE 2217 ms 184236 KB
01_test_32.txt TLE 2216 ms 184172 KB
01_test_33.txt AC 56 ms 76664 KB
01_test_34.txt AC 56 ms 76664 KB
01_test_35.txt TLE 2215 ms 166992 KB
01_test_36.txt TLE 2213 ms 138720 KB
01_test_37.txt TLE 2213 ms 135092 KB
01_test_38.txt TLE 2216 ms 184100 KB
01_test_39.txt TLE 2216 ms 184116 KB
01_test_40.txt TLE 2219 ms 184468 KB
01_test_41.txt TLE 2216 ms 184136 KB


2025-05-05 (Mon)
15:14:32 +09:00