Submission #63843233


Source Code Expand

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

INF_NEG = -10**18

class IterSegTree:
    def __init__(self, data):
        self.n = len(data)
        self.size = 1
        while self.size < self.n:
            self.size *= 2
        self.tree = [INF_NEG] * (2 * self.size)
        self.lazy = [0] * self.size
        for i in range(self.n):
            self.tree[self.size + i] = data[i]
        for i in range(self.n, self.size):
            self.tree[self.size + i] = INF_NEG
        for i in range(self.size - 1, 0, -1):
            self.tree[i] = max(self.tree[2 * i], self.tree[2 * i + 1])
    
    def _apply(self, idx, value):
        self.tree[idx] += value
        if idx < self.size:
            self.lazy[idx] += value

    def _push(self, idx):
        h = self.size.bit_length()
        for s in range(h, 0, -1):
            i = idx >> s
            if self.lazy[i]:
                self._apply(2 * i, self.lazy[i])
                self._apply(2 * i + 1, self.lazy[i])
                self.lazy[i] = 0

    def _build(self, idx):
        while idx > 1:
            idx //= 2
            self.tree[idx] = max(self.tree[2 * idx], self.tree[2 * idx + 1]) + self.lazy[idx]

    def update(self, l, r, value):
        l += self.size
        r += self.size
        l0, r0 = l, r 
        while l <= r:
            if l & 1:
                self._apply(l, value)
                l += 1
            if not r & 1:
                self._apply(r, value)
                r -= 1
            l //= 2
            r //= 2
        self._build(l0)
        self._build(r0)

    def query(self, l, r):
        l += self.size
        r += self.size
        self._push(l)
        self._push(r)
        res = INF_NEG
        while l <= r:
            if l & 1:
                res = max(res, self.tree[l])
                l += 1
            if not r & 1:
                res = max(res, self.tree[r])
                r -= 1
            l //= 2
            r //= 2
        return res

    def update_point(self, idx, new_val):
        current = self.query(idx, idx)
        diff = new_val - current
        self.update(idx, idx, 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[i]: A[i..N-1] に含まれる異なる整数の個数
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

F = [INF_NEG] * N
F[0] = prefix[0]

seg = IterSegTree(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(max(prev, 0), j - 1, 1)
    seg.update_point(j, prefix[j])
    if j - 1 >= 0 and j <= N - 2:
        dp = seg.query(0, j - 1)
        ans = max(ans, dp + suffix[j + 1])
    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 550
Code Size 3075 Byte
Status AC
Exec Time 959 ms
Memory 184548 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 2
AC × 44
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 58 ms 76312 KB
00_sample_01.txt AC 58 ms 76304 KB
01_test_00.txt AC 89 ms 83616 KB
01_test_01.txt AC 80 ms 82120 KB
01_test_02.txt AC 90 ms 83296 KB
01_test_03.txt AC 89 ms 83892 KB
01_test_04.txt AC 82 ms 81924 KB
01_test_05.txt AC 329 ms 113032 KB
01_test_06.txt AC 910 ms 166944 KB
01_test_07.txt AC 611 ms 159232 KB
01_test_08.txt AC 834 ms 166472 KB
01_test_09.txt AC 655 ms 159884 KB
01_test_10.txt AC 880 ms 166800 KB
01_test_11.txt AC 376 ms 120236 KB
01_test_12.txt AC 885 ms 166480 KB
01_test_13.txt AC 750 ms 178932 KB
01_test_14.txt AC 959 ms 166400 KB
01_test_15.txt AC 881 ms 142284 KB
01_test_16.txt AC 510 ms 142108 KB
01_test_17.txt AC 898 ms 149420 KB
01_test_18.txt AC 557 ms 149636 KB
01_test_19.txt AC 898 ms 153736 KB
01_test_20.txt AC 569 ms 153356 KB
01_test_21.txt AC 909 ms 158364 KB
01_test_22.txt AC 610 ms 158376 KB
01_test_23.txt AC 941 ms 163412 KB
01_test_24.txt AC 606 ms 163164 KB
01_test_25.txt AC 456 ms 147100 KB
01_test_26.txt AC 472 ms 150156 KB
01_test_27.txt AC 515 ms 149836 KB
01_test_28.txt AC 528 ms 150384 KB
01_test_29.txt AC 465 ms 149512 KB
01_test_30.txt AC 451 ms 149604 KB
01_test_31.txt AC 664 ms 184124 KB
01_test_32.txt AC 693 ms 184536 KB
01_test_33.txt AC 59 ms 76468 KB
01_test_34.txt AC 58 ms 76716 KB
01_test_35.txt AC 600 ms 166796 KB
01_test_36.txt AC 514 ms 139048 KB
01_test_37.txt AC 508 ms 135052 KB
01_test_38.txt AC 632 ms 184548 KB
01_test_39.txt AC 672 ms 184064 KB
01_test_40.txt AC 683 ms 184252 KB
01_test_41.txt AC 699 ms 184088 KB


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