結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,064 KB
testcase_01 AC 76 ms
70,840 KB
testcase_02 AC 77 ms
70,884 KB
testcase_03 AC 75 ms
70,904 KB
testcase_04 AC 73 ms
71,132 KB
testcase_05 AC 74 ms
70,944 KB
testcase_06 AC 75 ms
71,148 KB
testcase_07 AC 76 ms
70,704 KB
testcase_08 AC 76 ms
71,084 KB
testcase_09 AC 77 ms
71,304 KB
testcase_10 AC 76 ms
71,040 KB
testcase_11 AC 76 ms
71,220 KB
testcase_12 AC 76 ms
71,108 KB
testcase_13 AC 76 ms
71,216 KB
testcase_14 AC 77 ms
71,204 KB
testcase_15 AC 75 ms
71,196 KB
testcase_16 AC 75 ms
70,860 KB
testcase_17 AC 75 ms
71,196 KB
testcase_18 AC 589 ms
190,056 KB
testcase_19 AC 590 ms
190,112 KB
testcase_20 AC 533 ms
187,936 KB
testcase_21 AC 535 ms
188,076 KB
testcase_22 AC 530 ms
188,168 KB
testcase_23 AC 2,424 ms
190,268 KB
testcase_24 AC 2,442 ms
189,980 KB
testcase_25 AC 2,430 ms
190,152 KB
testcase_26 AC 2,300 ms
190,324 KB
testcase_27 AC 2,284 ms
190,868 KB
testcase_28 AC 2,256 ms
190,564 KB
testcase_29 AC 2,293 ms
190,060 KB
testcase_30 AC 2,290 ms
190,336 KB
testcase_31 AC 2,275 ms
190,324 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():
    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