結果

問題 No.1975 Zigzag Sequence
ユーザー 👑 rin204rin204
提出日時 2022-06-11 14:50:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 230 ms / 2,000 ms
コード長 1,511 bytes
コンパイル時間 927 ms
コンパイル使用メモリ 81,496 KB
実行使用メモリ 125,780 KB
最終ジャッジ日時 2023-10-22 00:15:06
合計ジャッジ時間 7,719 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,352 KB
testcase_01 AC 34 ms
53,352 KB
testcase_02 AC 33 ms
53,352 KB
testcase_03 AC 93 ms
76,840 KB
testcase_04 AC 78 ms
76,332 KB
testcase_05 AC 174 ms
94,664 KB
testcase_06 AC 158 ms
93,144 KB
testcase_07 AC 83 ms
76,744 KB
testcase_08 AC 138 ms
87,112 KB
testcase_09 AC 192 ms
105,732 KB
testcase_10 AC 146 ms
90,336 KB
testcase_11 AC 161 ms
92,316 KB
testcase_12 AC 88 ms
77,708 KB
testcase_13 AC 34 ms
53,352 KB
testcase_14 AC 34 ms
53,352 KB
testcase_15 AC 33 ms
53,352 KB
testcase_16 AC 33 ms
53,352 KB
testcase_17 AC 34 ms
53,352 KB
testcase_18 AC 128 ms
92,648 KB
testcase_19 AC 131 ms
92,648 KB
testcase_20 AC 173 ms
93,596 KB
testcase_21 AC 176 ms
93,596 KB
testcase_22 AC 208 ms
124,988 KB
testcase_23 AC 207 ms
125,252 KB
testcase_24 AC 215 ms
125,780 KB
testcase_25 AC 222 ms
125,772 KB
testcase_26 AC 187 ms
92,780 KB
testcase_27 AC 220 ms
110,724 KB
testcase_28 AC 219 ms
125,508 KB
testcase_29 AC 230 ms
125,508 KB
testcase_30 AC 225 ms
125,244 KB
testcase_31 AC 217 ms
125,516 KB
testcase_32 AC 150 ms
92,912 KB
testcase_33 AC 152 ms
92,912 KB
testcase_34 AC 189 ms
96,808 KB
testcase_35 AC 189 ms
96,808 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

MOD = 10 ** 9 + 7

class Bit:
    def __init__(self, n):
        self.size = n
        self.n0 = 1 << (n.bit_length() - 1)
        self.tree = [0] * (n + 1)
    
    def range_sum(self, l, r):
        return self.sum(r - 1) - self.sum(l - 1)
        
    def sum(self, i):
        i += 1
        s = 0
        while i > 0:
            s += self.tree[i]
            s %= MOD
            i -= i & -i
        return s
        
    def get(self, i):
        return self.sum(i) - self.sum(i - 1)
 
    def add(self, i, x):
        i += 1
        while i <= self.size:
            self.tree[i] += x
            self.tree[i] %= MOD
            i += i & -i
         
    def lower_bound(self, x):
        pos = 0
        plus = self.n0
        while plus > 0:
            if pos + plus <= self.size and self.tree[pos + plus] < x:
                x -= self.tree[pos + plus]
                pos += plus
            plus //= 2
        return pos

n = int(input())
A = list(map(int, input().split()))
se = set(A)
dic = {d:i for i, d in enumerate(sorted(se))}
A = [dic[a] for a in A]
l = len(se)

bit = Bit(l)
small = [0] * n
big = [0] * n
plus = 1
for i, a in enumerate(A):
    small[i] = bit.sum(a - 1)
    big[i] = bit.range_sum(a + 1, l)
    bit.add(a, plus)
    plus *= 2
    plus %= MOD
bit = Bit(l)
plus = 1
for i in range(n - 1, -1, -1):
    a = A[i]
    small[i] *= bit.sum(a - 1)
    big[i] *= bit.range_sum(a + 1, l)
    bit.add(a, plus)
    plus *= 2
    plus %= MOD

ans = sum(big) + sum(small)
print(ans % MOD)
0