#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
namespace ois
{
#define QuickArray SegmentTree
template<typename ValueT>
/*
* Type 'ValueT' Must Define:
* Operator +;
* Operator =;
*/
class SegmentTree
{
private:
#define IndexT int
struct SgtNode
{
IndexT left;
IndexT right;
ValueT sum;
ValueT lazy;
SgtNode(IndexT l=0,IndexT r=0,ValueT s=ValueT{},ValueT lz=ValueT{}):left{l},right{r},sum{s},lazy{lz}{}
~SgtNode(){}
void clear()
{
left=0;
right=0;
sum=ValueT{};
lazy=ValueT{};
}
IndexT size() const{return right-left+1;}
};
private:
#define MAX_SIZE 100000
#define ROOT 1
#define mb ((l+r)>>1)
#define mid ((nodes[id].left+nodes[id].right)>>1)
#define lc (id<<1)
#define rc (id<<1|1)
#define ns (4*theSize+4)
#define as (theSize+1)
public:
SegmentTree(){init();}
SegmentTree(const IndexT &n)
{
theSize=n;
nodes.resize(ns);
array.resize(as);
build(1,1,n,array);
}
SegmentTree(const IndexT &n,ValueT *v)
{
theSize=n;
nodes.resize(ns);
array.resize(as);
build(1,1,n,v);
}
SegmentTree(const SegmentTree &st)
{
theSize=st.theSize;
nodes.resize(ns);
array.resize(as);
std::memcpy(nodes,st.nodes,sizeof(nodes));
}
~SegmentTree(){}
SegmentTree& operator=(const SegmentTree &st)
{
theSize=st.theSize;
nodes.resize(ns);
array.resize(as);
std::memcpy(nodes,st.nodes,sizeof(nodes));
return *this;
}
public:
IndexT size() const{return theSize;}
IndexT maxSize() const{return MAX_SIZE;}
IndexT root() const{return ROOT;}
bool empty() const{return theSize==0;}
int depth() const{return (int)(log2(theSize))+2;}
void clear(){init();}
public:
void add(const IndexT &l,const IndexT &r,const ValueT &a){baseAdd(ROOT,l,r,a);}
void add(const IndexT &r,const ValueT &v){baseAdd(ROOT,r,r,v);}
void add(const ValueT &v){baseAdd(1,1,theSize,v);}
ValueT sum(const IndexT &l,const IndexT &r){return baseSum(ROOT,l,r);}
ValueT operator[](const IndexT &r){return baseSum(ROOT,r,r);}
ValueT sum(){return baseSum(1,1,theSize);}
std::vector<ValueT> toVector()
{
std::vector<ValueT> v{};
v.push_back(ValueT{});
for(IndexT i=1;i<=theSize;i++){v.push_back(baseSum(1,i,i));}
return v;
}
private:
void update(const IndexT &id){nodes[id].sum=nodes[lc].sum+nodes[rc].sum;}
void addLazy(const IndexT &id,const IndexT a)
{
nodes[id].lazy=nodes[id].lazy+a;
nodes[id].sum=nodes[id].sum+nodes[id].size()*a;
}
void pushDown(const IndexT &id)
{
if(!nodes[id].lazy){return;}
addLazy(lc,nodes[id].lazy);
addLazy(rc,nodes[id].lazy);
nodes[id].lazy=ValueT{};
}
private:
std::vector<SgtNode> nodes;
std::vector<ValueT> array;
IndexT theSize;
void init()
{
theSize=0;
for(auto x:array){x=ValueT{};}
for(auto x:nodes){x.clear();}
}
void build(const IndexT &id,const IndexT &l,const IndexT &r,ValueT *v)
{
nodes[id]=SgtNode{l,r,ValueT{},ValueT{}};
if(l==r)
{
nodes[id].sum=v[l];
return;
}
build(lc,l,mb,v);
build(rc,mb+1,r,v);
update(id);
}
void baseAdd(const IndexT &id,const IndexT &l,const IndexT &r,const ValueT &a)
{
if(nodes[id].left==l && nodes[id].right==r)
{
addLazy(id,a);
return;
}
pushDown(id);
if(l>mid){baseAdd(rc,l,r,a);}
else if(r<=mid){baseAdd(lc,l,r,a);}
else
{
baseAdd(lc,l,mid,a);
baseAdd(rc,mid+1,r,a);
}
update(id);
}
ValueT baseSum(const IndexT &id,const IndexT &l,const IndexT &r)
{
if(nodes[id].left==l && nodes[id].right==r){return nodes[id].sum;}
ValueT res{};
pushDown(id);
if(l>mid){res=res+baseSum(rc,l,r);}
else if(r<=mid){res=res+baseSum(lc,l,r);}
else{res=res+baseSum(lc,l,mid)+baseSum(rc,mid+1,r);}
return res;
}
private:
#undef IndexT
#undef MAX_SIZE
#undef ROOT
#undef mb
#undef mid
#undef lc
#undef rc
#undef ns
#undef as
};
}剪贴板
剪贴板 f9nbxm8z
剪贴板信息
剪贴板信息
- 收录
- 更新