結果

問題 No.121 傾向と対策:門松列(その2)
ユーザー nebukuro09nebukuro09
提出日時 2017-03-25 11:46:12
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 1,454 ms / 5,000 ms
コード長 1,752 bytes
コンパイル時間 1,181 ms
コンパイル使用メモリ 114,736 KB
実行使用メモリ 119,596 KB
最終ジャッジ日時 2023-09-03 12:39:26
合計ジャッジ時間 6,254 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
9,712 KB
testcase_01 AC 57 ms
11,416 KB
testcase_02 AC 5 ms
4,384 KB
testcase_03 AC 434 ms
44,248 KB
testcase_04 AC 1,454 ms
119,596 KB
testcase_05 AC 431 ms
43,516 KB
testcase_06 AC 303 ms
44,224 KB
testcase_07 AC 398 ms
44,752 KB
testcase_08 AC 502 ms
43,484 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop, core.stdc.stdio;


void main() {
    auto N = readln.chomp.to!int;
    auto A = readln.split.map!(to!int).array;
    auto B = A.dup.sort().uniq.array;

    int[int] compressor;
    foreach (i, e; enumerate(B)) {
        compressor[e] = i.to!int;
    }
    foreach (i; 0..N) {
        A[i] = compressor[A[i]];
    }
    auto M = B.length.to!int - 1;

    auto bit_left  = new BIT(M+1);
    auto bit_right = new BIT(M+1);
    auto bit_same  = new BIT(M+1);

    foreach (i; 1..N) {
        bit_right.add(A[i], 1);
    }

    long ans = 0;
    foreach (i; 1..N-1) {
        bit_right.add(A[i], -1);
        bit_left.add(A[i-1], 1);
        auto same = bit_same.sum(A[i-1], A[i-1]+1);
        bit_same.add(A[i-1], -same);
        bit_same.add(A[i-1], bit_left.sum(A[i-1], A[i-1]+1) * bit_right.sum(A[i-1], A[i-1]+1));
        
        if (A[i] > 0) {
            ans += bit_left.sum(0, A[i]) * bit_right.sum(0, A[i]) - bit_same.sum(0, A[i]);
        }
        if (A[i] < M) {
            ans += bit_left.sum(A[i]+1, M+1) * bit_right.sum(A[i]+1, M+1) - bit_same.sum(A[i]+1, M+1);
        }
    }
    
    ans.writeln;
}


class BIT {
    int N;
    long[] table;
    
    this(int n) {
        N = n;
        table = new long[](N);
        fill(table, 0);
    }
    
    // sum [0,i)
    long sum(int i){
        long ret = 0;
        for(--i; i>=0; i=(i&(i+1))-1) ret += table[i];
        return ret;
    }
    
    // sum [i,j)
    long sum(int i, int j) { return sum(j) - sum(i); }
    
    // add x to i
    void add(int i, long x) { for(; i < N; i|=i+1) table[i] += x; }
}
0