結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー Coki628Coki628
提出日時 2020-09-02 02:24:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 135 ms / 2,500 ms
コード長 2,452 bytes
コンパイル時間 985 ms
コンパイル使用メモリ 87,584 KB
実行使用メモリ 81,524 KB
最終ジャッジ日時 2023-08-13 11:43:19
合計ジャッジ時間 3,680 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,492 KB
testcase_01 AC 74 ms
71,624 KB
testcase_02 AC 74 ms
71,624 KB
testcase_03 AC 75 ms
71,700 KB
testcase_04 AC 74 ms
71,752 KB
testcase_05 AC 79 ms
76,188 KB
testcase_06 AC 81 ms
76,260 KB
testcase_07 AC 98 ms
77,960 KB
testcase_08 AC 113 ms
78,144 KB
testcase_09 AC 74 ms
71,656 KB
testcase_10 AC 75 ms
71,680 KB
testcase_11 AC 131 ms
80,676 KB
testcase_12 AC 135 ms
81,524 KB
testcase_13 AC 73 ms
71,724 KB
testcase_14 AC 74 ms
71,620 KB
testcase_15 AC 73 ms
71,644 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10**9)
INF = 10**19
MOD = 10**9 + 7
EPS = 10**-10

class BIT:
    """ Binary Indexed Tree """

    def __init__(self, n):
        # 0-indexed
        n += 1
        nv = 1
        while nv < n:
            nv *= 2
        self.n = n
        self.size = nv
        self.tree = [0] * nv

    def sum(self, i):
        """ [0, i]を合計する """
        s = 0
        i += 1
        while i > 0:
            s += self.tree[i-1]
            i -= i & -i
        return s

    def add(self, i, x):
        """ 値の追加:添字i, 値x """
        i += 1
        while i <= self.size:
            self.tree[i-1] += x
            i += i & -i

    def get(self, l, r=None):
        """ 区間和の取得 [l, r) """
        # 引数が1つなら一点の値を取得
        if r is None: r = l + 1
        res = 0
        if r: res += self.sum(r-1)
        if l: res -= self.sum(l-1)
        return res

    def update(self, i, x):
        """ 値の更新:添字i, 値x """
        self.add(i, x - self.get(i))

    def print(self):
        for i in range(self.n):
            print(self.get(i), end=' ')
        print()

N = INT()
L = [0] * (N+1)
L2 = [0] * (N+1)
R = [0] * (N+1)
R2 = [0] * (N+1)
for i in range(1, N+1):
    x = INT()
    if x <= i:
        L[i] = x
        L2[x] = i
    else:
        R[i] = x

ans = 0

# L - R
lcnt = set()
rcnt = set()
for i in range(1, N+1):
    if R[i]:
        rcnt.add(R[i])
        ans += len(lcnt)
    if L2[i]:
        lcnt.add(L2[i])
        ans += len(rcnt)
    if i in lcnt:
        lcnt.remove(i)
    if i in rcnt:
        rcnt.remove(i)

# L - L
bit = BIT(N+1)
for i in range(1, N+1):
    if L[i]:
        ans += bit.get(L[i], i+1)
        bit.add(L[i], 1)

# R - R
bit = BIT(N+1)
for i in range(N, 0, -1):
    if R[i]:
        ans += bit.get(i, R[i]+1)
        bit.add(R[i], 1)

print(ans)
0