結果

問題 No.742 にゃんにゃんにゃん 猫の挨拶
ユーザー Coki628Coki628
提出日時 2020-09-02 02:24:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 96 ms / 2,500 ms
コード長 2,452 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,492 KB
実行使用メモリ 80,368 KB
最終ジャッジ日時 2024-05-01 05:00:19
合計ジャッジ時間 1,740 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
53,748 KB
testcase_01 AC 36 ms
53,792 KB
testcase_02 AC 36 ms
54,256 KB
testcase_03 AC 36 ms
53,876 KB
testcase_04 AC 38 ms
54,508 KB
testcase_05 AC 42 ms
59,936 KB
testcase_06 AC 44 ms
62,072 KB
testcase_07 AC 60 ms
70,808 KB
testcase_08 AC 78 ms
77,324 KB
testcase_09 AC 36 ms
53,332 KB
testcase_10 AC 36 ms
52,872 KB
testcase_11 AC 93 ms
79,776 KB
testcase_12 AC 96 ms
80,368 KB
testcase_13 AC 35 ms
53,072 KB
testcase_14 AC 35 ms
53,552 KB
testcase_15 AC 36 ms
53,700 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