結果

問題 No.1435 Mmm......
ユーザー あかりきあかりき
提出日時 2021-03-20 18:20:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 547 ms / 2,000 ms
コード長 1,915 bytes
コンパイル時間 258 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 122,716 KB
最終ジャッジ日時 2024-05-01 05:36:18
合計ジャッジ時間 12,277 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
52,864 KB
testcase_01 AC 38 ms
52,608 KB
testcase_02 AC 37 ms
52,888 KB
testcase_03 AC 37 ms
52,608 KB
testcase_04 AC 41 ms
52,608 KB
testcase_05 AC 37 ms
52,352 KB
testcase_06 AC 418 ms
103,040 KB
testcase_07 AC 478 ms
107,260 KB
testcase_08 AC 517 ms
111,396 KB
testcase_09 AC 472 ms
108,876 KB
testcase_10 AC 467 ms
106,404 KB
testcase_11 AC 456 ms
105,524 KB
testcase_12 AC 447 ms
104,452 KB
testcase_13 AC 422 ms
103,168 KB
testcase_14 AC 368 ms
96,236 KB
testcase_15 AC 514 ms
112,652 KB
testcase_16 AC 474 ms
107,680 KB
testcase_17 AC 396 ms
98,224 KB
testcase_18 AC 538 ms
113,396 KB
testcase_19 AC 434 ms
104,480 KB
testcase_20 AC 511 ms
110,720 KB
testcase_21 AC 415 ms
121,216 KB
testcase_22 AC 493 ms
112,800 KB
testcase_23 AC 547 ms
122,716 KB
testcase_24 AC 535 ms
122,060 KB
testcase_25 AC 527 ms
121,968 KB
testcase_26 AC 545 ms
122,712 KB
testcase_27 AC 529 ms
122,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class SegTree:

  def __init__(self, N, id, f):
    self.n = 2**(N-1).bit_length()
    self.unitX = id
    self.X = [id] * (self.n * 2)
    self.f = f
    self.N = N

  def update(self, i, x):
    i += self.n
    self.X[i] = x
    i >>= 1
    while i:
      self.X[i] = self.f(self.X[i*2], self.X[i*2|1])
      i >>= 1

  def getvalue(self, i):
    return self.X[i + self.n]

  def query(self, l, r):
    l += self.n
    r += self.n
    al = self.unitX
    ar = self.unitX
    while l < r:
      if l & 1:
        al = self.f(al, self.X[l])
        l += 1
      if r & 1:
        r -= 1
        ar = self.f(self.X[r], ar)
      l >>= 1
      r >>= 1
    return self.f(al, ar)

  def max_right(self, l, g):
    l += self.n
    ll = l // (l & -l)
    ans = self.unitX
    while g(self.f(ans, self.X[ll])):
      ans = self.f(ans, self.X[ll])
      ll += 1
      while ~ll & 1:
        ll >>= 1
      if ll == 1:
        return self.N
    while ll < self.n:
      ll <<= 1
      if g(self.f(ans, self.X[ll])):
        ans = self.f(ans, self.X[ll])
        ll += 1
    return ll-self.n
  
  def min_left(self, r, g):
    r += self.n
    rr = max(r // (~r & -~r), 1)
    ans = self.unitX
    while g(self.f(self.X[rr], ans)):
      ans = self.f(self.X[rr], ans)
      rr -= 1
      while rr & 1:
        rr >>= 1
      if rr == 0:
        return -1
    while rr < self.n:
      rr <<= 1
      if g(self.f(self.X[rr+1], ans)):
        ans = self.f(self.X[rr+1], ans)
      else:
        rr += 1
    return rr - self.n

def f(a,b):
  am1,am2,aM=a
  bm1,bm2,bM=b
  if am1<bm1:
    m1=am1
    m2=min(am2,bm1)
  else:
    m1=bm1
    m2=min(bm2,am1)
  M=max(aM,bM)
  return (m1,m2,M)

n=int(input())
a=list(map(int,input().split()))

seg=SegTree(n,(10**18,10**18,-10**18),f)
for i in range(n):
  seg.update(i,(a[i],10**18,a[i]))

ans=0
for i in range(n-1):
  j=seg.max_right(i,lambda x:x[2]<=x[0]+x[1])
  ans+=j-i-1
print(ans)
0