Skip to main content

RangeMinimumQuery

Struct RangeMinimumQuery 

Source
pub struct RangeMinimumQuery<T> {
    data: Vec<T>,
    suffix: Vec<T>,
    prefix: Vec<T>,
    table: Vec<T>,
    blocks: usize,
}

Fields§

§data: Vec<T>§suffix: Vec<T>§prefix: Vec<T>§table: Vec<T>§blocks: usize

Implementations§

Source§

impl<T> RangeMinimumQuery<T>
where T: Ord + Copy,

Source

pub fn new(data: Vec<T>) -> Self

Examples found in repository?
crates/library_checker/src/data_structure/staticrmq.rs (line 31)
28pub fn staticrmq_range_minimum_query(reader: impl Read, writer: impl Write) {
29    prepare_io!(reader, writer);
30    sc!(n, q, a: [u64; n], lr: [(usize, usize); iter q]);
31    let rmq = RangeMinimumQuery::new(a);
32    for (l, r) in lr {
33        pp!(rmq.fold(l, r));
34    }
35}
More examples
Hide additional examples
crates/competitive/src/string/string_search.rs (line 74)
70    pub fn new(text: Vec<T>) -> Self {
71        let suffix_array = SuffixArray::new(&text);
72
73        let (lcp_array, rank) = suffix_array.lcp_array_with_rank(&text);
74        let rmq = RangeMinimumQuery::new(lcp_array.clone());
75
76        Self {
77            text,
78            suffix_array,
79            lcp_array,
80            rank,
81            rmq,
82        }
83    }
crates/competitive/src/tree/euler_tour.rs (line 302)
247    pub fn from_parents(parents: &[usize]) -> Self {
248        let n = parents.len();
249        let root = parents.iter().position(|&parent| parent == !0).unwrap();
250        let mut depth = vec![!0u32; n];
251        depth[root] = 0;
252        let mut label_to_node = Vec::with_capacity(n);
253        let mut node_to_label = vec![0u32; n];
254        label_to_node.push(root as u32);
255        let mut path = Vec::new();
256        for mut u in 0..n {
257            if depth[u] != !0 {
258                continue;
259            }
260            if depth[parents[u]] != !0 {
261                depth[u] = depth[parents[u]] + 1;
262                node_to_label[u] = label_to_node.len() as u32;
263                label_to_node.push(u as u32);
264                continue;
265            }
266            while depth[u] == !0 {
267                path.push(u);
268                u = parents[u];
269            }
270            while let Some(u) = path.pop() {
271                depth[u] = depth[parents[u]] + 1;
272                node_to_label[u] = label_to_node.len() as u32;
273                label_to_node.push(u as u32);
274            }
275        }
276
277        let mut label_to_index = vec![0u32; n];
278        for i in (1..n).rev() {
279            let u = label_to_node[i] as usize;
280            let parent = node_to_label[parents[u]] as usize;
281            label_to_index[parent] += label_to_index[i] + 1;
282        }
283        for i in 1..n {
284            let u = label_to_node[i] as usize;
285            let parent = node_to_label[parents[u]] as usize;
286            let descendants = label_to_index[i];
287            let next = label_to_index[parent];
288            label_to_index[i] = next;
289            label_to_index[parent] = next - descendants - 1;
290        }
291
292        let mut index_to_parent = vec![0u32; n];
293        for label in (1..n).rev() {
294            let u = label_to_node[label] as usize;
295            index_to_parent[label_to_index[label] as usize] = node_to_label[parents[u]];
296            node_to_label[u] = label_to_index[label];
297        }
298
299        Self {
300            node_to_index: node_to_label,
301            label_to_node,
302            rmq: RangeMinimumQuery::new(index_to_parent),
303            depth,
304        }
305    }
306
307    /// `order` must be a DFS preorder containing every vertex in `parents` exactly once.
308    /// `parents` uses `!0` for the root.
309    pub fn from_dfs_preorder(parents: &[usize], order: &[usize]) -> Self {
310        let n = parents.len();
311        let mut node_to_index = vec![0u32; n];
312        for (i, &u) in order.iter().enumerate() {
313            node_to_index[u] = i as u32;
314        }
315        let mut depth = vec![0u32; n];
316        let mut index_to_parent = vec![0u32; n];
317        for (i, &u) in order.iter().enumerate().skip(1) {
318            let p = parents[u];
319            depth[u] = depth[p] + 1;
320            index_to_parent[i] = node_to_index[p];
321        }
322        Self {
323            node_to_index,
324            label_to_node: order.iter().map(|&u| u as u32).collect(),
325            rmq: RangeMinimumQuery::new(index_to_parent),
326            depth,
327        }
328    }
Source

pub fn fold(&self, l: usize, r: usize) -> T

Examples found in repository?
crates/competitive/src/string/string_search.rs (line 146)
141    fn lcp_sa(&self, a: usize, b: usize) -> usize {
142        if a == b {
143            return self.text.len() - self.suffix_array[a];
144        }
145        let (l, r) = if a < b { (a, b) } else { (b, a) };
146        self.rmq.fold(l, r)
147    }
More examples
Hide additional examples
crates/library_checker/src/data_structure/staticrmq.rs (line 33)
28pub fn staticrmq_range_minimum_query(reader: impl Read, writer: impl Write) {
29    prepare_io!(reader, writer);
30    sc!(n, q, a: [u64; n], lr: [(usize, usize); iter q]);
31    let rmq = RangeMinimumQuery::new(a);
32    for (l, r) in lr {
33        pp!(rmq.fold(l, r));
34    }
35}
crates/competitive/src/tree/euler_tour.rs (line 342)
336    pub fn lca(&self, u: usize, v: usize) -> usize {
337        if u == v {
338            return u;
339        }
340        let u = self.node_to_index[u] as usize;
341        let v = self.node_to_index[v] as usize;
342        let label = self.rmq.fold(u.min(v) + 1, u.max(v) + 1) as usize;
343        self.label_to_node[label] as usize
344    }

Trait Implementations§

Source§

impl<T: Clone> Clone for RangeMinimumQuery<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for RangeMinimumQuery<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for RangeMinimumQuery<T>
where Vec<T>: Freeze,

§

impl<T> RefUnwindSafe for RangeMinimumQuery<T>
where Vec<T>: RefUnwindSafe,

§

impl<T> Send for RangeMinimumQuery<T>
where Vec<T>: Send,

§

impl<T> Sync for RangeMinimumQuery<T>
where Vec<T>: Sync,

§

impl<T> Unpin for RangeMinimumQuery<T>
where Vec<T>: Unpin,

§

impl<T> UnsafeUnpin for RangeMinimumQuery<T>
where Vec<T>: UnsafeUnpin,

§

impl<T> UnwindSafe for RangeMinimumQuery<T>
where Vec<T>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToArrayVecScalar for T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.