結果

問題 No.2223 Merged Med
ユーザー ShirotsumeShirotsume
提出日時 2023-02-16 21:16:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 6,346 ms / 7,000 ms
コード長 2,410 bytes
コンパイル時間 498 ms
コンパイル使用メモリ 87,080 KB
実行使用メモリ 293,068 KB
最終ジャッジ日時 2023-09-26 18:04:07
合計ジャッジ時間 98,938 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
88,588 KB
testcase_01 AC 146 ms
88,224 KB
testcase_02 AC 176 ms
99,128 KB
testcase_03 AC 146 ms
88,536 KB
testcase_04 AC 172 ms
98,904 KB
testcase_05 AC 188 ms
99,536 KB
testcase_06 AC 147 ms
88,604 KB
testcase_07 AC 191 ms
99,416 KB
testcase_08 AC 184 ms
99,148 KB
testcase_09 AC 183 ms
99,304 KB
testcase_10 AC 186 ms
99,284 KB
testcase_11 AC 162 ms
98,876 KB
testcase_12 AC 180 ms
99,384 KB
testcase_13 AC 2,292 ms
258,428 KB
testcase_14 AC 1,425 ms
193,356 KB
testcase_15 AC 4,243 ms
280,380 KB
testcase_16 AC 5,341 ms
293,068 KB
testcase_17 AC 2,701 ms
280,548 KB
testcase_18 AC 1,338 ms
192,476 KB
testcase_19 AC 2,086 ms
248,940 KB
testcase_20 AC 5,948 ms
290,588 KB
testcase_21 AC 6,346 ms
289,324 KB
testcase_22 AC 5,992 ms
287,692 KB
testcase_23 AC 5,899 ms
289,064 KB
testcase_24 AC 6,044 ms
286,100 KB
testcase_25 AC 5,978 ms
286,676 KB
testcase_26 AC 6,143 ms
290,736 KB
testcase_27 AC 5,992 ms
288,716 KB
testcase_28 AC 6,205 ms
288,880 KB
testcase_29 AC 6,109 ms
289,412 KB
testcase_30 AC 1,914 ms
279,060 KB
testcase_31 AC 1,903 ms
279,292 KB
testcase_32 AC 214 ms
97,976 KB
testcase_33 AC 4,933 ms
284,916 KB
testcase_34 AC 2,310 ms
264,928 KB
testcase_35 AC 2,330 ms
264,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import deque
import sys
from collections import deque, Counter
input = lambda: sys.stdin.readline().rstrip()
ii = lambda: int(input())
Mi = lambda: map(int, input().split())
Li = lambda: list(Mi())
inf = 2 ** 63 - 1
mod = 998244353

def main():

    # ---------------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---------------
    import random
    n, q = Mi()
    a = Li()
    for i in range(n):
        a[i] -= 1
    l = []
    r = []
    for _ in range(q):
        li, ri = Li()
        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 = [[] 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].pop()
                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