結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー 6soukiti296soukiti29
提出日時 2017-09-02 20:09:51
言語 Nim
(2.0.0)
結果
AC  
実行時間 1,048 ms / 5,000 ms
コード長 1,524 bytes
コンパイル時間 4,223 ms
コンパイル使用メモリ 71,160 KB
実行使用メモリ 104,676 KB
最終ジャッジ日時 2023-09-12 15:04:52
合計ジャッジ時間 9,638 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,572 KB
testcase_01 AC 45 ms
12,440 KB
testcase_02 AC 6 ms
7,652 KB
testcase_03 AC 325 ms
66,480 KB
testcase_04 AC 1,048 ms
104,676 KB
testcase_05 AC 437 ms
70,176 KB
testcase_06 AC 492 ms
69,792 KB
testcase_07 AC 584 ms
69,728 KB
testcase_08 AC 583 ms
70,212 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
/home/judge/data/code/Main.nim(1, 8) Warning: imported and not used: 'sequtils' [UnusedImport]
/home/judge/data/code/Main.nim(1, 26) Warning: imported and not used: 'math' [UnusedImport]

ソースコード

diff #

import sequtils,strutils,math,algorithm
type
    binaryIndexTree[I:static[int]] = array[I + 1,int]

proc add(BIT :var binaryIndexTree,i : int, a : int)=
    var index = i
    while BIT[0] >= index:
        BIT[index] += a
        index += ((index xor (index - 1)) and index)

proc sum(BIT : binaryIndexTree, i : int):int =
    var index = i
    while index > 0:
        result += BIT[index]
        index = (index and (index - 1))



proc scanf(frmt: cstring) {.varargs, importc,
    header: "<stdio.h>".}

type
    item = tuple[index, size : int]

var
    N = stdin.readline.parseInt
    T : array[1000_001, int]
    A = newSeq[item](N)
    
for n in 0..<N:
    scanf("%d",addr T[n])
    A[n] = (n, T[n])
    
A = A.sortedByIt(it.size)

var i : int = 1
for j, a in A:
    if j == 0:
        T[a.index] = i
    elif a.size == A[j - 1].size:
        T[a.index] = i
    else:
        i += 1
        T[a.index] = i

var
    BIT,SBIT : binaryIndexTree[1_000_001]
    Sr,Sl,S : array[1_000_001,int]

BIT[0] = 1_000_001
SBIT[0] = 1_000_001

var ans : int

for n in 0..<N:
    var t = T[n]
    Sr[t] += 1
    S[t] += 1
    
for j in 1..i:
    S[j] += S[j - 1]

for n in 0..<N:
    var t = T[n]
    SBIT.add(t, Sr[t] - Sl[t] - 1)
    Sr[t] -= 1
    Sl[t] += 1
    if t > 0:
        ans += BIT.sum(t - 1) * (S[t - 1] - BIT.sum(t - 1))
        ans -= SBIT.sum(t - 1)
    if t < i:
        ans += (BIT.sum(i) - BIT.sum(t)) * (S[i] - S[t] - BIT.sum(i) + BIT.sum(t))
        ans -= (SBIT.sum(i) - SBIT.sum(t))
    BIT.add(t, 1)

echo ans
0