結果
問題 | No.742 にゃんにゃんにゃん 猫の挨拶 |
ユーザー |
![]() |
提出日時 | 2018-10-05 23:06:37 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 16 ms / 2,500 ms |
コード長 | 913 bytes |
コンパイル時間 | 814 ms |
コンパイル使用メモリ | 98,560 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-06-13 01:50:35 |
合計ジャッジ時間 | 1,741 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 16 |
ソースコード
void main() {import std.stdio, std.string, std.conv, std.algorithm;int n;rd(n);auto a = new int[](n);foreach (i; 0 .. n)rd(a[i]);int cnt = 0;auto tree = new FenwickTree(n);foreach (i; 0 .. n) {cnt += i - tree.sum(a[i]);tree.add(a[i], 1);}writeln(cnt);}class FenwickTree {int n;int[] dat;this(int n) {this.n = n;dat.length = n + 1;}void add(int i, int x) {for (int k = i; k <= n; k += k & (-k))dat[k] += x;}int sum(int i) { // [1, i]int s = 0;for (int k = i; k > 0; k -= k & (-k))s += dat[k];return s;}int sum(int i, int j) { // [i, j]return sum(j) - sum(i);}}void rd(T...)(ref T x) {import std.stdio : readln;import std.string : split;import std.conv : to;auto l = readln.split;assert(l.length == x.length);foreach (i, ref e; x)e = l[i].to!(typeof(e));}