結果

問題 No.2935 Division Into 3 (Mex Edition)
ユーザー amesyuamesyu
提出日時 2024-10-12 01:30:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,035 ms / 5,000 ms
コード長 1,960 bytes
コンパイル時間 219 ms
コンパイル使用メモリ 82,256 KB
実行使用メモリ 292,612 KB
最終ジャッジ日時 2024-10-12 01:31:32
合計ジャッジ時間 58,553 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 468 ms
245,224 KB
testcase_01 AC 442 ms
245,220 KB
testcase_02 AC 452 ms
255,724 KB
testcase_03 AC 2,190 ms
278,228 KB
testcase_04 AC 2,813 ms
289,124 KB
testcase_05 AC 3,035 ms
290,612 KB
testcase_06 AC 3,022 ms
290,512 KB
testcase_07 AC 2,536 ms
286,076 KB
testcase_08 AC 2,580 ms
288,876 KB
testcase_09 AC 2,730 ms
292,612 KB
testcase_10 AC 2,752 ms
292,504 KB
testcase_11 AC 2,007 ms
290,672 KB
testcase_12 AC 2,396 ms
277,372 KB
testcase_13 AC 2,896 ms
274,988 KB
testcase_14 AC 2,787 ms
289,692 KB
testcase_15 AC 2,595 ms
278,644 KB
testcase_16 AC 2,119 ms
274,272 KB
testcase_17 AC 2,864 ms
290,552 KB
testcase_18 AC 2,863 ms
289,976 KB
testcase_19 AC 2,514 ms
273,976 KB
testcase_20 AC 2,678 ms
276,784 KB
testcase_21 AC 2,160 ms
275,580 KB
testcase_22 AC 2,611 ms
276,360 KB
testcase_23 AC 2,680 ms
277,176 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left, bisect_right

LOG = 17
MAX_N = 2**LOG # 131072
INF = 10**9

N = int(input())
A = [*map(int, input().split())]
ap = [[] for _ in range(MAX_N)]
for i in range(N):
    ap[A[i]].append(i)

treeL = [[[] for _ in range(2*MAX_N)] for __ in range(3)]
treeR = [[[] for _ in range(2*MAX_N)] for __ in range(3)]
interval = [(-1, -1)] * 2 * MAX_N

for t in range(1, 2*MAX_N):
    if t == 1: l, r = 0, MAX_N
    else:
        pl, pr = interval[t//2]
        if t&1: l, r = (pl + pr) >> 1, pr
        else: l, r = pl, (pl + pr) >> 1
        
    idx = []
    for x in range(l, r):
        for i in ap[x]:
            idx.append(i)
    idx.sort()
    
    for c in range(1, 4):
        cnt = [0] * (r - l)
        R, now = 0, 0
        for i in range(len(idx)):
            assert i <= R

            while R < len(idx) and now < r - l:
                pos = A[idx[R]] - l
                cnt[pos] += 1
                if cnt[pos] == c:
                    now += 1
                R += 1

            if now == r - l:
                treeL[c-1][t].append(idx[i])
                treeR[c-1][t].append(idx[R-1])
            
            if cnt[A[idx[i]]-l] == c:
                now -= 1

            cnt[A[idx[i]]-l] -= 1

        treeL[c-1][t].append(INF)
        treeR[c-1][t].append(INF)
    
    interval[t] = (l, r)

def query(l, r):
    mex = []
    for c in range(1, 4):
        ptr = 2
        while ptr < 2*MAX_N:
            L, R = interval[ptr]
            p = bisect_left(treeL[c-1][ptr], l)
            p = treeR[c-1][ptr][p]
            if p < r: ptr = (ptr^1)*2
            else: ptr <<= 1
        mex.append(ptr//2 - MAX_N)
    return mex

Q = int(input())
ansi = 0
for _ in range(Q):
    alpha, beta = map(int, input().split())
    l, r = alpha ^ ansi, beta ^ ansi
    mex1, mex2, mex3 = query(l-1, r)
    ansi = mex1+mex2+mex3
    if mex2 == 0: ansi = min(ansi, r-l-1)
    if mex3 == 0: ansi = min(ansi, r-l)
    print(ansi)
0