結果
問題 | No.1031 いたずら好きなお姉ちゃん |
ユーザー |
![]() |
提出日時 | 2023-03-23 01:20:30 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 586 ms / 3,500 ms |
コード長 | 1,269 bytes |
コンパイル時間 | 472 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 51,388 KB |
最終ジャッジ日時 | 2024-09-18 15:10:59 |
合計ジャッジ時間 | 26,654 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 53 |
ソースコード
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)