| Task: | Xor sum |
| Sender: | aalto26dm_020 |
| Submission time: | 2026-09-21 16:59:50 +0300 |
| Language: | Python3 (PyPy3) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.04 s | details |
| #2 | ACCEPTED | 0.39 s | details |
Code
import sys
import math
input_d = sys.stdin.read().split()
n = int(input_d[0])
q = int(input_d[1])
vals = [int(i) for i in input_d[2:n+2]]
queries = []
i = n+2
for k in range(i,len(input_d),2):
queries.append([int(i) for i in input_d[k:k+2]])
nn = 2**(math.ceil(math.log2(n))) if n>0 else 1 #next power of two to handle cases where $n$ is not a power of two
tree = [0]*(2*nn)
#gotta handle if n is not a power of two
#first original elements
for i in range(n):
tree[nn+i] = vals[i]
for i in range(nn-1,0,-1):
tree[i] = tree[2*i] ^ tree[2*i+1]
def update(k,x):
k += nn
tree[k]=x #replace value
k//=2
while(k>=1):
tree[k] = tree[2*k] ^ tree[2*k+1]
k//=2
def getxor(a,b):
a+=nn
b+=nn
res = 0
while(a<=b):
if a%2 == 1:
res = res ^ tree[a]
a+=1
if b %2==0:
res = res ^ tree[b]
b-=1
a//=2
b//=2
print(res)
return res
for query in queries:
getxor(query[0]-1,query[1]-1)
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 8 36 7 6 4 6 2 9 4 8 1 1 1 2 1 3 ... |
| correct output |
|---|
| 7 1 5 3 1 ... |
| user output |
|---|
| 7 1 5 3 1 ... |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 200000 200000 921726510 307633388 992247073 ... |
| correct output |
|---|
| 834756431 130379787 403037296 308618218 784778243 ... |
| user output |
|---|
| 834756431 130379787 403037296 308618218 784778243 ... |
