:heavy_check_mark: 双対セグメント木
(segment_tree/dual_segment_tree.hpp)

Depends on

Verified with

Code

#pragma once
#include"../alga/maybe.hpp"

/**
 * @brief 双対セグメント木
 */

template<typename T,typename F>
class dual_segment_tree{
	struct node;
	using np=node*;
	using i64=long long;
	struct node{
		maybe<T> val;
		np ch[2]={nullptr,nullptr};
		node(maybe<T> val=maybe<T>()):val(val){}
	};
	np root=nullptr;
	i64 n=1,sz;
    F op;
	public:
	dual_segment_tree(i64 sz,F op=F()):sz(sz),op(op){while(n<sz)n<<=1;}
	inline void update(i64 l,i64 r,T x){update(l,r,x,0,n,root);}
	inline maybe<T> get(i64 x){return get(x,0,n,root);}
	private:
	void eval(np& t){
        auto f=expand<T,F>(op);
		if(t->val.is_none())return;
		if(!t->ch[0])t->ch[0]=new node();
		if(!t->ch[1])t->ch[1]=new node();
		t->ch[0]->val=f(t->ch[0]->val,t->val);
		t->ch[1]->val=f(t->ch[1]->val,t->val);
		t->val=maybe<T>();
	}
	void update(i64 a,i64 b,T x,i64 l,i64 r,np& t){
        auto f=expand<T,F>(op);
        if(!t)t=new node();
		if(r-l>1)eval(t);
		if(r<=a||b<=l)return;
		else if(a<=l&&r<=b)t->val=f(t->val,x);
	    else if(r-l>1){
			update(a,b,x,l,(l+r)/2,t->ch[0]);
			update(a,b,x,(l+r)/2,r,t->ch[1]);
		}
	}
	maybe<T> get(i64 x,i64 l,i64 r,np& t){
        auto f=expand<T,F>(op);
        if(!t)t=new node();
		if(r-l>1)eval(t);
		if(x<l||r<=x)return maybe<T>();
        else if(r-l==1){
            return t->val;
        }
		else return f(get(x,l,(l+r)/2,t->ch[0]),get(x,(l+r)/2,r,t->ch[1]));
	}
};
#line 2 "alga/maybe.hpp"
#include<cassert>

/**
 * @brief Maybe
 * @see https://ja.wikipedia.org/wiki/%E3%83%A2%E3%83%8A%E3%83%89_(%E3%83%97%E3%83%AD%E3%82%B0%E3%83%A9%E3%83%9F%E3%83%B3%E3%82%B0)#Maybe%E3%83%A2%E3%83%8A%E3%83%89
 */

template<typename T>
struct maybe{
    bool _is_none;
    T val;
    maybe():_is_none(true){}
    maybe(T val):_is_none(false),val(val){}
    T unwrap()const{
        assert(!_is_none);
        return val;
    }
    T unwrap_or(T e)const{
        return _is_none?e:val;
    }
    bool is_none()const{return _is_none;}
    bool is_some()const{return !_is_none;}
};

template<typename T,typename F>
auto expand(F op){
    return [&op](const maybe<T>& s,const maybe<T>& t){
        if(s.is_none())return t;
        if(t.is_none())return s;
        return maybe<T>(op(s.unwrap(),t.unwrap()));
    };
}
#line 3 "segment_tree/dual_segment_tree.hpp"

/**
 * @brief 双対セグメント木
 */

template<typename T,typename F>
class dual_segment_tree{
	struct node;
	using np=node*;
	using i64=long long;
	struct node{
		maybe<T> val;
		np ch[2]={nullptr,nullptr};
		node(maybe<T> val=maybe<T>()):val(val){}
	};
	np root=nullptr;
	i64 n=1,sz;
    F op;
	public:
	dual_segment_tree(i64 sz,F op=F()):sz(sz),op(op){while(n<sz)n<<=1;}
	inline void update(i64 l,i64 r,T x){update(l,r,x,0,n,root);}
	inline maybe<T> get(i64 x){return get(x,0,n,root);}
	private:
	void eval(np& t){
        auto f=expand<T,F>(op);
		if(t->val.is_none())return;
		if(!t->ch[0])t->ch[0]=new node();
		if(!t->ch[1])t->ch[1]=new node();
		t->ch[0]->val=f(t->ch[0]->val,t->val);
		t->ch[1]->val=f(t->ch[1]->val,t->val);
		t->val=maybe<T>();
	}
	void update(i64 a,i64 b,T x,i64 l,i64 r,np& t){
        auto f=expand<T,F>(op);
        if(!t)t=new node();
		if(r-l>1)eval(t);
		if(r<=a||b<=l)return;
		else if(a<=l&&r<=b)t->val=f(t->val,x);
	    else if(r-l>1){
			update(a,b,x,l,(l+r)/2,t->ch[0]);
			update(a,b,x,(l+r)/2,r,t->ch[1]);
		}
	}
	maybe<T> get(i64 x,i64 l,i64 r,np& t){
        auto f=expand<T,F>(op);
        if(!t)t=new node();
		if(r-l>1)eval(t);
		if(x<l||r<=x)return maybe<T>();
        else if(r-l==1){
            return t->val;
        }
		else return f(get(x,l,(l+r)/2,t->ch[0]),get(x,(l+r)/2,r,t->ch[1]));
	}
};
Back to top page