傻逼线段树
#include <iostream>
#include <vector>
#include <string>
class Segment_TREE
{
using data_type = long long;
protected:
struct node
{
int l, r;
int ls, rs;
data_type lztag, sum;
node(int l_ = 0, int r_ = 0, int ls_ = 0, int rs_ = 0, const data_type &lztag_ = 0, const data_type &sum_ = 0) : l(l_), r(r_), ls(ls_), rs(rs_), lztag(lztag_), sum(sum_) {}
};
std::vector<node> tr;
int root;
public:
void pushdown(const int p)
{
if (tr[p].ls)
{
tr[tr[p].ls].lztag += tr[p].lztag;
tr[tr[p].ls].sum += tr[p].lztag * (tr[tr[p].ls].r - tr[tr[p].ls].l + 1);
}
if (tr[p].rs)
{
tr[tr[p].rs].lztag += tr[p].lztag;
tr[tr[p].rs].sum += tr[p].lztag * (tr[tr[p].rs].r - tr[tr[p].rs].l + 1);
}
tr[p].lztag = 0;
return;
}
void pushup(const int p)
{
tr[p].sum = 0;
if (tr[p].ls)
tr[p].sum += tr[tr[p].ls].sum;
if (tr[p].rs)
tr[p].sum += tr[tr[p].rs].sum;
return;
}
void add(const int p, const int l1, const int r1, const data_type x)
{
if (l1 > tr[p].r || r1 < tr[p].l)
return;
if (l1 <= tr[p].l && tr[p].r <= r1)
{
tr[p].lztag += x;
tr[p].sum += x * (tr[p].r - tr[p].l + 1);
return;
}
pushdown(p);
if (tr[p].ls)
add(tr[p].ls, l1, r1, x);
if (tr[p].rs)
add(tr[p].rs, l1, r1, x);
pushup(p);
return;
}
void add(const int l1, const int r1, const data_type x)
{
return add(root, l1, r1, x);
}
data_type query(const int p, const int l1, const int r1)
{
if (l1 > tr[p].r || r1 < tr[p].l)
return 0;
if (l1 <= tr[p].l && tr[p].r <= r1)
{
return tr[p].sum;
}
data_type ans = 0;
pushdown(p);
if (tr[p].ls)
ans += query(tr[p].ls, l1, r1);
if (tr[p].rs)
ans += query(tr[p].rs, l1, r1);
// pushup();
return ans;
}
data_type query(const int l1, const int r1)
{
return query(root, l1, r1);
}
int get_new_node(const node &obj = node())
{
if (tr.empty())
tr.push_back(node()); // 防止出现 0
tr.push_back(obj);
return tr.size() - 1;
}
void build(const int p)
{
if (tr[p].l == tr[p].r)
return;
int mid = (tr[p].l + tr[p].r) >> 1;
tr[p].ls = get_new_node(node(tr[p].l, mid, 0, 0, 0, 0));
build(tr[p].ls);
tr[p].rs = get_new_node(node(mid + 1, tr[p].r, 0, 0, 0, 0));
build(tr[p].rs);
// pushdown();
pushup(p);
return;
}
void build(const int p, const data_type x[])
{
if (tr[p].l == tr[p].r)
{
tr[p].sum = x[tr[p].r];
return;
}
int mid = (tr[p].l + tr[p].r) >> 1;
tr[p].ls = get_new_node(node(tr[p].l, mid, 0, 0, 0, 0));
build(tr[p].ls, x);
tr[p].rs = get_new_node(node(mid + 1, tr[p].r, 0, 0, 0, 0));
build(tr[p].rs, x);
// pushdown();
pushup(p);
return;
}
Segment_TREE(const int l_ = 0, const int r_ = 0)
{
build(root = get_new_node(node(l_, r_, 0, 0, 0, 0)));
}
Segment_TREE(const int l_, const int r_, const data_type x[])
{
build(root = get_new_node(node(l_, r_, 0, 0, 0, 0)), x);
}
} segtree;
const int maxn = 1e5;
long long a[maxn + 1];
int n, m;
int opt;
int main()
{
std::cin >> n >> m;
for (int i = 1; i <= n; i++)
{
std::cin >> a[i];
}
segtree = Segment_TREE(1, n, a);
int l1, r1;
long long x;
while (std::cin >> opt)
{
if (opt == 1)
{
std::cin >> l1 >> r1 >> x;
segtree.add(l1, r1, x);
}
if (opt == 2)
{
std::cin >> l1 >> r1;
std::cout << segtree.query(l1, r1) << std::endl;
}
}
return 0;
}#include<iostream>
#include<string>
class Segment_TREE_NODE
{
using data_type=long long;
private:
int l,r;
Segment_TREE_NODE *ls,*rs;
data_type lztag,sum;
public:
void pushdown()
{
if(ls!=nullptr)
{
ls->lztag+=lztag;
ls->sum+=lztag*(ls->r-ls->l+1);
}
if(rs!=nullptr)
{
rs->lztag+=lztag;
rs->sum+=lztag*(rs->r-rs->l+1);
}
lztag=0;
return;
}
void pushup()
{
sum=0;
if(ls!=nullptr)sum+=ls->sum;
if(rs!=nullptr)sum+=rs->sum;
return;
}
void add(const int l1,const int r1,const data_type x)
{
if(l1>r||r1<l)return;
if(l1<=l&&r<=r1)
{
lztag+=x;
sum+=x*(r-l+1);
return;
}
pushdown();
if(ls!=nullptr)
ls->add(l1,r1,x);
if(rs!=nullptr)
rs->add(l1,r1,x);
pushup();
return;
}
data_type query(const int l1,const int r1)
{
if(l1>r||r1<l)return 0;
if(l1<=l&&r<=r1)
{
return sum;
}
data_type ans=0;
pushdown();
if(ls!=nullptr)
ans+=ls->query(l1,r1);
if(rs!=nullptr)
ans+=rs->query(l1,r1);
//pushup();
return ans;
}
Segment_TREE_NODE(const int l_=0,const int r_=0):
l(l_),r(r_),lztag(0),sum(0),ls(nullptr),rs(nullptr)
{
if(l==r)return;
int mid=(l+r)>>1;
ls=new Segment_TREE_NODE(l,mid);
rs=new Segment_TREE_NODE(mid+1,r);
//pushdown();
pushup();
return;
}
Segment_TREE_NODE(const int l_,const int r_,const data_type x[]):
l(l_),r(r_),lztag(0),sum(0),ls(nullptr),rs(nullptr)
{
if(l==r)
{
sum=x[r];
return;
}
int mid=(l+r)>>1;
ls=new Segment_TREE_NODE(l,mid,x);
rs=new Segment_TREE_NODE(mid+1,r,x);
//pushdown();
pushup();
return;
}
Segment_TREE_NODE(Segment_TREE_NODE &&obj):
l(obj.l),r(obj.r)
,lztag(obj.lztag),sum(obj.sum)
{
if(ls!=nullptr)delete ls;
if(rs!=nullptr)delete rs;
ls=obj.ls;
obj.ls=nullptr;
rs=obj.rs;
obj.rs=nullptr;
}
Segment_TREE_NODE(const Segment_TREE_NODE &obj):
l(obj.l),r(obj.r),lztag(obj.lztag),sum(obj.sum)
{
if(ls!=nullptr)delete ls;
ls=nullptr;
if(rs!=nullptr)delete rs;
rs=nullptr;
if(obj.ls!=nullptr)
ls=new Segment_TREE_NODE(*(obj.ls));
if(obj.rs!=nullptr)
rs=new Segment_TREE_NODE(*(obj.rs));
return;
}
Segment_TREE_NODE& operator=(const Segment_TREE_NODE &obj)
{
if(this==&obj)return *this;
l=(obj.l),r=(obj.r),lztag=(obj.lztag),sum=(obj.sum);
if(ls!=nullptr)delete ls;
ls=nullptr;
if(rs!=nullptr)delete rs;
rs=nullptr;
if(obj.ls!=nullptr)
ls=new Segment_TREE_NODE(*(obj.ls));
if(obj.rs!=nullptr)
rs=new Segment_TREE_NODE(*(obj.rs));
return *this;
}
Segment_TREE_NODE& operator=(Segment_TREE_NODE &&obj)
{
l=(obj.l),r=(obj.r),lztag=(obj.lztag),sum=(obj.sum);
if(ls!=nullptr)delete ls;ls=nullptr;
if(rs!=nullptr)delete rs;rs=nullptr;
ls=obj.ls;obj.ls=nullptr;
rs=obj.rs;obj.rs=nullptr;
return *this;
}
~Segment_TREE_NODE()
{
if(ls!=nullptr)delete ls;
if(rs!=nullptr)delete rs;
return;
}
}root1,root;
const int maxn=1e5;
long long a[maxn+1];
int n,m;
int opt;
int main()
{
std::cin>>n>>m;
for(int i=1;i<=n;i++)
{
std::cin>>a[i];
}
root1=Segment_TREE_NODE(1,n,a);
root=root1;
int l1,r1;
long long x;
while(std::cin>>opt)
{
if(opt==1)
{
std::cin>>l1>>r1>>x;
root.add(l1,r1,x);
}
if(opt==2)
{
std::cin>>l1>>r1;
std::cout<<root.query(l1,r1)<<std::endl;
}
root=root;
}
return 0;
}