結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー vwxyzvwxyz
提出日時 2023-03-23 01:20:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 301 ms / 3,500 ms
コード長 1,269 bytes
コンパイル時間 189 ms
コンパイル使用メモリ 81,784 KB
実行使用メモリ 112,708 KB
最終ジャッジ日時 2023-10-18 19:14:06
合計ジャッジ時間 15,281 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,532 KB
testcase_01 AC 38 ms
53,532 KB
testcase_02 AC 37 ms
53,532 KB
testcase_03 AC 122 ms
77,360 KB
testcase_04 AC 125 ms
78,168 KB
testcase_05 AC 116 ms
77,384 KB
testcase_06 AC 117 ms
77,420 KB
testcase_07 AC 116 ms
77,264 KB
testcase_08 AC 115 ms
77,504 KB
testcase_09 AC 112 ms
77,404 KB
testcase_10 AC 125 ms
77,336 KB
testcase_11 AC 210 ms
97,736 KB
testcase_12 AC 208 ms
97,560 KB
testcase_13 AC 233 ms
102,432 KB
testcase_14 AC 226 ms
100,304 KB
testcase_15 AC 242 ms
100,908 KB
testcase_16 AC 214 ms
99,464 KB
testcase_17 AC 225 ms
102,820 KB
testcase_18 AC 229 ms
99,888 KB
testcase_19 AC 235 ms
100,912 KB
testcase_20 AC 227 ms
101,944 KB
testcase_21 AC 233 ms
100,200 KB
testcase_22 AC 214 ms
97,448 KB
testcase_23 AC 215 ms
99,912 KB
testcase_24 AC 228 ms
100,908 KB
testcase_25 AC 243 ms
101,984 KB
testcase_26 AC 240 ms
102,464 KB
testcase_27 AC 216 ms
100,500 KB
testcase_28 AC 233 ms
102,064 KB
testcase_29 AC 232 ms
102,056 KB
testcase_30 AC 238 ms
102,244 KB
testcase_31 AC 251 ms
102,428 KB
testcase_32 AC 228 ms
102,424 KB
testcase_33 AC 232 ms
102,164 KB
testcase_34 AC 155 ms
111,816 KB
testcase_35 AC 204 ms
108,140 KB
testcase_36 AC 201 ms
110,552 KB
testcase_37 AC 204 ms
109,864 KB
testcase_38 AC 301 ms
109,460 KB
testcase_39 AC 248 ms
105,856 KB
testcase_40 AC 287 ms
106,244 KB
testcase_41 AC 222 ms
109,240 KB
testcase_42 AC 226 ms
108,948 KB
testcase_43 AC 231 ms
106,708 KB
testcase_44 AC 277 ms
106,376 KB
testcase_45 AC 231 ms
105,656 KB
testcase_46 AC 225 ms
110,028 KB
testcase_47 AC 222 ms
107,592 KB
testcase_48 AC 213 ms
111,572 KB
testcase_49 AC 233 ms
109,940 KB
testcase_50 AC 238 ms
109,580 KB
testcase_51 AC 221 ms
112,504 KB
testcase_52 AC 229 ms
112,708 KB
testcase_53 AC 219 ms
112,500 KB
testcase_54 AC 36 ms
53,532 KB
testcase_55 AC 37 ms
53,532 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