結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー vwxyzvwxyz
提出日時 2023-03-23 01:20:30
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 556 ms / 3,500 ms
コード長 1,269 bytes
コンパイル時間 515 ms
コンパイル使用メモリ 11,884 KB
実行使用メモリ 51,452 KB
最終ジャッジ日時 2023-10-18 19:14:38
合計ジャッジ時間 24,604 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
10,184 KB
testcase_01 AC 29 ms
10,184 KB
testcase_02 AC 30 ms
10,188 KB
testcase_03 AC 48 ms
11,444 KB
testcase_04 AC 48 ms
11,444 KB
testcase_05 AC 48 ms
11,444 KB
testcase_06 AC 47 ms
11,444 KB
testcase_07 AC 49 ms
11,444 KB
testcase_08 AC 48 ms
11,444 KB
testcase_09 AC 47 ms
11,444 KB
testcase_10 AC 48 ms
11,444 KB
testcase_11 AC 431 ms
38,960 KB
testcase_12 AC 430 ms
38,416 KB
testcase_13 AC 523 ms
44,560 KB
testcase_14 AC 443 ms
39,580 KB
testcase_15 AC 505 ms
42,532 KB
testcase_16 AC 427 ms
38,332 KB
testcase_17 AC 507 ms
42,908 KB
testcase_18 AC 500 ms
41,736 KB
testcase_19 AC 511 ms
42,992 KB
testcase_20 AC 539 ms
44,572 KB
testcase_21 AC 489 ms
41,716 KB
testcase_22 AC 430 ms
38,332 KB
testcase_23 AC 444 ms
38,848 KB
testcase_24 AC 522 ms
43,280 KB
testcase_25 AC 538 ms
44,128 KB
testcase_26 AC 500 ms
42,156 KB
testcase_27 AC 458 ms
39,632 KB
testcase_28 AC 533 ms
45,192 KB
testcase_29 AC 529 ms
45,192 KB
testcase_30 AC 534 ms
45,192 KB
testcase_31 AC 542 ms
45,192 KB
testcase_32 AC 528 ms
45,192 KB
testcase_33 AC 541 ms
45,192 KB
testcase_34 AC 492 ms
51,452 KB
testcase_35 AC 520 ms
46,776 KB
testcase_36 AC 508 ms
47,224 KB
testcase_37 AC 523 ms
46,776 KB
testcase_38 AC 535 ms
48,420 KB
testcase_39 AC 512 ms
45,960 KB
testcase_40 AC 520 ms
47,492 KB
testcase_41 AC 526 ms
47,012 KB
testcase_42 AC 533 ms
48,036 KB
testcase_43 AC 518 ms
47,036 KB
testcase_44 AC 521 ms
46,712 KB
testcase_45 AC 498 ms
45,648 KB
testcase_46 AC 530 ms
46,160 KB
testcase_47 AC 515 ms
47,080 KB
testcase_48 AC 513 ms
48,328 KB
testcase_49 AC 546 ms
47,476 KB
testcase_50 AC 536 ms
47,024 KB
testcase_51 AC 556 ms
48,284 KB
testcase_52 AC 547 ms
48,284 KB
testcase_53 AC 551 ms
48,284 KB
testcase_54 AC 30 ms
10,184 KB
testcase_55 AC 29 ms
10,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
import bisect

def Cartesian_Tree(lst,maximum=False):
    N=len(lst)
    parents=[None]*N
    stack=[]
    for i in range(N):
        prev=None
        if maximum:
            while stack and lst[stack[-1]]<lst[i]:
                prev=stack.pop()
        else:
            while stack and lst[stack[-1]]>lst[i]:
                prev=stack.pop()
        if prev!=None:
            parents[prev]=i
        if stack:
            parents[i]=stack[-1]
        stack.append(i)
    graph=[[] for x in range(N)]
    for x in range(N):
        if parents[x]!=None:
            graph[parents[x]].append(x)
        else:
            root=x
    return graph,root

N=int(readline())
P=list(map(int,readline().split()))
ans=0
for P in (P,P[::-1]):
    graph,root=Cartesian_Tree(P,maximum=True)
    query=[None]*N
    stack=[(0,root,N)]
    while stack:
        l,rt,r=stack.pop()
        query[rt]=l
        for x in graph[rt]:
            if x<rt:
                stack.append((l,x,rt))
            else:
                stack.append((rt+1,x,r))
    stack=[]
    for i in range(N):
        while stack and P[stack[-1]]>P[i]:
            stack.pop()
        ans+=len(stack)-bisect.bisect_left(stack,query[i])
        stack.append(i)
print(ans)
0