2022-12-18日更新
function addition库
添加function标头所没有的取min,max的class重载函数运算符式函数对象
template <typename _type_> struct maximize{
constexpr inline _type_ operator()(const _type_ &x,const _type_ &y)const{return x<y?y:x;}
};
template <typename _type_> struct minimize{
constexpr inline _type_ operator()(const _type_ &x,const _type_ &y)const{return x<y?x:y;}
};
sparse table 稀疏表(ST表)
继承function addition库
template<typename _type_,int _size,class _operator=maximize<int> > class sparse_table{
private:
std::vector<std::vector<_type_> > st;
int msz;
_operator oper;
public:
inline _type_& operator [](int x){return st[0][x];}
inline const _type_ operator [](int x)const{return st[0][x];}
inline _type_& at(int x){return st.at(0).at(x);}
inline const _type_ at(int x)const{return st.at(0).at(x);}
inline void init(std::vector<_type_> val){
st[0]=val,st[0].resize(_size);
int len=val.size();
msz=log2(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(int len){
msz=log2(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void clear(){std::vector<std::vector<_type_> >().swap(st),st.resize(1),st[0].resize(_size),msz=0;}
inline sparse_table(std::vector<_type_> val){st.resize(1),init(val);}
inline sparse_table(){msz=0,st.resize(1),st[0].resize(_size);}
inline _type_ qry(int l,int r){int k=log2(r-l+1);return oper(st[k][l],st[k][r-(1<<k)+1]);}
};2022-12-20日更新
modint
具体分为static和dynamic两类,static只支持id相同内模数相同;dynamic会有assert检查运算的两类之间模数是否相同,且操作会有点奇怪(尽力惹)。
支持更多整数类型,以及有支持到long long的快速乘,其他内容和acl的modint一样。
template <typename _type_,int id> class static_modint{
static_assert(std::is_integral<_type_>::value&&std::is_signed<_type_>::value,"This type is not supplied");
private:
typedef typename std::make_unsigned<_type_>::type ntp;
ntp a;
static ntp modnum;
public:
const ntp& value=a;
#define tp static_modint<_type_,id>
inline static void setmod(_type_ x){assert(x>0),modnum=x;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline static tp raw(T x){tp ta;ta.a=x;return ta;}
constexpr inline static_modint(){a=0;}
constexpr inline static_modint(const tp& x){a=x.a;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
constexpr inline static_modint(T x){x%=modnum,a=(x<0?x+modnum:x);}
constexpr inline ntp val(){return a;}
inline void operator =(const tp& x){a=x.a;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline void operator =(T x){*this=tp(x);}
constexpr inline tp operator +(const tp& x)const{return raw(a+x.a>modnum?a+x.a-modnum:a+x.a);}
constexpr inline tp operator -(const tp& x)const{return raw(a-x.a>modnum?a-x.a+modnum:a-x.a);}
constexpr inline tp operator +()const{return *this;}
constexpr inline tp operator -()const{return tp()-*this;}
constexpr inline tp operator *(const tp& x)const{return (_type_)(a*x.a-(ntp)((long double)a/modnum*x.a+0.5L)*modnum);}
constexpr inline tp& operator ++(){return ((++a)==modnum?a=0:0),*this;}
constexpr inline tp& operator --(){return ((!a)?a=modnum:0),--a,*this;}
constexpr inline tp operator ++(int){tp res=*this;++*this;return res;}
constexpr inline tp operator --(int){tp res=*this;--*this;return res;}
constexpr inline tp& operator +=(const tp& x){return *this=*this+x,*this;}
constexpr inline tp& operator -=(const tp& x){return *this=*this-x,*this;}
constexpr inline tp& operator *=(const tp& x){return *this=*this*x,*this;}
template <typename T> constexpr inline tp pow(T y){tp res(1),x=*this;for(;y;y>>=1,x*=x) if(y&1) res*=x;return res;}
inline tp inv(){
auto _my_gcd=[](_type_ a,_type_ b)->pair<_type_,_type_> {
if(a==0) return {b,0};
_type_ x=b,y=a,r1=1,r2=0;
while(y){
_type_ u=x/y;
x-=y*u,r2-=r1*u,swap(x,y),swap(r1,r2);
}
if(r2<0) r2+=b/x;
return {x,r2};
};
auto res=_my_gcd(a,modnum);
assert(res.first==1);
return raw(res.second);
}
constexpr inline tp operator / (const tp& x)const{return *this*x.inv();}
constexpr inline tp& operator /= (const tp& x){return *this=*this*x.inv(),*this;}
#undef tp
};
template<typename _type_,int id> typename std::make_unsigned<_type_>::type static_modint<_type_,id>::modnum=0;
template <typename _type_> class dynamic_modint{
static_assert(std::is_integral<_type_>::value&&std::is_signed<_type_>::value,"This type is not supplied");
private:
typedef typename std::make_unsigned<_type_>::type ntp;
ntp a,modnum;
public:
const ntp &value=a;
#define tp dynamic_modint<_type_>
inline void setmod(_type_ x){assert(x>0),modnum=x;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline tp raw(T x){tp ta;ta.a=x,ta.modnum=modnum;return ta;}
constexpr inline dynamic_modint(){a=0;}
constexpr inline dynamic_modint(const tp &x){a=x.a,modnum=x.modnum;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
constexpr inline dynamic_modint(T x){x%=modnum,a=(x<0?x+modnum:x);}
constexpr inline ntp val(){return a;}
inline void operator =(const tp& x){*this=x;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline void operator =(T x){x%=modnum,a=(x<0?x+modnum:x);}
constexpr inline tp operator +(const tp& x)const{return assert(modnum==x.modnum),raw(a+x.a>modnum?a+x.a-modnum:a+x.a);}
constexpr inline tp operator -(const tp& x)const{return assert(modnum==x.modnum),raw(a-x.a>modnum?a-x.a+modnum:a-x.a);}
constexpr inline tp operator +()const{return *this;}
constexpr inline tp operator -()const{return tp()-*this;}
constexpr inline tp operator *(const tp& x)const{return assert(modnum==x.modnum),(_type_)(a*x.a-(ntp)((long double)a/modnum*x.a+0.5L)*modnum);}
constexpr inline tp& operator ++(){return ((++a)==modnum?a=0:0),*this;}
constexpr inline tp& operator --(){return ((!a)?a=modnum:0),--a,*this;}
constexpr inline tp operator ++(int){tp res=*this;++*this;return res;}
constexpr inline tp operator --(int){tp res=*this;--*this;return res;}
constexpr inline tp& operator +=(const tp& x){return *this=*this+x,*this;}
constexpr inline tp& operator -=(const tp& x){return *this=*this-x,*this;}
constexpr inline tp& operator *=(const tp& x){return *this=*this*x,*this;}
template <typename T> constexpr inline tp pow(T y){tp res(1),x=*this;for(;y;y>>=1,x*=x) if(y&1) res*=x;return res;}
inline tp inv(){
auto _my_gcd=[](_type_ a,_type_ b)->pair<_type_,_type_> {
if(a==0) return {b,0};
_type_ x=b,y=a,r1=1,r2=0;
while(y){
_type_ u=x/y;
x-=y*u,r2-=r1*u,swap(x,y),swap(r1,r2);
}
if(r2<0) r2+=b/x;
return {x,r2};
};
auto res=_my_gcd(a,modnum);
assert(res.first==1);
return raw(res.second);
}
constexpr inline tp operator / (const tp& x)const{return *this*x.inv();}
constexpr inline tp& operator /= (const tp& x){return *this=*this*x.inv(),*this;}
#undef tp
};
template<typename _type_> using mint=static_modint<_type_,-1>;2022-12-26 更新
将st表中的初始_size值塞回里面,不参与初始vector的size更改。
以防毒瘤卡空间
template<typename _type_,class _operator=maximize<int> > class sparse_table{
private:
std::vector<std::vector<_type_> > st;
int msz,_size;
_operator oper;
public:
inline _type_& operator [](int x){return st[0][x];}
inline const _type_ operator [](int x)const{return st[0][x];}
inline _type_& at(int x){return st.at(0).at(x);}
inline const _type_ at(int x)const{return st.at(0).at(x);}
inline void resize(int x){_size=x,st[0].resize(_size);}
inline void init(std::vector<_type_> val){
st[0]=val,st[0].resize(_size);
int len=val.size();
msz=log2(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(int len){
msz=log2(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(){init(_size);}
inline void clear(){std::vector<std::vector<_type_> >().swap(st),st.resize(1),st[0].resize(_size),msz=0;}
inline sparse_table(std::vector<_type_> val){st.resize(1),init(val);}
inline sparse_table(){msz=0,_size=0,st.resize(1);}
inline sparse_table(int x){msz=0,_size=x,st.resize(1),st[0].resize(_size);}
inline _type_ qry(int l,int r){int k=log2(r-l+1);return oper(st[k][l],st[k][r-(1<<k)+1]);}
};
2022-12-27 更新
还是ST表
为了效率,我们将log2这个玩意换成了更快的 31-builtin_clz(len),但是MSVC用不了,笑(自己改预定义宏吧,不想无谓的加代码
template<typename _type_,class _operator=maximize<int> > class sparse_table{
private:
std::vector<std::vector<_type_> > st;
int msz,_size;
_operator oper;
public:
inline _type_& operator [](int x){return st[0][x];}
inline const _type_ operator [](int x)const{return st[0][x];}
inline _type_& at(int x){return st.at(0).at(x);}
inline const _type_ at(int x)const{return st.at(0).at(x);}
inline void clear(){std::vector<std::vector<_type_> >().swap(st),st.resize(1),st[0].resize(_size),msz=0;}
inline void resize(int size__){_size=size__,clear();}
inline void init(std::vector<_type_> val){
st[0]=val,st[0].resize(_size);
int len=val.size();
msz=31-__builtin_clz(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(int len){
msz=31-__builtin_clz(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(){init(_size);}
inline sparse_table(std::vector<_type_> val){st.resize(1),init(val);}
inline sparse_table(){msz=0,_size=0,st.resize(1);}
inline sparse_table(int size__){msz=0,_size=size__,st.resize(1),st[0].resize(_size);}
inline _type_ qry(int l,int r){int k=31-__builtin_clz(r-l+1);return oper(st[k][l],st[k][r-(1<<k)+1]);}
};2023-01-06 大更新
05日预告:全部都有调整!及一个实测基本上比其他模板会快一点点的模板(?)
更新列表:
sparse_table:将构造函数方式修改
template <typename _type_> struct maximize{
constexpr inline _type_ operator()(const _type_ &x,const _type_ &y)const{return x<y?y:x;}
};
template <typename _type_> struct minimize{
constexpr inline _type_ operator()(const _type_ &x,const _type_ &y)const{return x<y?x:y;}
};
template<typename _type_,class _operator=maximize<int> > class sparse_table{
protected:
std::vector<std::vector<_type_> > st;
int msz,_size;
_operator oper;
public:
inline _type_& operator [](int x){return st[0][x];}
inline const _type_ operator [](int x)const{return st[0][x];}
inline _type_& at(int x){return st.at(0).at(x);}
inline const _type_ at(int x)const{return st.at(0).at(x);}
inline void clear(){std::vector<std::vector<_type_> >().swap(st),st.resize(1),st[0].resize(_size),msz=0;}
inline void resize(int size__){_size=size__,clear();}
inline void set_operator(_operator _oper){oper=_oper;}
inline void init(std::vector<_type_> val){
st[0]=val,st[0].resize(_size);
int len=val.size();
msz=31-__builtin_clz(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(int len){
msz=31-__builtin_clz(len);
st.resize(msz+1);
for(int j=1,k;j<=msz;++j){
st[j].resize(k=len-(1<<j)+1);
for(int i=0;i<k;++i) st[j][i]=oper(st[j-1][i],st[j-1][i+(1<<(j-1))]);
}
}
inline void init(){init(_size);}
inline sparse_table(){msz=0,_size=0,st.resize(1);}
inline sparse_table(_operator the_oper):oper(the_oper){msz=0,_size=0,st.resize(1);}
inline sparse_table(std::vector<_type_> val){st.resize(1),init(val);}
inline sparse_table(std::vector<_type_> val,_operator the_oper):oper(the_oper){st.resize(1),init(val);}
inline sparse_table(int size__):msz(0),_size(size__){st.resize(1),st[0].resize(_size);}
inline sparse_table(int size__,_operator the_oper):oper(the_oper),msz(0),_size(size__){st.resize(1),st[0].resize(_size);}
inline _type_ qry(int l,int r){assert(l<=r);int k=31-__builtin_clz(r-l+1);return oper(st[k][l],st[k][r-(1<<k)+1]);}
};modint 添加新的东西,default_modint,原因:类模板static成员变量简直慢出新高度……其实static_modint在ac-library是dynamic_modint,default_modint对应的却是static_modint,你猜诛仙这的dynamic_modint有什么用?
template <typename _type_,typename std::make_unsigned<_type_>::type modnum> class default_modint{
static_assert(std::is_integral<_type_>::value&&std::is_signed<_type_>::value,"This type is not supplied");
protected:
typedef typename std::make_unsigned<_type_>::type ntp;
ntp a;
public:
#define tp default_modint<_type_,modnum>
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline static tp raw(T x){tp ta;ta.a=x;return ta;}
inline default_modint():a(0){}
template <typename T,typename std::enable_if<is_integral<T>::value&&std::is_signed<T>::value>::type* =nullptr>
inline default_modint(T x){x%=modnum,a=(x<0?x+modnum:x);}
template <typename T,typename std::enable_if<is_integral<T>::value&&std::is_unsigned<T>::value>::type* =nullptr>
inline default_modint(T x){a=x%modnum;}
constexpr inline ntp val(){return a;}
constexpr inline tp& operator ++(){return ((++a)==modnum?a=0:0),*this;}
constexpr inline tp& operator --(){return ((!a)?a=modnum:0),--a,*this;}
constexpr inline tp operator ++(int){tp res=*this;++*this;return res;}
constexpr inline tp operator --(int){tp res=*this;--*this;return res;}
constexpr inline tp& operator +=(const tp& x){return a+=x.a,(a>=modnum?a-=modnum:0),*this;}
constexpr inline tp& operator -=(const tp& x){return a-=x.a,(a>=modnum?a+=modnum:0),*this;}
template<class T> constexpr inline tp& operator *=(const T& x){}
template<class T,typename std::enable_if<std::is_same<default_modint<T,modnum>,tp>::value&&std::is_same<_type_,long long>::value>::type* =nullptr>
constexpr inline tp& operator *=(const default_modint<T,modnum>& x){return a=(a*x.a-(ntp)((long double)a/modnum*x.a+0.5L)*modnum),(a>=modnum?a+=modnum:0),*this;}
template<class T,typename std::enable_if<std::is_same<default_modint<T,modnum>,tp>::value>::type* =nullptr>
constexpr inline tp& operator *=(const default_modint<T,modnum>& x){return a=1ull*a*x.a%modnum,*this;}
constexpr inline tp operator +(const tp& x)const{return tp(*this)+=x;}
constexpr inline tp operator -(const tp& x)const{return tp(*this)-=x;}
constexpr inline tp operator +()const{return *this;}
constexpr inline tp operator -()const{return tp()-*this;}
constexpr inline tp operator *(const tp& x)const{return tp(*this)*=x;}
template <typename T> constexpr inline tp pow(T y){tp res(1),x=*this;for(;y;y>>=1,x*=x) if(y&1) res*=x;return res;}
inline tp inv(){
_type_ x=modnum,y=a,r1=1,r2=0;
if(a!=0){
while(y){
_type_ u=x/y;
x-=y*u,r2-=r1*u,swap(x,y),swap(r1,r2);
}
if(r2<0) r2+=modnum/x;
}
assert(x==1);
return raw(r2);
}
constexpr inline tp& operator /=(const tp& x){return *this*=x.inv(),*this;}
constexpr inline tp operator /(const tp& x)const{return tp(*this)/=x;}
constexpr inline bool operator ==(const tp& x){return a==x.a;}
constexpr inline bool operator !=(const tp& x){return a!=x.a;}
#undef tp
};
template <typename _type_,int id> class static_modint{
static_assert(std::is_integral<_type_>::value&&std::is_signed<_type_>::value,"This type is not supplied");
protected:
typedef typename std::make_unsigned<_type_>::type ntp;
ntp a;
static ntp modnum;
public:
#define tp static_modint<_type_,id>
inline static void setmod(_type_ x){assert(x>0),modnum=x;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline static tp raw(T x){tp ta;ta.a=x;return ta;}
inline static_modint():a(0){}
template <typename T,typename std::enable_if<is_integral<T>::value&&std::is_signed<T>::value>::type* =nullptr>
inline static_modint(T x){x%=modnum,a=(x<0?x+modnum:x);}
template <typename T,typename std::enable_if<is_integral<T>::value&&std::is_unsigned<T>::value>::type* =nullptr>
inline static_modint(T x){a=x%modnum;}
constexpr inline ntp val(){return a;}
constexpr inline tp& operator ++(){return ((++a)==modnum?a=0:0),*this;}
constexpr inline tp& operator --(){return ((!a)?a=modnum:0),--a,*this;}
constexpr inline tp operator ++(int){tp res=*this;++*this;return res;}
constexpr inline tp operator --(int){tp res=*this;--*this;return res;}
constexpr inline tp& operator +=(const tp& x){return a+=x.a,(a>=modnum?a-=modnum:0),*this;}
constexpr inline tp& operator -=(const tp& x){return a-=x.a,(a>=modnum?a+=modnum:0),*this;}
template<class T> constexpr inline tp& operator *=(const T& x){}
template<class T,typename std::enable_if<std::is_same<static_modint<T,id>,tp>::value&&std::is_same<_type_,long long>::value>::type* =nullptr>
constexpr inline tp& operator *=(const static_modint<T,id>& x){return a=(a*x.a-(ntp)((long double)a/modnum*x.a+0.5L)*modnum),(a>=modnum?a+=modnum:0),*this;}
template<class T,typename std::enable_if<std::is_same<static_modint<T,id>,tp>::value>::type* =nullptr>
constexpr inline tp& operator *=(const static_modint<T,id>& x){return a=1ull*a*x.a%modnum,*this;}
constexpr inline tp operator +(const tp& x)const{return tp(*this)+=x;}
constexpr inline tp operator -(const tp& x)const{return tp(*this)-=x;}
constexpr inline tp operator +()const{return *this;}
constexpr inline tp operator -()const{return tp()-*this;}
constexpr inline tp operator *(const tp& x)const{return tp(*this)*=x;}
template <typename T> constexpr inline tp pow(T y){tp res(1),x=*this;for(;y;y>>=1,x*=x) if(y&1) res*=x;return res;}
inline tp inv(){
_type_ x=modnum,y=a,r1=1,r2=0;
if(a!=0){
while(y){
_type_ u=x/y;
x-=y*u,r2-=r1*u,swap(x,y),swap(r1,r2);
}
if(r2<0) r2+=modnum/x;
}
assert(x==1);
return raw(r2);
}
constexpr inline tp& operator /=(const tp& x){return *this*=x.inv(),*this;}
constexpr inline tp operator /(const tp& x)const{return tp(*this)/=x;}
constexpr inline bool operator ==(const tp& x){return a==x.a;}
constexpr inline bool operator !=(const tp& x){return a!=x.a;}
#undef tp
};
template<typename _type_,int id> typename std::make_unsigned<_type_>::type static_modint<_type_,id>::modnum=0;
template <typename _type_> class dynamic_modint{
static_assert(std::is_integral<_type_>::value&&std::is_signed<_type_>::value,"This type is not supplied");
protected:
typedef typename std::make_unsigned<_type_>::type ntp;
ntp a,modnum;
public:
#define tp dynamic_modint<_type_>
inline void setmod(_type_ x){assert(x>0),modnum=x;}
template <typename T,typename std::enable_if<std::is_integral<T>::value>::type* =nullptr>
inline tp raw(T x){tp ta;ta.a=x,ta.modnum=modnum;return ta;}
inline dynamic_modint():a(0){}
template <typename T,typename std::enable_if<is_integral<T>::value&&std::is_signed<T>::value>::type* =nullptr>
inline dynamic_modint(T x){x%=modnum,a=(x<0?x+modnum:x);}
template <typename T,typename std::enable_if<is_integral<T>::value&&std::is_unsigned<T>::value>::type* =nullptr>
inline dynamic_modint(T x){a=x%modnum;}
constexpr inline ntp val(){return a;}
constexpr inline tp& operator ++(){return ((++a)==modnum?a=0:0),*this;}
constexpr inline tp& operator --(){return ((!a)?a=modnum:0),--a,*this;}
constexpr inline tp operator ++(int){tp res=*this;++*this;return res;}
constexpr inline tp operator --(int){tp res=*this;--*this;return res;}
constexpr inline tp& operator +=(const tp& x){return assert(modnum==x.modnum),a+=x.a,(a>=modnum?a-=modnum:0),*this;}
constexpr inline tp& operator -=(const tp& x){return assert(modnum==x.modnum),a-=x.a,(a>=modnum?a+=modnum:0),*this;}
template<class T> constexpr inline tp& operator *=(const T& x){}
template<class T,typename std::enable_if<std::is_same<T,_type_>::value&&std::is_same<_type_,long long>::value>::type* =nullptr>
constexpr inline tp& operator *=(const dynamic_modint<T>& x){
return assert(modnum==x.modnum),a=(a*x.a-(ntp)((long double)a/modnum*x.a+0.5L)*modnum),(a>=modnum?a+=modnum:0),*this;
}
template<class T,typename std::enable_if<std::is_same<T,_type_>::value>::type* =nullptr>
constexpr inline tp& operator *=(const dynamic_modint<T>& x){return assert(modnum==x.modnum),a=1ull*a*x.a%modnum,*this;}
constexpr inline tp operator +(const tp& x)const{return tp(*this)+=x;}
constexpr inline tp operator -(const tp& x)const{return tp(*this)-=x;}
constexpr inline tp operator +()const{return *this;}
constexpr inline tp operator -()const{tp tmp=*this;return tmp.a=0,tmp-*this;}
constexpr inline tp operator *(const tp& x)const{return tp(*this)*=x;}
template <typename T> constexpr inline tp pow(T y){tp res(1),x=*this;for(;y;y>>=1,x*=x) if(y&1) res*=x;return res;}
inline tp inv(){
_type_ x=modnum,y=a,r1=1,r2=0;
if(a!=0){
while(y){
_type_ u=x/y;
x-=y*u,r2-=r1*u,swap(x,y),swap(r1,r2);
}
if(r2<0) r2+=modnum/x;
}
assert(x==1);
return raw(r2);
}
constexpr inline tp& operator /=(const tp& x){return *this*=x.inv(),*this;}
constexpr inline tp operator /(const tp& x)const{return tp(*this)/=x;}
constexpr inline bool operator ==(const tp& x){return a==x.a;}
constexpr inline bool operator !=(const tp& x){return a!=x.a;}
#undef tp
};
template<typename _type_> using mint=static_modint<_type_,-1>;
template<typename _type_,_type_ modnum> using modint=default_modint<_type_,modnum>;新加入数据结构:lazy-tag线段树,类里带beats函数,以及还有一个带线段树上二分的子类。
因为线段树种类太多…就只先写了这些…后面还有什么慢慢加。
template <typename mytp> struct default_empty{
inline mytp operator ()(const mytp &a,const mytp &b)const{return mytp();}
};
template <typename mytp> struct default_pushup_oper{
inline mytp operator ()(const mytp &a,const mytp &b)const{return a+b;}
};
template <typename mytp> struct default_pushdown_oper{
inline mytp operator ()(const mytp &a,const mytp &b)const{return a+b;}
};
template <typename answer_type,typename input_type> struct default_assignment_oper{
inline answer_type operator ()(const answer_type &a,const input_type &b)const{return a+b;}
};
template <typename answer_type,typename input_type
,class pushup_oper=default_pushup_oper<answer_type>
,class pushdown_oper=default_pushdown_oper<input_type>
,class assignment_oper=default_assignment_oper<answer_type,input_type> > class lazy_segment_tree{
protected:
int size_,height;
pushup_oper pushup;
pushdown_oper pushdown;
assignment_oper assign;
answer_type answer_type_e;
input_type input_type_e;
std::vector<answer_type> nodes;
std::vector<input_type> tags;
inline int floor_log2(int x){return 32-__builtin_clz(x);}
inline int ceil_log2(int x){return 32-__builtin_clz(x)+((x&-x)!=x);}
inline void lazy_assign(int ind,input_type x){nodes[ind]=assign(nodes[ind],x),ind<size_?(tags[ind]=pushdown(tags[ind],x),void()):void();}
inline void real_pushdown(int ind){lazy_assign(ind<<1,tags[ind]),lazy_assign(ind<<1|1,tags[ind]),tags[ind]=input_type_e;}
inline void real_pushup(int ind){nodes[ind]=pushup(nodes[ind<<1],nodes[ind<<1|1]);}
template<class unary_predicate_for_answer_t> inline void pushdown_until_all_true(int ind,unary_predicate_for_answer_t pred){
if(ind>=size_||pred(nodes[ind])) return;
if(!pred(nodes[ind<<1])) lazy_assign(ind<<1,tags[ind]);
if(!pred(nodes[ind<<1|1])) lazy_assign(ind<<1|1,tags[ind]);
tags[ind]=input_type_e,real_pushup(ind);
}
inline void all_pushdown(int lid,int rid){
for(int i=height;i>=1;--i){
if((lid>>i)==(rid>>i)) real_pushdown(lid>>i);
else{
if(((lid>>i)<<i)!=lid) real_pushdown(lid>>i);
if(((rid>>i)<<i)!=rid) real_pushdown((rid-1)>>i);
}
}
}
inline void all_pushup(int lid,int rid){
for(int i=1;i<=height;++i){
if((lid>>i)==(rid>>i)) real_pushup(lid>>i);
else{
if(((lid>>i)<<i)!=lid) real_pushup(lid>>i);
if(((rid>>i)<<i)!=rid) real_pushup((rid-1)>>i);
}
}
}
public:
inline answer_type& operator [](int x){return nodes[x+size_];}
inline const answer_type operator [](int x)const{return nodes[x+size_];}
inline answer_type& at(int x){return nodes.at(x+size_);}
inline const answer_type at(int x)const{return nodes.at(x+size_);}
inline void clear(){nodes=std::vector<answer_type>(size_*2,answer_type_e),tags=std::vector<input_type>(size_,input_type_e);}
inline void resize(int x){size_=x,height=floor_log2(size_),clear();}
inline void set_answer_type_e(answer_type answer_type_e_){answer_type_e=answer_type_e_;}
inline void set_input_type_e(input_type input_type_e_){input_type_e=input_type_e_;}
inline void set_pushup_oper(pushup_oper _oper){pushup=_oper;}
inline void set_pushdown_oper(pushdown_oper _oper){pushdown=_oper;}
inline void set_assignment_oper(assignment_oper _oper){assign=_oper;}
inline void build(){
for(int ind=size_-1;ind>0;--ind) nodes[ind]=pushup(nodes[ind<<1],nodes[ind<<1|1]);
}
inline void build(std::vector<input_type> in){
int len=std::min((int)in.size(),size_);
for(int i=0;i<len;++i) nodes[i+size_]=assign(nodes[i+size_],in[i]);
build();
}
explicit inline lazy_segment_tree(){size_=0,height=0,answer_type_e=answer_type(),input_type_e=input_type();}
explicit inline lazy_segment_tree(pushup_oper _oper1,pushdown_oper _oper2,assignment_oper _oper3):
pushup(_oper1),pushdown(_oper2),assign(_oper3){
size_=0,height=0,answer_type_e=answer_type(),input_type_e=input_type();
}
explicit inline lazy_segment_tree(int size__){size_=size__,height=floor_log2(size_),clear();}
explicit inline lazy_segment_tree(std::vector<input_type> in){size_=in.size(),height=floor_log2(size_),clear(),build(in);}
explicit inline lazy_segment_tree(int size__,answer_type answer_type_e_,input_type input_type_e_){
size_=size__,height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear();
}
explicit inline lazy_segment_tree(std::vector<input_type> in,answer_type answer_type_e_,input_type input_type_e_){
size_=in.size(),height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear(),build(in);
}
explicit inline lazy_segment_tree(int size__,answer_type answer_type_e_,input_type input_type_e_
,pushup_oper _oper1,pushdown_oper _oper2,assignment_oper _oper3):
pushup(_oper1),pushdown(_oper2),assign(_oper3){
size_=size__,height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear();
}
explicit inline lazy_segment_tree(std::vector<input_type> in,answer_type answer_type_e_,input_type input_type_e_
,pushup_oper _oper1,pushdown_oper _oper2,assignment_oper _oper3):
pushup(_oper1),pushdown(_oper2),assign(_oper3){
size_=in.size(),height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear(),build(in);
}
inline void update(int l,int r,input_type x){
assert(l>=0&&r<size_&&l<=r);
l+=size_,r+=size_+1,all_pushdown(l,r);
int tl=l,tr=r;
while(tl<tr){
if(tl&1) lazy_assign(tl++,x);
if(tr&1) lazy_assign(--tr,x);
tl>>=1,tr>>=1;
}
all_pushup(l,r);
}
inline void update(int pos,input_type x){
assert(pos>=0&&pos<size_);
pos+=size_;
for(int i=height;i>=1;--i) real_pushdown(pos>>i);
nodes[pos]=assign(nodes[pos],x);
while(pos>>=1) real_pushup(pos);
}
inline void default_update(int pos,answer_type x){
assert(pos>=0&&pos<size_);
pos+=size_;
for(int i=height;i>=1;--i) real_pushdown(pos>>i);
nodes[pos]=x;
while(pos>>=1) real_pushup(pos);
}
template<class unary_predicate_for_answer_t> inline void beats_update(int l,int r,input_type x,unary_predicate_for_answer_t pred){
assert(l>=0&&r<size_&&l<=r);
l+=size_,r+=size_+1,all_pushdown(l,r);
int tl=l,tr=r;
while(tl<tr){
if(tl&1) lazy_assign(tl,x),pushdown_until_all_true(tl++,pred);
if(tr&1) lazy_assign(--tr,x),pushdown_until_all_true(tr,pred);
tl>>=1,tr>>=1;
}
all_pushup(l,r);
}
constexpr inline answer_type query_all()const{return nodes[1];}
inline answer_type query(int l,int r){
assert(l>=0&&r<size_);
if(r<l) return answer_type_e;
l+=size_,r+=size_+1,all_pushdown(l,r);
answer_type resl=answer_type_e,resr=answer_type_e;
while(l<r){
if(l&1) resl=pushup(resl,nodes[l++]);
if(r&1) resr=pushup(nodes[--r],resr);
l>>=1,r>>=1;
}
return pushup(resl,resr);
}
inline answer_type query(int pos){
assert(pos>=0&&pos<size_);
pos+=size_;
for(int i=height;i>=1;--i) real_pushdown(pos>>i);
return nodes[pos];
}
};
#define lazy_segtree_template lazy_segment_tree<answer_type,input_type,pushup_oper,pushdown_oper,assignment_oper>
template <typename answer_type,typename input_type
,class pushup_oper=default_pushup_oper<answer_type>
,class pushdown_oper=default_empty<input_type>
,class assignment_oper=default_assignment_oper<answer_type,input_type> > class lazy_and_binary_search_segment_tree:
public lazy_segtree_template{
private:
using p_=lazy_segtree_template;
using p_::size_;
using p_::height;
using p_::pushup;
using p_::pushdown;
using p_::assign;
using p_::answer_type_e;
using p_::input_type_e;
using p_::nodes;
using p_::tags;
using p_::real_pushdown;
using p_::floor_log2;
using p_::ceil_log2;
public:
using p_::clear;
using p_::build;
inline void resize(int x){size_=1<<ceil_log2(x),height=floor_log2(size_),clear();}
explicit inline lazy_and_binary_search_segment_tree(){size_=0,height=0,answer_type_e=answer_type(),p_::input_type_e=input_type();}
explicit inline lazy_and_binary_search_segment_tree(pushup_oper _oper1,pushdown_oper _oper2,assignment_oper _oper3):p_(_oper1,_oper2,_oper3){
size_=0,height=0,answer_type_e=answer_type(),input_type_e=input_type();
}
explicit inline lazy_and_binary_search_segment_tree(int size__){size_=1<<ceil_log2(size__),height=floor_log2(size_),clear();}
explicit inline lazy_and_binary_search_segment_tree(std::vector<input_type> in){size_=1<<ceil_log2(in.size()),height=floor_log2(size_),clear(),build(in);}
explicit inline lazy_and_binary_search_segment_tree(int size__,answer_type answer_type_e_,input_type input_type_e_){
size_=1<<ceil_log2(size__),height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear();
}
explicit inline lazy_and_binary_search_segment_tree(std::vector<input_type> in,answer_type answer_type_e_,input_type input_type_e_){
size_=1<<ceil_log2(in.size()),height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear(),build(in);
}
explicit inline lazy_and_binary_search_segment_tree(int size__,answer_type answer_type_e_,input_type input_type_e_
,pushup_oper _oper1,pushdown_oper _oper2,assignment_oper _oper3):p_(_oper1,_oper2,_oper3){
size_=1<<ceil_log2(size__),height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear();
}
explicit inline lazy_and_binary_search_segment_tree(std::vector<input_type> in,answer_type answer_type_e_,input_type input_type_e_
,pushup_oper _oper1,pushdown_oper _oper2,assignment_oper _oper3):p_(_oper1,_oper2,_oper3){
size_=1<<ceil_log2(in.size()),height=floor_log2(size_),answer_type_e=answer_type_e_,input_type_e=input_type_e_,clear(),build(in);
}
template<class unary_predicate_for_answer_t> inline int min_left(int pos,unary_predicate_for_answer_t pred){
assert(pos>=0&&pos<size_);
if(!pred(nodes[pos])) return -1;
if(pos==0) return 0;
pos+=size_;
for(int i=height;i>=1;--i) real_pushdown(pos>>i);
answer_type tmp=answer_type_e;
while((pos&-pos)!=pos){
while((pos&1)&&pos>1) pos>>=1;
answer_type &&t1=pushup(nodes[pos],tmp);
if(!pred(t1)){
while(pos<size_){
real_pushdown(pos),pos=pos<<1|1;
answer_type &&t2=pushup(nodes[pos],tmp);
if(pred(t2)) tmp=t2,--pos;
}
return pos-size_+1;
}
tmp=t1,--pos;
}
return 0;
}
template<class unary_predicate_for_answer_t> inline int max_right(int pos,unary_predicate_for_answer_t pred){
assert(pos>=0&&pos<size_);
if(!pred(nodes[pos])) return -1;
if(pos==size_-1) return size_-1;
pos+=size_;
for(int i=height;i>=1;--i) real_pushdown(pos>>i);
answer_type tmp=answer_type_e;
while(((~pos)&-(~pos))!=pos+1){
while((pos&1)==0) pos>>=1;
answer_type &&t1=pushup(tmp,nodes[pos]);
if(!pred(t1)){
while(pos<size_){
real_pushdown(pos),pos=pos<<1;
answer_type &&t2=pushup(tmp,nodes[pos]);
if(pred(t2)) tmp=t2,++pos;
}
return pos-size_-1;
}
tmp=t1,++pos;
}
return size_-1;
}
};
#undef lazy_segtree_templateps:诛仙决定每周至少更新一次