結果
| 問題 |
No.121 傾向と対策:門松列(その2)
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2015-07-14 00:57:27 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,500 bytes |
| コンパイル時間 | 308 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 312,228 KB |
| 最終ジャッジ日時 | 2024-07-08 07:04:47 |
| 合計ジャッジ時間 | 8,028 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 1 WA * 8 |
ソースコード
def compress(As):
'''とびとびの値のリストを連続する値のリストに変換する。変換後の最大値も返す。
'''
val2idx = {a: i for i, a in enumerate(sorted(set(As)))}
return [val2idx[a] for a in As] #,len(uniqA) - 1
def add_count_f(val, data, size):
'''セグメントツリーへの要素 val を追加し、
val 未満の要素の個数と、val 超の要素の個数を返す。
'''
pos = size + val
count = 0
while pos:
if pos & 1:
count += data[pos-1]
data[pos] += 1
pos >>= 1
return count, data[1] - count - data[size + val]
def count_aba2(As):
maxA = max(As)
freq = [-1] * (maxA + 1)
for a in As:
freq[a] += 1
posList = {i:[] for i in range(maxA + 1) if freq[i]}
for i, a in enumerate(As):
if freq[a]:
posList[a].append(i)
count = 0
for poss in posList.values():
n = len(poss)
for i in range(1, n):
count += (poss[i] - poss[i-1] - 1) * i * (n-i)
return count
N = int(input())
As = compress(list(map(int, input().split())))
size = 1 << (len(bin(N)) - 2)
segL = [0] * (size * 2)
segR = [0] * (size * 2)
result = 0
for i, a in enumerate(As, 1):
belowL, aboveL = add_count_f(a, segL, size)
belowR, aboveR = add_count_f(As[-i], segR, size)
result += belowL * belowR + aboveL * aboveR
# 左右が同じ値の疑似門松列の個数を差し引く
result -= count_aba2(As)
print(result)