結果

問題 No.1975 Zigzag Sequence
ユーザー titiatitia
提出日時 2022-06-13 01:56:37
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 1,918 ms / 2,000 ms
コード長 1,859 bytes
コンパイル時間 94 ms
コンパイル使用メモリ 12,116 KB
実行使用メモリ 40,812 KB
最終ジャッジ日時 2023-10-24 21:01:23
合計ジャッジ時間 33,930 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 68 ms
14,104 KB
testcase_01 AC 65 ms
14,104 KB
testcase_02 AC 65 ms
14,104 KB
testcase_03 AC 217 ms
16,900 KB
testcase_04 AC 118 ms
15,100 KB
testcase_05 AC 1,389 ms
25,052 KB
testcase_06 AC 1,092 ms
24,816 KB
testcase_07 AC 228 ms
17,052 KB
testcase_08 AC 784 ms
20,608 KB
testcase_09 AC 1,533 ms
30,168 KB
testcase_10 AC 1,019 ms
23,728 KB
testcase_11 AC 1,142 ms
25,040 KB
testcase_12 AC 280 ms
16,384 KB
testcase_13 AC 63 ms
14,104 KB
testcase_14 AC 63 ms
14,104 KB
testcase_15 AC 65 ms
14,168 KB
testcase_16 AC 63 ms
14,104 KB
testcase_17 AC 63 ms
14,104 KB
testcase_18 AC 371 ms
23,500 KB
testcase_19 AC 374 ms
23,500 KB
testcase_20 AC 1,307 ms
24,552 KB
testcase_21 AC 1,309 ms
24,296 KB
testcase_22 AC 1,592 ms
39,432 KB
testcase_23 AC 1,511 ms
39,440 KB
testcase_24 AC 1,799 ms
39,796 KB
testcase_25 AC 1,828 ms
39,800 KB
testcase_26 AC 1,449 ms
25,208 KB
testcase_27 AC 1,918 ms
32,692 KB
testcase_28 AC 1,681 ms
40,812 KB
testcase_29 AC 1,692 ms
40,812 KB
testcase_30 AC 1,713 ms
40,808 KB
testcase_31 AC 1,680 ms
40,812 KB
testcase_32 AC 373 ms
23,504 KB
testcase_33 AC 385 ms
23,504 KB
testcase_34 AC 1,468 ms
26,384 KB
testcase_35 AC 1,511 ms
26,400 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