結果

問題 No.1371 交換門松列・松
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2020-11-13 12:25:46
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,478 ms / 4,000 ms
コード長 1,711 bytes
コンパイル時間 412 ms
コンパイル使用メモリ 86,428 KB
実行使用メモリ 177,364 KB
最終ジャッジ日時 2023-09-30 02:02:42
合計ジャッジ時間 28,228 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,112 KB
testcase_01 AC 73 ms
71,320 KB
testcase_02 AC 75 ms
71,088 KB
testcase_03 AC 75 ms
71,200 KB
testcase_04 AC 74 ms
71,164 KB
testcase_05 AC 74 ms
71,200 KB
testcase_06 AC 75 ms
71,296 KB
testcase_07 AC 76 ms
71,280 KB
testcase_08 AC 77 ms
71,328 KB
testcase_09 AC 77 ms
71,384 KB
testcase_10 AC 74 ms
71,160 KB
testcase_11 AC 76 ms
71,284 KB
testcase_12 AC 78 ms
71,112 KB
testcase_13 AC 77 ms
71,196 KB
testcase_14 AC 77 ms
71,148 KB
testcase_15 AC 76 ms
71,412 KB
testcase_16 AC 76 ms
71,140 KB
testcase_17 AC 75 ms
71,076 KB
testcase_18 AC 574 ms
176,844 KB
testcase_19 AC 579 ms
176,940 KB
testcase_20 AC 523 ms
173,164 KB
testcase_21 AC 525 ms
173,208 KB
testcase_22 AC 519 ms
173,428 KB
testcase_23 AC 2,396 ms
177,272 KB
testcase_24 AC 2,408 ms
176,560 KB
testcase_25 AC 2,454 ms
177,364 KB
testcase_26 AC 2,391 ms
177,128 KB
testcase_27 AC 2,478 ms
176,680 KB
testcase_28 AC 2,471 ms
176,980 KB
testcase_29 AC 2,429 ms
177,196 KB
testcase_30 AC 2,417 ms
176,600 KB
testcase_31 AC 2,433 ms
177,324 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#!/usr/bin/env pypy3
import sys
import array

#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=[None]*(len(points)+len(queries))
    pos=0
    for x,y in points:
        acts[pos]=(x*fstsgn,y*sndsgn,1)
        pos+=1
    for x,y in queries:
        acts[pos]=(x*fstsgn,y*sndsgn,0)
        pos+=1
    acts.sort()
    bit=array.array('l',[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():
    input=sys.stdin.readline
    n=int(input().rstrip())
    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