线段树是一种维护区间和等功能的数据结构
struct SegTree{
struct node{
int l,r,sum,len,laz;
}tree[N<<2];
inline void pushdown(int u){
int lson=u<<1,rson=lson|1;
tree[lson].laz+=tree[u].laz;
tree[rson].laz+=tree[u].laz;
tree[lson].sum=(tree[lson].sum+tree[lson].len*tree[u].laz)%mod;
tree[rson].sum=(tree[rson].sum+tree[rson].len*tree[u].laz)%mod;
tree[u].laz=0;
}
void build(int u,int l,int r,int w[]){
tree[u].l=l,tree[u].r=r,tree[u].len=r-l+1,tree[u].sum=tree[u].laz=0;
if(l==r){
tree[u].sum=w[l];
if(tree[u].sum>=mod)tree[u].sum%=mod;
return;
}
int mid=(l+r)>>1,lson=u<<1,rson=lson|1;
build(lson,l,mid,w);
build(rson,mid+1,r,w);
tree[u].sum=(tree[lson].sum+tree[rson].sum)%mod;
}
void update(int u,int l,int r,int k){
printf("<%d %d %d %d>",u,l,r,k);
if(l<=tree[u].l&&tree[u].r<=r){
tree[u].laz+=k;
tree[u].sum=(tree[u].sum+k*tree[u].len)%mod;
return;
}
else{
if(tree[u].laz)pushdown(u);
int mid=(tree[u].l+tree[u].r)>>1,lson=u<<1,rson=lson|1;
if(l<=mid)update(lson,l,r,k);
if(r>mid)update(rson,l,r,k);
tree[u].sum=(tree[lson].sum+tree[rson].sum)%mod;
}
}
int query(int u,int l,int r){
if(l<=tree[u].l && tree[u].r<=r){
return tree[u].sum;
}
else{
int res=0,mid=(tree[u].l+tree[u].r)>>1,lson=u<<1,rson=lson|1;
if(tree[u].laz)pushdown(u);
if(l<=mid)res=(res+query(lson,l,r))%mod;
if(r>mid)res=(res+query(rson,l,r))%mod;
return res;
}
}
void print(int u){
if(tree[u].l==tree[u].r)printf("%d ",tree[u].sum);
else {
int lson=u<<1,rson=lson|1;
print(lson);
print(rson);
}
}
}tr;