| Task: | Dynamic Range Minimum Queries |
| Sender: | aalto26dh_033 |
| Submission time: | 2026-09-20 14:46:27 +0300 |
| Language: | Rust (2021) |
| Status: | READY |
| Result: | ACCEPTED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.24 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_triple() -> (usize, usize, usize) {
let mut input = 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(), 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![usize::MAX; 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] = usize::min(tree[2 * parent], tree[2 * parent + 1]);
parent = (parent / 2);
}
k += 1;
}
tree
}
fn update_tree(tree: &mut Vec<usize>, mut k: usize, u: usize, n2: &usize) {
k += *n2;
tree[k] = u as usize;
let mut parent = k / 2;
while parent >= 1 {
tree[parent] = usize::min(tree[2 * parent], tree[2 * parent + 1]);
parent = (parent / 2);
}
}
fn find_min(tree: &Vec<usize>, mut a: usize, mut b: usize, n2: &usize) {
a += *n2;
b += *n2;
let mut c_min = usize::MAX;
while (a <= b) {
if a % 2 == 1 {
c_min = usize::min(tree[a], c_min);
a += 1;
}
if b % 2 == 0 {
c_min = usize::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 (command, a, b) = take_triple();
if command == 1 {
update_tree(&mut tree, a - 1, b, &n2);
} else {
find_min(&tree, a - 1, b - 1, &n2);
}
}
}
Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 8 80 7 6 4 6 2 9 4 8 2 1 1 2 1 2 2 1 3 ... |
| correct output |
|---|
| 7 6 4 4 2 ... |
| user output |
|---|
| 7 6 4 4 2 ... |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 200000 200000 398739055 65343131 699208332 3... |
| correct output |
|---|
| 28609 129890 20378 20378 311522 ... |
| user output |
|---|
| 28609 129890 20378 20378 311522 ... |
