結果
問題 | No.1300 Sum of Inversions |
ユーザー | Shinya Fujita |
提出日時 | 2024-10-11 09:32:15 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,013 ms / 2,000 ms |
コード長 | 3,217 bytes |
コンパイル時間 | 343 ms |
コンパイル使用メモリ | 81,664 KB |
実行使用メモリ | 219,636 KB |
最終ジャッジ日時 | 2024-10-11 09:32:44 |
合計ジャッジ時間 | 26,877 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
52,992 KB |
testcase_01 | AC | 38 ms
52,480 KB |
testcase_02 | AC | 42 ms
52,816 KB |
testcase_03 | AC | 793 ms
153,728 KB |
testcase_04 | AC | 761 ms
153,632 KB |
testcase_05 | AC | 657 ms
127,708 KB |
testcase_06 | AC | 869 ms
184,684 KB |
testcase_07 | AC | 863 ms
160,536 KB |
testcase_08 | AC | 952 ms
189,176 KB |
testcase_09 | AC | 933 ms
189,668 KB |
testcase_10 | AC | 527 ms
119,424 KB |
testcase_11 | AC | 555 ms
119,808 KB |
testcase_12 | AC | 865 ms
153,892 KB |
testcase_13 | AC | 769 ms
153,256 KB |
testcase_14 | AC | 1,013 ms
219,636 KB |
testcase_15 | AC | 923 ms
189,648 KB |
testcase_16 | AC | 781 ms
154,248 KB |
testcase_17 | AC | 538 ms
116,416 KB |
testcase_18 | AC | 584 ms
121,152 KB |
testcase_19 | AC | 716 ms
148,480 KB |
testcase_20 | AC | 692 ms
148,496 KB |
testcase_21 | AC | 722 ms
148,764 KB |
testcase_22 | AC | 632 ms
128,184 KB |
testcase_23 | AC | 904 ms
184,908 KB |
testcase_24 | AC | 674 ms
128,896 KB |
testcase_25 | AC | 558 ms
120,444 KB |
testcase_26 | AC | 593 ms
120,148 KB |
testcase_27 | AC | 621 ms
126,744 KB |
testcase_28 | AC | 990 ms
218,480 KB |
testcase_29 | AC | 710 ms
147,908 KB |
testcase_30 | AC | 963 ms
189,420 KB |
testcase_31 | AC | 680 ms
128,076 KB |
testcase_32 | AC | 656 ms
128,788 KB |
testcase_33 | AC | 98 ms
107,008 KB |
testcase_34 | AC | 135 ms
102,352 KB |
testcase_35 | AC | 554 ms
169,608 KB |
testcase_36 | AC | 622 ms
218,800 KB |
ソースコード
class SegmentTree: def __init__(self, a): self.padding = 0 self.n = len(a) self.N = 2 ** (self.n-1).bit_length() self.seg_data = [self.padding]*(self.N-1) + a + [self.padding]*(self.N-self.n) for i in range(2*self.N-2, 0, -2): self.seg_data[(i-1)//2] = self.seg_data[i] + self.seg_data[i-1] def __len__(self): return self.n def __getitem__(self, i): return self.seg_data[self.N-1+i] def __setitem__(self, i, x): idx = self.N - 1 + i self.seg_data[idx] = x while idx: idx = (idx-1) // 2 self.seg_data[idx] = self.seg_data[2*idx+1] + self.seg_data[2*idx+2] def query(self, i, j): # [i, j) if i == j: return 0 else: idx1 = self.N - 1 + i idx2 = self.N - 2 + j # 閉区間にする result = self.padding while idx1 < idx2 + 1: if idx1&1 == 0: # idx1が偶数 result = result + self.seg_data[idx1] if idx2&1 == 1: # idx2が奇数 result = result + self.seg_data[idx2] idx2 -= 1 idx1 //= 2 idx2 = (idx2 - 1)//2 return result def kth_left_idx(self, fr, k): if self.query(0, fr+1) < k: return -1 remain = k now = fr + self.N - 1 while self.seg_data[now] < remain: if now % 2: remain -= self.seg_data[now] now -= 1 else: now = (now - 1) // 2 while now < self.N - 1: nl = 2*now + 1 nr = nl + 1 if self.seg_data[nr] < remain: remain -= self.seg_data[nr] now = nl else: now = nr return now - (self.N - 1) def kth_right_idx(self, fr, k): if self.query(fr, self.n) < k: return -1 remain = k now = fr + self.N - 1 while self.seg_data[now] < remain: if now % 2 == 0: remain -= self.seg_data[now] now += 1 else: now //= 2 while now < self.N - 1: nl = 2*now + 1 nr = nl + 1 if self.seg_data[nl] < remain: remain -= self.seg_data[nl] now = nr else: now = nl return now - (self.N - 1) N = int(input()) A = list(map(int, input().split())) B = sorted(set(A)) M = len(B) a2id = dict(zip(B, range(M))) C = SegmentTree([0]*M) S = SegmentTree([0]*M) mod = 998244353 cnt = [0] * N ans = [0] * N for i, ai in enumerate(A): idx = a2id[ai] c = C.query(idx+1, M) cnt[i] = c ans[i] = S.query(idx+1, M) C[idx] += 1 S[idx] += ai C = SegmentTree([0]*M) S = SegmentTree([0]*M) ANS = 0 for i in range(N-1, -1, -1): ai = A[i] idx = a2id[ai] c = C.query(0, idx) s = S.query(0, idx) ANS += c * cnt[i] * ai % mod ANS += c * ans[i] % mod ANS += cnt[i] * s % mod ANS %= mod C[idx] += 1 S[idx] += ai print(ANS)