結果

問題 No.1975 Zigzag Sequence
ユーザー titiatitia
提出日時 2022-06-13 01:57:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 332 ms / 2,000 ms
コード長 1,852 bytes
コンパイル時間 276 ms
コンパイル使用メモリ 81,504 KB
実行使用メモリ 133,712 KB
最終ジャッジ日時 2023-10-24 21:01:32
合計ジャッジ時間 8,620 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
73,712 KB
testcase_01 AC 51 ms
73,712 KB
testcase_02 AC 52 ms
73,712 KB
testcase_03 AC 99 ms
84,392 KB
testcase_04 AC 89 ms
83,712 KB
testcase_05 AC 237 ms
96,004 KB
testcase_06 AC 202 ms
100,288 KB
testcase_07 AC 99 ms
84,448 KB
testcase_08 AC 182 ms
93,840 KB
testcase_09 AC 263 ms
113,496 KB
testcase_10 AC 193 ms
97,448 KB
testcase_11 AC 213 ms
99,968 KB
testcase_12 AC 105 ms
84,644 KB
testcase_13 AC 51 ms
73,736 KB
testcase_14 AC 51 ms
73,736 KB
testcase_15 AC 51 ms
73,736 KB
testcase_16 AC 51 ms
73,736 KB
testcase_17 AC 52 ms
73,736 KB
testcase_18 AC 149 ms
98,500 KB
testcase_19 AC 149 ms
98,500 KB
testcase_20 AC 229 ms
100,132 KB
testcase_21 AC 231 ms
100,132 KB
testcase_22 AC 276 ms
131,764 KB
testcase_23 AC 275 ms
131,868 KB
testcase_24 AC 332 ms
133,320 KB
testcase_25 AC 323 ms
133,712 KB
testcase_26 AC 241 ms
94,308 KB
testcase_27 AC 311 ms
114,696 KB
testcase_28 AC 307 ms
133,492 KB
testcase_29 AC 305 ms
133,120 KB
testcase_30 AC 304 ms
132,612 KB
testcase_31 AC 299 ms
132,492 KB
testcase_32 AC 184 ms
100,084 KB
testcase_33 AC 185 ms
100,084 KB
testcase_34 AC 246 ms
104,912 KB
testcase_35 AC 252 ms
104,912 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

mod=10**9+7

N=int(input())
A=list(map(int,input().split()))

def solve(A):
    A=list(A)

    S=sorted(set(A))
    D={S[i]:i+1 for i in range(len(S))}

    for i in range(N):
        A[i]=D[A[i]]

    # BIT(BIT-indexed tree)
    LEN=len(S)+1

    BIT=[0]*(LEN+1) # 1-indexedなtree. 配列BITの長さはLEN+1にしていることに注意。

    def update(v,w): # index vにwを加える
        while v<=LEN:
            BIT[v]+=w
            v+=(v&(-v)) # v&(-v)で、最も下の立っているビット. 自分を含む大きなノードへ. たとえばv=3→v=4

    def getvalue(v): # [1,v]の区間の和を求める
        ANS=0
        while v!=0:
            ANS+=BIT[v]
            v-=(v&(-v)) # 自分より小さい自分の和を構成するノードへ. たとえばv=14→v=12へ
        return ANS


    BIT2=[0]*(LEN+1) # 1-indexedなtree. 配列BITの長さはLEN+1にしていることに注意。

    def update2(v,w): # index vにwを加える
        while v<=LEN:
            BIT2[v]+=w
            v+=(v&(-v)) # v&(-v)で、最も下の立っているビット. 自分を含む大きなノードへ. たとえばv=3→v=4

    def getvalue2(v): # [1,v]の区間の和を求める
        ANS=0
        while v!=0:
            ANS+=BIT2[v]
            v-=(v&(-v)) # 自分より小さい自分の和を構成するノードへ. たとえばv=14→v=12へ
        return ANS

    POW2=[1]
    for i in range(10**5+10):
        POW2.append(POW2[-1]*2%mod)


    for i in range(N):
        a=A[i]
        update(a,POW2[i])

    ANS=0

    for i in range(N-1,-1,-1):
        a=A[i]
        ANS+=getvalue(a-1)*getvalue2(a-1)
        ANS%=mod

        update(a,-POW2[i])
        update2(a,POW2[N-1-i])

    return ANS


B=[10**10-a for a in A]
print((solve(tuple(A))+solve(tuple(B)))%mod)
0