結果
| 問題 | No.121 傾向と対策:門松列(その2) |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-03-25 11:32:21 |
| 言語 | D (dmd 2.109.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,323 bytes |
| 記録 | |
| コンパイル時間 | 1,086 ms |
| コンパイル使用メモリ | 128,688 KB |
| 実行使用メモリ | 153,700 KB |
| 最終ジャッジ日時 | 2024-06-12 18:33:51 |
| 合計ジャッジ時間 | 12,870 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 4 TLE * 1 -- * 4 |
ソースコード
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 st_left = new SegmentTree(M+1);
auto st_right = new SegmentTree(M+1);
auto st_same = new SegmentTree(M+1);
foreach (i; 1..N) {
st_right.add(A[i], 1);
}
long ans = 0;
foreach (i; 1..N-1) {
st_right.add(A[i], -1);
st_left.add(A[i-1], 1);
auto same = st_same.sum(A[i-1], A[i-1]);
st_same.add(A[i-1], -same);
st_same.add(A[i-1], st_left.sum(A[i-1], A[i-1]) * st_right.sum(A[i-1], A[i-1]));
if (A[i] > 0) {
ans += st_left.sum(0, A[i]-1) * st_right.sum(0, A[i]-1) - st_same.sum(0, A[i]-1);
}
if (A[i] < M) {
ans += st_left.sum(A[i]+1, M) * st_right.sum(A[i]+1, M) - st_same.sum(A[i]+1, M);
}
}
ans.writeln;
}
class SegmentTree {
long[] table;
int size;
this(int n) {
assert(bsr(n) < 29);
size = 1 << (bsr(n) + 2);
table = new long[](size);
fill(table, 0);
}
void add(int pos, long num) {
return add(pos, num, 0, 0, size/2-1);
}
void add(int pos, long num, int i, int left, int right) {
table[i] += num;
if (left == right)
return;
auto mid = (left + right) / 2;
if (pos <= mid)
add(pos, num, i*2+1, left, mid);
else
add(pos, num, i*2+2, mid+1, right);
}
long sum(int pl, int pr) {
return sum(pl, pr, 0, 0, size/2-1);
}
long sum(int pl, int pr, int i, int left, int right) {
if (pl > right || pr < left)
return 0;
else if (pl <= left && right <= pr)
return table[i];
else
return
sum(pl, pr, i*2+1, left, (left+right)/2) +
sum(pl, pr, i*2+2, (left+right)/2+1, right);
}
}