维护区间和,两个操作:一个是将某个区间设置成一个值,一个是将某个区间增加一个固定值
/**************************************************************/
每走到一个区间就把lazy tag下放。下放的时候注意顺序!
#include <cstdio> #include <cstring> const int N = 100100; struct NODE{ int l,r; int sum; int setTo,add; NODE(){setTo=add=0;} int length(){return (r-l+1);} }; int data[N]; NODE segtree[N*3]; void build(int id,int l,int r){ segtree[id].l = l; segtree[id].r = r; if(l==r){ segtree[id].sum = data[l]; return ; } int mid = (l+r)>>1; build(id*2+0,l,mid); build(id*2+1,mid+1,r); segtree[id].sum = segtree[id*2+0].sum+segtree[id*2+1].sum; } void modify(int id,int spos,int epos,int value,int type){ if(segtree[id].l==spos&&segtree[id].r==epos){ if(type){ segtree[id].setTo = value; segtree[id].add = 0; segtree[id].sum = segtree[id].length()*value; } else{ segtree[id].add += value; segtree[id].sum += segtree[id].length()*value; } return ; } //push down if(segtree[id].setTo){ segtree[id*2+0].setTo=segtree[id*2+1].setTo=segtree[id].setTo; segtree[id*2+0].add=segtree[id*2+1].add=0; segtree[id*2+0].sum = segtree[id*2+0].length()*segtree[id*2+0].setTo; segtree[id*2+1].sum = segtree[id*2+1].length()*segtree[id*2+1].setTo; segtree[id].setTo = 0; } if(segtree[id].add){ segtree[id*2+0].add += segtree[id].add; segtree[id*2+1].add += segtree[id].add; segtree[id*2+0].sum += segtree[id*2+0].length()*segtree[id].add; segtree[id*2+1].sum += segtree[id*2+1].length()*segtree[id].add; segtree[id].add = 0; } int mid = (segtree[id].l+segtree[id].r)>>1; if(epos<=mid) modify(id*2,spos,epos,value,type); else if(spos>mid) modify(id*2+1,spos,epos,value,type); else{ modify(id*2+0,spos,mid,value,type); modify(id*2+1,mid+1,epos,value,type); } segtree[id].sum = segtree[id*2+0].sum+segtree[id*2+1].sum; } int main(){ int n,t; scanf("%d%d",&n,&t); n++; for(int i=1;i<=n;i++) scanf("%d",data+i); build(1,1,n); while(t--){ int a,b,c,d; scanf("%d%d%d%d",&a,&b,&c,&d); modify(1,b+1,c+1,d,a); printf("%d\n",segtree[1].sum); } return 0; }
时间: 2024-10-12 11:22:27