結果

問題 No.3205 Range Pairwise Xor Query
ユーザー dp_ijk
提出日時 2025-07-18 21:30:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,018 ms / 2,000 ms
コード長 1,325 bytes
コンパイル時間 448 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 199,528 KB
最終ジャッジ日時 2025-07-18 21:30:34
合計ジャッジ時間 13,558 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

class Accumulated:
   def __init__(self, iterable):
      from itertools import accumulate
      self.cumsum = list(accumulate(iterable, initial=0))
      self.N = len(self.cumsum) - 1

   def get(self, idx):
      assert 0 <= idx < self.N
      return self.cumsum[idx+1] - self.cumsum[idx]

   def fold(self, l, r):
      if l < 0: l = 0
      if r > self.N: r = self.N
      if l >= r: return 0
      return self.cumsum[r] - self.cumsum[l]

   def __getitem__(self, idx: int|slice):
      if isinstance(idx, int):
         return self.get(idx)
      else:
         l = idx.start
         r = idx.stop
         if l is None: l = 0
         if r is None: r = self.N
         return self.fold(l, r)

   def __iter__(self):
      for i in range(self.N):
         yield self.get(i)

   def __len__(self):
      return self.N

   def __repr__(self):
      return repr(list(self))

   sum = fold
   prod = fold

N, Q = map(int, input().split())
A = list(map(int, input().split()))

maxbitlen = max(int.bit_length(a) for a in A)
segs = [Accumulated([(a>>e)&1 for a in A]) for e in range(maxbitlen)]

ans = []
for _ in range(Q):
   l, r = map(int, input().split())
   l -= 1
   res = 0
   for e in range(maxbitlen):
      x1 = segs[e][l:r]
      x0 = (r-l) - x1
      res += (x0*x1)<<e
   ans.append(res)

for a in ans:
   print(a)
0