#include<bits/stdc++.h>
using namespace std;
#define ls p<<1
#define rs p<<1|1
#define lson ls,l,mid
#define rson rs,mid+1,r
#define root 1,1,n
const int N = 1e6+9,mod = 1e9+7;
typedef long long ll;
int n,m,a[N];
struct tree
{
int l,r,add,sum;
tree()//需要写 不写不对 不知道为什么
{
l = r = sum = add = 0;
}
void col(int v)//打标记
{
}
}t[N<<2];
tree operator +(const tree &l,const tree &r)
{
tree p;
p.l = l.l;
p.r = r.r;
p.add = 0;//需要写 不写会乱标记
p.sum = l.sum + r.sum;
return p;
}
void pushup(int p)
{
t[p] = t[ls] + t[rs];
}
void pushdown(int p)
{
int v = t[p].add;
if(v)
{
t[ls].col(v);
t[rs].col(v);
t[p].add = 0;
}
}
void build(int p,int l,int r)
{
if(l == r)
{
t[p].l = t[p].r = l;
t[p].col(a[l]);
return;
}
int mid = (l+r)>>1;
build(ls,l,mid);
build(rs,mid+1,r);
pushup(p);
}
void modify(int p,int l,int r,int L,int R,int v)
{
if(L <= l && R >= r)
{
t[p].col(v);
return;
}
pushdown(p);
int mid = l+r>>1;
if(l <= mid)modify(lson,L,R,v);
if(r > mid)modify(rson,L,R,v);
pushup(p);
}
tree query(int p,int l,int r,int L,int R)
{
if(L <= l && R >= r)return t[p];
pushdown(p);
int mid = l+r>>1;
if(r <= mid)return query(lson,L,R);
else if(l > mid)return query(rson,L,R);
else return query(lson,L,R) + query(rson,L,R);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i = 1;i <= n;i++)scanf("%d",a+i);
build(root);
while(m--)
{
int op,l,r,v;
scanf("%d%d%d",&op,&l,&r);
if(op == 1)
{
scanf("%d",&v);
modify(root,l,r,v);
}
else if(op == 2)
{
printf("%d",query(root,l,r).sum);
}
}
return 0;
}
标签:return,int,rson,mid,ls,query,模板
From: https://www.cnblogs.com/AC7-/p/17725938.html