結果
問題 | No.1300 Sum of Inversions |
ユーザー | LyricalMaestro |
提出日時 | 2024-10-19 04:44:57 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,012 ms / 2,000 ms |
コード長 | 3,214 bytes |
コンパイル時間 | 488 ms |
コンパイル使用メモリ | 82,532 KB |
実行使用メモリ | 183,928 KB |
最終ジャッジ日時 | 2024-10-19 04:45:25 |
合計ジャッジ時間 | 28,087 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
52,888 KB |
testcase_01 | AC | 37 ms
53,780 KB |
testcase_02 | AC | 36 ms
53,672 KB |
testcase_03 | AC | 762 ms
127,264 KB |
testcase_04 | AC | 788 ms
127,012 KB |
testcase_05 | AC | 656 ms
122,148 KB |
testcase_06 | AC | 890 ms
150,968 KB |
testcase_07 | AC | 860 ms
131,660 KB |
testcase_08 | AC | 973 ms
155,200 KB |
testcase_09 | AC | 957 ms
155,248 KB |
testcase_10 | AC | 529 ms
132,992 KB |
testcase_11 | AC | 558 ms
132,780 KB |
testcase_12 | AC | 761 ms
126,996 KB |
testcase_13 | AC | 783 ms
126,336 KB |
testcase_14 | AC | 1,012 ms
183,928 KB |
testcase_15 | AC | 935 ms
155,520 KB |
testcase_16 | AC | 807 ms
127,236 KB |
testcase_17 | AC | 527 ms
127,612 KB |
testcase_18 | AC | 568 ms
127,288 KB |
testcase_19 | AC | 712 ms
121,144 KB |
testcase_20 | AC | 702 ms
121,544 KB |
testcase_21 | AC | 733 ms
121,192 KB |
testcase_22 | AC | 634 ms
123,540 KB |
testcase_23 | AC | 903 ms
150,688 KB |
testcase_24 | AC | 665 ms
119,164 KB |
testcase_25 | AC | 557 ms
129,200 KB |
testcase_26 | AC | 582 ms
133,152 KB |
testcase_27 | AC | 578 ms
126,780 KB |
testcase_28 | AC | 931 ms
161,972 KB |
testcase_29 | AC | 667 ms
120,996 KB |
testcase_30 | AC | 946 ms
155,620 KB |
testcase_31 | AC | 642 ms
123,712 KB |
testcase_32 | AC | 635 ms
119,544 KB |
testcase_33 | AC | 210 ms
107,100 KB |
testcase_34 | AC | 246 ms
103,216 KB |
testcase_35 | AC | 545 ms
140,588 KB |
testcase_36 | AC | 593 ms
183,340 KB |
ソースコード
## https://yukicoder.me/problems/no/1300 MOD = 998244353 class SegmentTree: """ 非再帰版セグメント木。 更新は「加法」、取得は「最大値」のもの限定。 """ def __init__(self, init_array): n = 1 while n < len(init_array): n *= 2 self.size = n self.array = [0] * (2 * self.size) for i, a in enumerate(init_array): self.array[self.size + i] = a end_index = self.size start_index = end_index // 2 while start_index >= 1: for i in range(start_index, end_index): self.array[i] = (self.array[2 * i] + self.array[2 * i + 1]) % MOD end_index = start_index start_index = end_index // 2 def add(self, x, a): index = self.size + x self.array[index] += a while index > 1: index //= 2 self.array[index] = (self.array[2 * index] + self.array[2 * index + 1]) % MOD def get_sum(self, l, r): L = self.size + l; R = self.size + r # 2. 区間[l, r)の最大値を求める s = 0 while L < R: if R & 1: R -= 1 s += self.array[R] s %= MOD if L & 1: s += self.array[L] s %= MOD L += 1 L >>= 1; R >>= 1 return s def main(): N = int(input()) A = list(map(int, input().split())) # 座標圧縮 a_set = set(A) a_set.add(max(A) + 1) a_set.add(max(A) + 2) a_list = list(a_set) a_list.sort() a_map = {} for i, a in enumerate(a_list): a_map[a] = i a_max = len(a_list) # forward方向 forward_nums = [0] * N forward_values = [0] * N num_seg_tree = SegmentTree([0] * a_max) value_seg_tree = SegmentTree([0] * a_max) for i in range(N): a = A[i] a_ = a_map[a] s = num_seg_tree.get_sum(a_ + 1, a_max) v = value_seg_tree.get_sum(a_ + 1, a_max) forward_nums[i] = s forward_values[i] = v num_seg_tree.add(a_, 1) value_seg_tree.add(a_, a) # backward方向 backward_nums = [0] * N backward_values = [0] * N num_seg_tree = SegmentTree([0] * a_max) value_seg_tree = SegmentTree([0] * a_max) for i in reversed(range(N)): a = A[i] a_ = a_map[a] if a_ > 0: s = num_seg_tree.get_sum(0, a_) v = value_seg_tree.get_sum(0, a_) else: s = 0 v = 0 backward_nums[i] = s backward_values[i] = v num_seg_tree.add(a_, 1) value_seg_tree.add(a_, a) answer = 0 for i in range(N): a = A[i] f_n = forward_nums[i] f_v = forward_values[i] b_n = backward_nums[i] b_v = backward_values[i] ans1 = (a * f_n) % MOD ans1 *= b_n ans1 %= MOD ans2 = (f_v * b_n) % MOD ans3 = (b_v * f_n) % MOD answer += (ans1 + ans2) % MOD answer %= MOD answer += ans3 answer %= MOD print(answer) if __name__ == "__main__": main()