結果
問題 |
No.1371 交換門松列・松
|
ユーザー |
![]() |
提出日時 | 2020-11-13 12:25:46 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 2,706 ms / 4,000 ms |
コード長 | 1,711 bytes |
コンパイル時間 | 493 ms |
コンパイル使用メモリ | 82,560 KB |
実行使用メモリ | 176,476 KB |
最終ジャッジ日時 | 2024-12-27 02:41:08 |
合計ジャッジ時間 | 29,678 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
#!/usr/bin/env pypy3 import sys import array #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=[None]*(len(points)+len(queries)) pos=0 for x,y in points: acts[pos]=(x*fstsgn,y*sndsgn,1) pos+=1 for x,y in queries: acts[pos]=(x*fstsgn,y*sndsgn,0) pos+=1 acts.sort() bit=array.array('l',[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()