結果
問題 | No.2938 Sigma Sigma Distance Distance Problem |
ユーザー | noriaoki |
提出日時 | 2024-10-18 22:11:08 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 58 ms / 2,000 ms |
コード長 | 3,591 bytes |
コンパイル時間 | 157 ms |
コンパイル使用メモリ | 82,456 KB |
実行使用メモリ | 67,348 KB |
最終ジャッジ日時 | 2024-10-18 22:47:18 |
合計ジャッジ時間 | 3,127 ms |
ジャッジサーバーID (参考情報) |
judge8 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 40 ms
53,796 KB |
testcase_01 | AC | 38 ms
52,868 KB |
testcase_02 | AC | 39 ms
53,012 KB |
testcase_03 | AC | 52 ms
65,176 KB |
testcase_04 | AC | 40 ms
54,788 KB |
testcase_05 | AC | 39 ms
54,108 KB |
testcase_06 | AC | 41 ms
54,352 KB |
testcase_07 | AC | 45 ms
60,972 KB |
testcase_08 | AC | 39 ms
53,864 KB |
testcase_09 | AC | 41 ms
54,016 KB |
testcase_10 | AC | 44 ms
60,232 KB |
testcase_11 | AC | 39 ms
53,940 KB |
testcase_12 | AC | 45 ms
61,692 KB |
testcase_13 | AC | 54 ms
64,468 KB |
testcase_14 | AC | 52 ms
64,432 KB |
testcase_15 | AC | 50 ms
62,716 KB |
testcase_16 | AC | 58 ms
67,348 KB |
testcase_17 | AC | 42 ms
58,836 KB |
testcase_18 | AC | 38 ms
52,956 KB |
testcase_19 | AC | 57 ms
66,740 KB |
testcase_20 | AC | 56 ms
65,892 KB |
testcase_21 | AC | 57 ms
66,872 KB |
04_evil.txt | AC | 834 ms
108,596 KB |
ソースコード
# https://maspypy.com/segment-tree-%E3%81%AE%E3%81%8A%E5%8B%89%E5%BC%B71 class SegmentTree(): def __init__(self, N: int, func, unit: int = 0): self.N = N self.X_unit = unit self.X = [self.X_unit] * (2*self.N) self.X_f = func def build(self, seq): for i, x in enumerate(seq, self.N): self.X[i] = x for i in range(self.N - 1, 0, -1): self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1]) def set_val(self, i, x): i += self.N self.X[i] = x while i > 1: i >>= 1 self.X[i] = self.X_f(self.X[i << 1], self.X[i << 1 | 1]) def get(self, i): return self.X[i + self.N] def fold(self, L, R): L += self.N R += self.N vL = self.X_unit vR = self.X_unit while L < R: if L & 1: vL = self.X_f(vL, self.X[L]) L += 1 if R & 1: R ^= 1 vR = self.X_f(self.X[R], vR) L >>= 1 R >>= 1 return self.X_f(vL, vR) def fold_all(self): return self.X[1] def _search_right_root(self, L, f): R = self.N << 1 vL = self.X_unit sR = [] while L < R: if L & 1: if not f(self.X_f(vL, self.X[L])): return vL, L vL = self.X_f(vL, self.X[L]) L += 1 if R & 1: R ^= 1 sR.append(R) L >>= 1 R >>= 1 while sR: R = sR.pop() if not f(self.X_f(vL, self.X[R])): return vL, R vL = self.X_f(vL, self.X[R]) return -1, self.N << 1 def _search_left_root(self, R, f): L = self.N vR = self.X_unit sL = [] while L < R: if L & 1: sL.append(L) L += 1 if R & 1: R ^= 1 if not f(self.X_f(self.X[R], vR)): return vR, R vR = self.X_f(self.X[R], vR) L >>= 1 R >>= 1 while sL: L = sL.pop() if not f(self.X_f(self.X[L], vR)): return vR, L vR = self.X_f(self.X[L], vR) return -1, self.N def max_right(self, L, f): L += self.N vL, R = self._search_right_root(L, f) while R < self.N: R <<= 1 if f(self.X_f(vL, self.X[R])): vL = self.X_f(vL, self.X[R]) R |= 1 return R - self.N def min_left(self, R, f): R += self.N vR, L = self._search_left_root(R, f) if L == self.N: return 0 while L < self.N: L <<= 1 L |= 1 if f(self.X_f(self.X[L], vR)): vR = self.X_f(self.X[L], vR) L ^= 1 return L + 1 - self.N n = int(input()) a = list(map(int, input().split())) s = SegmentTree(n, lambda x, y: (x[0]+y[0], x[1]+y[1]), (0, 0)) g = SegmentTree(n, lambda x, y: (x[0]+y[0], x[1]+y[1]), (0, 0)) g.build([(1, i) for i in range(n)]) a = sorted([(a[i], i) for i in range(n)]) ans = 0 for v, i in a: g.set_val(i, (0, 0)) ans += i*v*s.fold(0, i)[0] ans -= v*s.fold(0, i)[1] ans += i*v*g.fold(i+1, n)[0] ans -= v*g.fold(i+1, n)[1] ans += v*s.fold(i+1, n)[1] ans -= i*v*s.fold(i+1, n)[0] ans += v*g.fold(0, i)[1] ans -= i*v*g.fold(0, i)[0] s.set_val(i, (1, i)) print(ans*2)