結果
問題 | No.742 にゃんにゃんにゃん 猫の挨拶 |
ユーザー |
![]() |
提出日時 | 2020-09-02 02:24:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 103 ms / 2,500 ms |
コード長 | 2,452 bytes |
コンパイル時間 | 153 ms |
コンパイル使用メモリ | 82,596 KB |
実行使用メモリ | 80,420 KB |
最終ジャッジ日時 | 2024-11-21 06:14:40 |
合計ジャッジ時間 | 1,856 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
import sysdef 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**19MOD = 10**9 + 7EPS = 10**-10class BIT:""" Binary Indexed Tree """def __init__(self, n):# 0-indexedn += 1nv = 1while nv < n:nv *= 2self.n = nself.size = nvself.tree = [0] * nvdef sum(self, i):""" [0, i]を合計する """s = 0i += 1while i > 0:s += self.tree[i-1]i -= i & -ireturn sdef add(self, i, x):""" 値の追加:添字i, 値x """i += 1while i <= self.size:self.tree[i-1] += xi += i & -idef get(self, l, r=None):""" 区間和の取得 [l, r) """# 引数が1つなら一点の値を取得if r is None: r = l + 1res = 0if r: res += self.sum(r-1)if l: res -= self.sum(l-1)return resdef 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] = xL2[x] = ielse:R[i] = xans = 0# L - Rlcnt = 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 - Lbit = 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 - Rbit = 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)