結果

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

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 70 ms
70,996 KB
testcase_01 AC 70 ms
71,168 KB
testcase_02 AC 71 ms
71,160 KB
testcase_03 AC 70 ms
71,108 KB
testcase_04 AC 69 ms
70,672 KB
testcase_05 AC 69 ms
70,920 KB
testcase_06 AC 70 ms
71,092 KB
testcase_07 AC 70 ms
71,112 KB
testcase_08 AC 70 ms
71,008 KB
testcase_09 AC 71 ms
71,164 KB
testcase_10 AC 71 ms
71,040 KB
testcase_11 AC 71 ms
71,088 KB
testcase_12 AC 70 ms
70,864 KB
testcase_13 AC 71 ms
70,836 KB
testcase_14 AC 71 ms
70,944 KB
testcase_15 AC 70 ms
71,060 KB
testcase_16 AC 69 ms
71,272 KB
testcase_17 AC 70 ms
71,080 KB
testcase_18 AC 592 ms
197,396 KB
testcase_19 AC 631 ms
197,348 KB
testcase_20 AC 584 ms
197,124 KB
testcase_21 AC 530 ms
196,528 KB
testcase_22 AC 536 ms
196,592 KB
testcase_23 AC 2,293 ms
193,948 KB
testcase_24 AC 2,331 ms
195,340 KB
testcase_25 AC 2,283 ms
194,808 KB
testcase_26 AC 2,281 ms
193,796 KB
testcase_27 AC 2,294 ms
194,960 KB
testcase_28 AC 2,298 ms
195,252 KB
testcase_29 AC 2,227 ms
194,592 KB
testcase_30 AC 2,248 ms
194,592 KB
testcase_31 AC 2,272 ms
195,408 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)
    for x,y,k in acts:
        if k:
            add(bit,y*sndsgn,1)
        else:
            if sndsgn==-1:
                total+=acc(bit,n)-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=[]
    ll=[]
    lh=[]
    hl=[]
    hh=[]
    for i in range(n):
        if a[i]<a[i+1]:
            #でこ
            b=min(a[i-1],a[i+1])
            downs.append((a[i],b))
            lh.append((b,a[i]))
            ll.append((b,a[i]))
        else:
            #ぼこ
            b=max(a[i-1],a[i+1])
            ups.append((a[i],b))
            hh.append((b,a[i]))
            hl.append((b,a[i]))
    total=0
    #凹凹
    total+=bit_manip(n,downs,lh,1,-1)
    #凸凹
    total+=bit_manip(n,ups,ll,1,1)
    #凹凸
    total+=bit_manip(n,downs,hh,-1,-1)
    #凸凸
    total+=bit_manip(n,ups,hl,-1,1)

    print((total-n)//2)

main()
0