結果

問題 No.1091 Range Xor Query
ユーザー LyricalMaestro
提出日時 2026-09-06 22:52:15
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 252 ms / 2,000 ms
+ 604µs
コード長 1,706 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 266 ms
コンパイル使用メモリ 96,072 KB
実行使用メモリ 115,072 KB
最終ジャッジ日時 2026-09-06 22:52:35
合計ジャッジ時間 10,868 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/1091

class SegmentTree:
    """
    非再帰版セグメント木。
    更新は「加法」、取得は「最大値」のもの限定。
    """

    def __init__(self, init_array):
        n = 1
        while n < len(init_array):
            n *= 2
        
        self.size = n
        self.array = [0] * (2 * self.size)
        for i, a in enumerate(init_array):
            self.array[self.size + i] = a
        
        end_index = self.size
        start_index = end_index // 2
        while start_index >= 1:
            for i in range(start_index, end_index):
                self.array[i] = self.array[2 * i] ^ self.array[2 * i + 1]
            end_index = start_index
            start_index = end_index // 2

    def add(self, x, a):
        index = self.size + x
        self.array[index] += a
        while index > 1:
            index //= 2
            self.array[index] = max(self.array[2 * index], self.array[2 * index + 1])

    def get_max(self, l, r):
        L = self.size + l; R = self.size + r

        # 2. 区間[l, r)の最大値を求める
        s = 0
        while L < R:
            if R & 1:
                R -= 1
                s ^= self.array[R]
            if L & 1:
                s ^= self.array[L]
                L += 1
            L >>= 1; R >>= 1
        return s

def main():
    N, Q= map(int ,input().split())
    A = list(map(int, input().split()))
    lr = []
    for _ in range(Q):
        l, r = map(int, input().split())
        lr.append((l - 1, r - 1))


    seg_tree = SegmentTree(A)
    for l, r in lr:
        ans = seg_tree.get_max(l, r + 1)
        print(ans)
    



if __name__ == "__main__":
    main()
0