| Task: | Xor sum |
| Sender: | aalto26dm_008 |
| Submission time: | 2026-09-21 16:25:57 +0300 |
| Language: | Rust (2021) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.39 s | details |
Code
#![allow(unused)]
use std::io;
fn take_tuple() -> (usize, usize) {
let mut input: String = String::new();
io::stdin().read_line(&mut input).unwrap();
let mut it = input
.trim()
.split_whitespace()
.map(|x| x.parse::<usize>().unwrap());
(it.next().unwrap(), it.next().unwrap())
}
fn take_vector() -> Vec<usize> {
let mut input = String::new();
io::stdin().read_line(&mut input).unwrap();
let arr: Vec<usize> = input
.trim()
.split_whitespace()
.map(|x| x.parse::<usize>().unwrap())
.collect();
return arr;
}
fn build_segment_tree(arr: &Vec<usize>, n2: &usize) -> Vec<usize> {
let mut tree: Vec<usize> = vec![0; n2 * 2];
let mut k = *n2;
for i in 0..arr.len() {
let mut parent = (k / 2);
tree[k] = arr[i];
while parent >= 1 {
tree[parent] = tree[2 * parent] ^ tree[2 * parent + 1];
parent = (parent / 2);
}
k += 1;
}
tree
}
fn find_min(tree: &Vec<usize>, mut a: usize, mut b: usize, n2: &usize) {
a += *n2;
b += *n2;
let mut c_min = 0;
while (a <= b) {
if a % 2 == 1 {
c_min = tree[a] ^ c_min;
a += 1;
}
if b % 2 == 0 {
c_min = tree[b] ^ c_min;
b -= 1;
}
a = (a / 2);
b = (b / 2);
}
println!("{c_min}");
}
fn main() {
let (n, q): (usize, usize) = take_tuple();
let mut arr: Vec<usize> = take_vector();
let n2 = arr.len().next_power_of_two();
let mut tree: Vec<usize> = build_segment_tree(&arr, &n2);
for _ in 0..q {
let (a, b) = take_tuple();
find_min(&tree, a - 1, b - 1, &n2);
}
}
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 ... |
