結果
問題 | No.1435 Mmm...... |
ユーザー | tyawanmusi |
提出日時 | 2020-10-21 02:25:48 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 2,743 bytes |
コンパイル時間 | 183 ms |
コンパイル使用メモリ | 82,176 KB |
実行使用メモリ | 119,176 KB |
最終ジャッジ日時 | 2024-07-21 09:20:27 |
合計ジャッジ時間 | 4,994 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | WA | - |
testcase_01 | WA | - |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | RE | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | WA | - |
testcase_21 | RE | - |
testcase_22 | RE | - |
testcase_23 | RE | - |
testcase_24 | RE | - |
testcase_25 | RE | - |
testcase_26 | RE | - |
testcase_27 | RE | - |
ソースコード
class SegmentTree: def __init__(self, n, p, unit, f): self.n = n self.num = 2**((n-1).bit_length()) self.seg = [unit]*(self.num*2) for i in range(n): self.seg[self.num+i] = p[i] for i in range(self.num-1, 0, -1): self.seg[i] = f(self.seg[i << 1], self.seg[(i << 1)+1]) self.unit = unit self.f = f def update(self, i, x): i += self.num self.seg[i] = x while i: i >>= 1 self.seg[i] = self.f(self.seg[i << 1], self.seg[(i << 1)+1]) def query(self, l, r): ansl = ansr = self.unit l += self.num r += self.num-1 if l == r: return self.seg[l] while l < r: if l & 1: ansl = self.f(ansl, self.seg[l]) l += 1 if ~r & 1: ansr = self.f(self.seg[r], ansr) r -= 1 l >>= 1 r >>= 1 if l == r: ansl = self.f(ansl, self.seg[l]) return self.f(ansl, ansr) def max_right(self, l, g): l += self.num ll = l // (l & -l) ans = self.unit while g(self.f(ans, self.seg[ll])): ans = self.f(ans, self.seg[ll]) ll += 1 while ~ll & 1: ll >>= 1 if ll == 1: return self.n while ll < self.num: ll <<= 1 if g(self.f(ans, self.seg[ll])): ans = self.f(ans, self.seg[ll]) ll += 1 return ll-self.num def min_left(self, r, g): r += self.num rr = max(r // (~r & -~r), 1) ans = self.unit while g(self.f(self.seg[rr], ans)): ans = self.f(self.seg[rr], ans) rr -= 1 while rr & 1: rr >>= 1 if rr == 0: return -1 while rr < self.num: rr <<= 1 if g(self.f(self.seg[rr+1], ans)): ans = self.f(self.seg[rr+1], ans) else: rr += 1 return rr - self.num def f(x,y): xm1,xm2=x ym1,ym2=y if xm1<ym1: m1=xm1 m2=min(ym1,xm2) else: m1=ym1 m2=min(xm1,ym2) return (m1,m2) MAX=10**9 n=int(input()) p=list(map(int,input().split())) seg=SegmentTree(n,[(i,MAX)for i in p],(MAX,MAX),f) ans=0 ind=[0]*(n+1) for i in range(n): ind[p[i]]=i+1 ll=[0]+[i for i in range(n+1)] rr=[i+1 for i in range(n+1)]+[n+1] for M in range(1,n+1): i=ind[M] l=ll[i]+1 r=rr[i]-1 ll[rr[i]]=ll[i] rr[ll[i]]=rr[i] if r-l<2: continue l-=1 i-=1 r-=1 if i-l<r-i: for li in range(i,l-1,-1): if sum(seg.query(li,max(i+1,li+3)))<M: continue ri=min(seg.max_right(li,lambda x: M<=sum(x))-1,r) if i-li>=2: ans+=ri-i+1 else: ans+=ri-li-1 else: for ri in range(i,r+1): if sum(seg.query(min(ri-2,i),ri+1))<M: continue li=max(seg.min_left(ri,lambda x: M<=sum(x))+1,l) if ri-i>=2: ans+=i-li+1 else: ans+=ri-li-1 print(ans)