結果

問題 No.2223 Merged Med
ユーザー stoqstoq
提出日時 2023-02-16 06:57:27
言語 PyPy3
(7.3.15)
結果
TLE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,342 bytes
コンパイル時間 1,161 ms
コンパイル使用メモリ 86,992 KB
実行使用メモリ 294,036 KB
最終ジャッジ日時 2023-09-26 17:57:13
合計ジャッジ時間 66,606 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 132 ms
90,292 KB
testcase_01 AC 132 ms
90,536 KB
testcase_02 AC 143 ms
90,616 KB
testcase_03 AC 132 ms
90,296 KB
testcase_04 AC 143 ms
90,740 KB
testcase_05 AC 176 ms
101,520 KB
testcase_06 AC 133 ms
90,480 KB
testcase_07 AC 177 ms
101,316 KB
testcase_08 AC 178 ms
101,124 KB
testcase_09 AC 176 ms
101,408 KB
testcase_10 AC 177 ms
101,268 KB
testcase_11 AC 140 ms
90,340 KB
testcase_12 AC 173 ms
101,256 KB
testcase_13 AC 2,549 ms
264,064 KB
testcase_14 AC 1,446 ms
194,064 KB
testcase_15 AC 4,254 ms
290,664 KB
testcase_16 AC 5,629 ms
282,072 KB
testcase_17 AC 2,726 ms
280,580 KB
testcase_18 AC 1,414 ms
193,640 KB
testcase_19 AC 2,232 ms
255,528 KB
testcase_20 AC 6,372 ms
290,572 KB
testcase_21 AC 6,518 ms
287,300 KB
testcase_22 AC 6,318 ms
288,180 KB
testcase_23 AC 6,319 ms
289,552 KB
testcase_24 AC 6,565 ms
289,320 KB
testcase_25 AC 6,931 ms
289,984 KB
testcase_26 AC 6,636 ms
289,348 KB
testcase_27 AC 6,574 ms
289,504 KB
testcase_28 AC 6,632 ms
287,516 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque


def main():
    n, q = map(int, input().split())

    # ---------------segtree---------------

    SIZE = 1 << 17
    LOG = 17
    d = [(0, 0, 0, 0) for _ in range(SIZE * 2)]

    # (lmax, rmax, max, sum)
    def op(s, t):
        return (max(s[0], s[3] + t[0]), max(t[1], s[1] + t[3]),
                max(s[2], t[2], s[1] + t[0]), s[3] + t[3])

    def seg_init():
        for i in range(n):
            d[SIZE + i] = (1, 1, 1, 1)
        for i in range(SIZE - 1, 0, -1):
            seg_update(i)

    def seg_prod(l, r):
        # assert 0 <= l and l <= r and r <= self.n
        sml = (0, 0, 0, 0)
        smr = (0, 0, 0, 0)
        l += SIZE
        r += SIZE
        while l < r:
            if l & 1:
                sml = op(sml, d[l])
                l += 1
            if r & 1:
                smr = op(d[r - 1], smr)
                r -= 1
            l >>= 1
            r >>= 1
        return op(sml, smr)

    def seg_set(p, x):
        # assert 0 <= p and p < n
        p += SIZE
        d[p] = x
        for i in range(1, LOG + 1):
            seg_update(p >> i)

    def seg_update(k):
        d[k] = op(d[2 * k], d[2 * k + 1])

    # ---------------segtree---------------

    a = list(map(int, input().split()))
    for i in range(n):
        a[i] -= 1
    l = []
    r = []
    for _ in range(q):
        li, ri = map(int, input().split())
        l.append(li - 1)
        r.append(ri)
    p = [(a[i], i) for i in range(n)]
    p.sort()
    lo = [-1] * q
    hi = [n] * q
    mi_num = [deque() for _ in range(n)]
    for i in range(q):
        mi_num[(lo[i] + hi[i]) >> 1].append(i)

    ans_cnt = 0
    while ans_cnt < q:
        seg_init()
        for mi in range(n):
            seg_set(p[mi][1], (-1, -1, -1, -1))
            while len(mi_num[mi]) > 0:
                i = mi_num[mi].popleft()
                s = seg_prod(l[i], r[i])
                if s[3] < 0 or s[3] - s[2] + 1 < 0:
                    hi[i] = mi
                else:
                    lo[i] = mi
                if hi[i] - lo[i] > 1:
                    mi_num[lo[i] + ((hi[i] - lo[i]) >> 1)].append(i)
                else:
                    ans_cnt += 1
    for i in range(q):
        print(p[hi[i]][0] + 1)


main()
0