結果

問題 No.1371 交換門松列・松
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2020-11-13 12:13:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,241 ms / 4,000 ms
コード長 1,578 bytes
コンパイル時間 1,171 ms
コンパイル使用メモリ 87,056 KB
実行使用メモリ 191,356 KB
最終ジャッジ日時 2023-09-30 02:00:54
合計ジャッジ時間 26,763 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 71 ms
71,276 KB
testcase_01 AC 71 ms
71,488 KB
testcase_02 AC 71 ms
71,448 KB
testcase_03 AC 74 ms
71,472 KB
testcase_04 AC 71 ms
71,340 KB
testcase_05 AC 69 ms
71,340 KB
testcase_06 AC 71 ms
71,424 KB
testcase_07 AC 71 ms
71,296 KB
testcase_08 AC 71 ms
71,176 KB
testcase_09 AC 71 ms
71,052 KB
testcase_10 AC 71 ms
71,256 KB
testcase_11 AC 70 ms
71,376 KB
testcase_12 AC 76 ms
71,344 KB
testcase_13 AC 71 ms
71,020 KB
testcase_14 AC 74 ms
71,488 KB
testcase_15 AC 71 ms
71,340 KB
testcase_16 AC 71 ms
71,436 KB
testcase_17 AC 71 ms
71,024 KB
testcase_18 AC 552 ms
190,200 KB
testcase_19 AC 560 ms
190,344 KB
testcase_20 AC 508 ms
188,248 KB
testcase_21 AC 509 ms
188,304 KB
testcase_22 AC 514 ms
188,176 KB
testcase_23 AC 2,241 ms
190,160 KB
testcase_24 AC 2,218 ms
190,624 KB
testcase_25 AC 2,160 ms
190,236 KB
testcase_26 AC 2,186 ms
190,756 KB
testcase_27 AC 2,230 ms
191,356 KB
testcase_28 AC 2,240 ms
190,724 KB
testcase_29 AC 2,237 ms
190,380 KB
testcase_30 AC 2,216 ms
190,748 KB
testcase_31 AC 2,183 ms
190,708 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3
import sys

#https://yukicoder.me/submissions/385244 の移植
def add(bit,x,v):
    n=len(bit)
    while x<n:
        bit[x]+=v
        x+=x&-x


def acc(bit,x):
    a=0
    while x>0:
        a+=bit[x]
        x&=x-1
    return a

def bit_manip(n,points,queries,fstsgn,sndsgn):
    total=0
    acts=[]
    for x,y in points:
        acts.append((x*fstsgn,y*sndsgn,1))
    for x,y in queries:
        acts.append((x*fstsgn,y*sndsgn,0))
    acts.sort()
    bit=[0]*(n+1)
    count=0
    for x,y,k in acts:
        if k:
            add(bit,y*sndsgn,1)
            count+=1
        else:
            if sndsgn==-1:
                total+=count-acc(bit,-y-1)
            else:
                total+=acc(bit,y)
    return total
                

def main():
    n=int(input())
    a=list(map(int,input().split()))

    if a[n-2]<a[n-1]:
        a.append(0)
    else:
        a.append(n+2)
    if a[0]<a[1]:
        a.append(n+2)
    else:
        a.append(0)
    downs=[]
    ups=[]
    lany=[]
    hany=[]
    for i in range(n):
        if a[i]<a[i+1]:
            #でこ
            b=min(a[i-1],a[i+1])
            downs.append((a[i],b))
            lany.append((b,a[i]))
        else:
            #ぼこ
            b=max(a[i-1],a[i+1])
            ups.append((a[i],b))
            hany.append((b,a[i]))
    total=0
    #凹凹
    total+=bit_manip(n,downs,lany,1,-1)
    #凸凹
    total+=bit_manip(n,ups,lany,1,1)
    #凹凸
    total+=bit_manip(n,downs,hany,-1,-1)
    #凸凸
    total+=bit_manip(n,ups,hany,-1,1)

    print((total-n)//2)

main()
0