Submission #69535344


Source Code Expand

mod = 998244353

def solve1(a):
    # a[i]: -2 or [0, n)
    n = len(a)
    dummy = -2
    accl = [True] * (n + 1)
    for i in range(n):
        accl[i + 1] = accl[i] and (a[i] == dummy or a[i] == i + 1)
    accr = [True] * (n + 1)
    for i in range(n)[::-1]:
        accr[i] = accr[i + 1] and (a[i] == dummy or a[i] == i - 1)

    ans = 0
    # (i-1 <<-> [i]) をつくれる
    avl = [0] * n
    # ([i] <->> i+1) をつくれる
    avr = [0] * n
    for c in range(n):
        if not (accl[c] and accr[c + 1]):
            continue
        if a[c] == dummy:
            ans += n
            avl[c] = 1
            avr[c] = 1
        else:
            ans += 1
            if a[c] == c - 1:
                avl[c] = 1
            if a[c] == c + 1:
                avr[c] = 1
    ans -= sum(avl[c] * avr[c - 1] for c in range(1, n))
    ans %= mod
    return ans

if __name__ == "__main__":
    n = int(input())
    a = tuple(map(lambda s_: int(s_) - 1, input().split()))
    print(solve1(a))

Submission Info

Submission Time
Task C - Tree Sequence
User wasd314
Language Python (PyPy 3.10-v7.3.12)
Score 600
Code Size 1038 Byte
Status AC
Exec Time 113 ms
Memory 123620 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 34
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 01_random_00.txt, 01_random_01.txt, 01_random_02.txt, 01_random_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 02_min_00.txt, 02_min_01.txt, 02_min_02.txt, 02_min_03.txt, 02_min_04.txt, 02_min_05.txt, 02_min_06.txt, 02_min_07.txt, 02_min_08.txt, 02_min_09.txt, 02_min_10.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 60 ms 76316 KiB
00_sample_01.txt AC 59 ms 76380 KiB
00_sample_02.txt AC 60 ms 76596 KiB
01_random_00.txt AC 104 ms 113420 KiB
01_random_01.txt AC 90 ms 104280 KiB
01_random_02.txt AC 111 ms 123620 KiB
01_random_03.txt AC 111 ms 123044 KiB
01_random_04.txt AC 110 ms 123048 KiB
01_random_05.txt AC 105 ms 117044 KiB
01_random_06.txt AC 98 ms 109532 KiB
01_random_07.txt AC 111 ms 122000 KiB
01_random_08.txt AC 112 ms 122948 KiB
01_random_09.txt AC 113 ms 122864 KiB
01_random_10.txt AC 96 ms 118300 KiB
01_random_11.txt AC 97 ms 118308 KiB
01_random_12.txt AC 97 ms 117992 KiB
01_random_13.txt AC 96 ms 117972 KiB
01_random_14.txt AC 107 ms 121716 KiB
01_random_15.txt AC 106 ms 121908 KiB
01_random_16.txt AC 108 ms 122012 KiB
01_random_17.txt AC 107 ms 121884 KiB
01_random_18.txt AC 104 ms 121852 KiB
01_random_19.txt AC 107 ms 121872 KiB
02_min_00.txt AC 60 ms 76296 KiB
02_min_01.txt AC 61 ms 76564 KiB
02_min_02.txt AC 60 ms 76308 KiB
02_min_03.txt AC 60 ms 76176 KiB
02_min_04.txt AC 60 ms 76284 KiB
02_min_05.txt AC 60 ms 76568 KiB
02_min_06.txt AC 60 ms 76596 KiB
02_min_07.txt AC 59 ms 76172 KiB
02_min_08.txt AC 59 ms 76264 KiB
02_min_09.txt AC 61 ms 76268 KiB
02_min_10.txt AC 61 ms 76500 KiB