結果

問題 No.1031 いたずら好きなお姉ちゃん
ユーザー shotoyooshotoyoo
提出日時 2021-05-17 23:08:25
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 372 ms / 3,500 ms
コード長 5,037 bytes
コンパイル時間 241 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 102,656 KB
最終ジャッジ日時 2024-04-16 10:54:12
合計ジャッジ時間 17,514 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
52,992 KB
testcase_01 AC 45 ms
53,248 KB
testcase_02 AC 42 ms
52,736 KB
testcase_03 AC 164 ms
78,976 KB
testcase_04 AC 155 ms
78,336 KB
testcase_05 AC 143 ms
78,848 KB
testcase_06 AC 155 ms
78,720 KB
testcase_07 AC 152 ms
78,208 KB
testcase_08 AC 152 ms
78,848 KB
testcase_09 AC 152 ms
78,592 KB
testcase_10 AC 159 ms
78,464 KB
testcase_11 AC 303 ms
99,200 KB
testcase_12 AC 299 ms
98,688 KB
testcase_13 AC 312 ms
100,736 KB
testcase_14 AC 313 ms
99,072 KB
testcase_15 AC 312 ms
100,608 KB
testcase_16 AC 301 ms
96,384 KB
testcase_17 AC 314 ms
100,736 KB
testcase_18 AC 308 ms
99,328 KB
testcase_19 AC 306 ms
100,736 KB
testcase_20 AC 314 ms
100,736 KB
testcase_21 AC 305 ms
99,200 KB
testcase_22 AC 308 ms
96,512 KB
testcase_23 AC 300 ms
98,688 KB
testcase_24 AC 310 ms
100,992 KB
testcase_25 AC 313 ms
100,992 KB
testcase_26 AC 309 ms
100,608 KB
testcase_27 AC 304 ms
98,816 KB
testcase_28 AC 313 ms
101,248 KB
testcase_29 AC 314 ms
101,120 KB
testcase_30 AC 316 ms
101,504 KB
testcase_31 AC 307 ms
101,376 KB
testcase_32 AC 324 ms
101,376 KB
testcase_33 AC 319 ms
101,248 KB
testcase_34 AC 256 ms
102,400 KB
testcase_35 AC 327 ms
102,016 KB
testcase_36 AC 328 ms
102,144 KB
testcase_37 AC 327 ms
102,656 KB
testcase_38 AC 367 ms
100,736 KB
testcase_39 AC 367 ms
100,736 KB
testcase_40 AC 363 ms
100,864 KB
testcase_41 AC 369 ms
100,864 KB
testcase_42 AC 370 ms
100,736 KB
testcase_43 AC 341 ms
100,736 KB
testcase_44 AC 359 ms
100,736 KB
testcase_45 AC 332 ms
100,864 KB
testcase_46 AC 357 ms
101,120 KB
testcase_47 AC 352 ms
101,248 KB
testcase_48 AC 372 ms
101,120 KB
testcase_49 AC 368 ms
101,376 KB
testcase_50 AC 365 ms
101,120 KB
testcase_51 AC 321 ms
101,760 KB
testcase_52 AC 320 ms
101,888 KB
testcase_53 AC 320 ms
101,632 KB
testcase_54 AC 43 ms
52,736 KB
testcase_55 AC 43 ms
52,992 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda : sys.stdin.readline().rstrip()

sys.setrecursionlimit(2*10**5+10)
write = lambda x: sys.stdout.write(x+"\n")
debug = lambda x: sys.stderr.write(x+"\n")
writef = lambda x: print("{:.12f}".format(x))

### セグメント木(はやい)
class SG:
    def __init__(self, n, v=None, ninf=10**12):
        self._n = n
        self.ninf = ninf
        x = 0
        while (1 << x) < n:
            x += 1
        self._log = x
        self._size = 1 << self._log
        self._d = [self.ninf] * (2 * self._size)
        if v is not None:
            for i in range(self._n):
                self._d[self._size + i] = v[i]
        for i in range(self._size - 1, 0, -1):
            self._update(i)
    def _update(self, k):
        self._d[k] = op(self._d[2 * k], self._d[2 * k + 1])
    def update(self, p, x):
        assert 0 <= p < self._n
        p += self._size
        self._d[p] = x
        for i in range(1, self._log + 1):
#             self._update(p >> i)
            k = p>>i
            self._d[k] = op(self._d[2 * k], self._d[2 * k + 1])
    def get(self, p):
#         assert 0 <= p < self._n
        return self._d[p + self._size]
    def check(self):
        return [self.get(p) for p in range(self._n)]
    def query(self, left, right):
        # [l,r)の総和
        assert 0 <= left <= right <= self._n
        sml = self.ninf
        smr = self.ninf
        left += self._size
        right += self._size
        # 外側から計算していく(lは小さい側から, rは大きい側から)
        while left < right:
            if left & 1:
                sml = op(sml, self._d[left])
                left += 1
            if right & 1:
                right -= 1
                smr = op(self._d[right], smr)
            left >>= 1
            right >>= 1
        return op(sml, smr)
    def query_all(self):
        return self._d[1]
    def max_right(self, left, f):
        """f(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最大の r
        -> rはf(op(a[l:r+1]))がFalseになる最小のr
        """
#         assert 0 <= left <= self._n
#         assert f(ninf)
        if left == self._n:
            return self._n
        left += self._size
        sm = self.ninf
        first = True
        while first or (left & -left) != left:
            first = False
            while left % 2 == 0:
                left >>= 1
            if not f(op(sm, self._d[left])):
                while left < self._size:
                    left *= 2
                    if f(op(sm, self._d[left])):
                        sm = op(sm, self._d[left])
                        left += 1
                return left - self._size
            sm = op(sm, self._d[left])
            left += 1
        return self._n
    def min_left(self, right, f):
        """f(op(a[l], a[l + 1], ..., a[r - 1])) = true となる最小の l
        """
#         assert 0 <= right <= self._n
#         assert f(ninf)
        if right == 0:
            return 0
        right += self._size
        sm = self.ninf
        first = True
        while first or (right & -right) != right:
            first = False
            right -= 1
            while right > 1 and right % 2:
                right >>= 1
            if not f(op(self._d[right], sm)):
                while right < self._size:
                    right = 2 * right + 1
                    if f(op(self._d[right], sm)):
                        sm = op(self._d[right], sm)
                        right -= 1
                return right + 1 - self._size
            sm = op(self._d[right], sm)
        return 0
def nearest_larger(a):
    """a[i]よりa[j]が大きいようなjの最小値
    """
    s = [inf]
    si = [None]
    n = len(a)
    ans = [-1]*n
    for i in range(n)[::-1]:
        v = a[i]
        while s[-1]<v:
            s.pop()
            si.pop()
        ans[i] = si[-1]
        s.append(v)
        si.append(i)
    return ans
inf = 10**12
class L2RM:
    """left-to-right maxima
    配列aをi->jにいくときに、最大値が何回更新されるか
    """
    def __init__(self, a):
        self.n = len(a)
        s = [inf]
        si = [-1]
        n = len(a)
        ds = [0]*n
        for i in range(n)[::-1]:
            v = a[i]
            while s[-1]<v:
                s.pop()
                si.pop()
            if si[-1]!=-1:
                ds[i] = ds[si[-1]] + 1
            s.append(v)
            si.append(i)
        self.sg = SG(n, ds, ninf=10**12)
    def query(self, l, r):
        """[l,r]で最大値が何回更新されるか
        """
        return self.sg.get(l) - self.sg.query(l,r+1)
op = min

n = int(input())
a = list(map(int, input().split()))
sg = SG(n,[(v,i) for i,v in enumerate(a)],ninf=(10**12,-1))
l1 = L2RM(a)
l2 = L2RM(a[::-1])
q = [(0,n-1)]
ans = 0
while q:
    l,r = q.pop()
    if l==r:
        continue
    v,m = sg.query(l,r+1)
    ans += l1.query(m,r) + l2.query(n-1-m, n-1-l)
    if l!=m:
        q.append((l,m-1))
    if r!=m:
        q.append((m+1,r))
print(ans)
0