You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
//summation segment treeletmut seg = SegSum::new(0, m);//range: [0,m), subsequent operations have to be within this rangelet delta :T = ...//T: Add<Output=T> + Mul<i64, Output<T>> + Clone + Default + Debugseg.add(a, b,&delta);//add delta to range [a,b)
seg.update(a, b,&val);//set range [a,b) to val
...let v :T = seg.query_range(i, j);//query sum in range [i,j)//max segment treeletmut seg = SegMax::new(0, m);//range: [0,m), subsequent operations have to be within this rangelet delta :T = ...//T: Add<Output=T> + Ord + Debug + Clone + Min//eg: implMinfori64{fn min() -> i64{
i64::MIN}}
seg.add(a, b,&delta);//add delta to range [a,b)
seg.update(a, b,&val);//set range [a,b) to max(val,element[i]) for i in [a,b)
...let v :T = seg.query_range(i, j);//query max in range [i,j)
red black tree
letmut t : treez::rb::TreeRb<isize,isize> = treez::rb::TreeRb::new();for i in0..nums.len(){let r = nums[i];
t.insert( r, i asisize);}for i in0..nums.len(){let r = nums[i];let v = t.remove(&r ).expect("remove unsuccessful");}
letmut t = treap::NodePtr::new();{let v = t.query_key_range( -100.,100.).iter().map(|x| x.key()).collect::<Vec<_>>();assert_eq!( v.len(),0);}let items = vec![56, -45,1,6,9, -30,7, -9,12,77, -25];for i in items.iter(){
t = t.insert(*i asf32,*i ).0;}
t = t.remove_by_key_range(5.,10.);letmut expected = items.iter().cloned().filter(|x| *x < 5 || *x >= 10).collect::<Vec<_>>();
expected.sort();{let v = t.query_key_range( -100.,100.).iter().map(|x| x.key()).collect::<Vec<_>>();assert_eq!( v.len(), expected.len());
expected.iter().zip( v.iter()).for_each(|(a,b)| assert!(equal_f32((*a asf32),*b )));}let((t1, t2), node_with_key_0 ) = t.split_by_key(0.);assert!( node_with_key_0.is_some());let t3 = t1.merge_contiguous( t2 );{let v = t3.query_key_range( -100.,100.).iter().map(|x| x.key()).collect::<Vec<_>>();assert_eq!( v.len(), expected.len());
expected.iter().zip( v.iter()).for_each(|(a,b)| assert!(equal_f32((*a asf32),*b )));}let va = (100..200).map(|x| (x*2)).collect::<Vec<i32>>();letmut t4 = treap::NodePtr::new();for i in va.iter(){
t4 = t4.insert((*i asf32),*i ).0;}let t5 = t3.union(t4);let vc = (50..70).map(|x| (x*2)).collect::<Vec<i32>>();letmut t6 = treap::NodePtr::new();for i in vc.iter(){
t6 = t6.insert((*i asf32),*i ).0;}let t7 = t5.intersect( t6 );
disjoint set
letmut v = Dsu::new(10);letmut arr = vec![];for i in0..5{letmut node_new = Dsu::new(i);
v.merge(&mut node_new);
arr.push(node_new);}assert_eq!(v.len(),6);for(idx,i)in arr.iter_mut().enumerate(){assert!( i.is_same_set(&mut v));assert_eq!( i.get_data(), idx);}
lower_bound, upper_bound
same logic as C++ lower/upper_bound; requires item type to have cmp::Ord trait
letmut arr = ...arr.sort();let val = ...let idx = bound::upper_bound(&arr[..],&val);//idx in [0, arr.size]letmut arr = ...arr.sort();let val = ...let idx = bound::lower_bound(&arr[..],&val);
strongly connected components
let num_nodes = 4usize;//nodes assumed to be contiguous in range [0,num_nodes)let rel = vec![(0,1),(1,2),(2,0),(1,3)];//edgeslet out = compute(num_nodes,&rel[..]);