• 线段树模板(区间修改,懒标)


     1 #include<algorithm>
     2 #include<cstdio>
     3 using namespace std;
     4 typedef long long ll;
     5 const ll N=1e5+10,INF=0x3f3f3f3f;
     6 ll n,m;
     7 ll sum[N],t[N<<2],tag[N<<2],ans[N<<2];
     8 inline ll ls(ll p){return p<<1;}
     9 inline ll rs(ll p){return p<<1|1;}
    10 inline void push_up(ll p){ans[p]=ans[ls(p)]+ans[rs(p)];}
    11 inline void add(ll l,ll r,ll p,ll k){ans[p]+=k*(r-l+1);tag[p]+=k;}
    12 inline ll read(){
    13     ll x=0,f=1;char ch=getchar();
    14     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    15     while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    16     return x*f;
    17 }
    18 inline void scan(){
    19     n=read();m=read();
    20     for(ll i=1;i<=n;i++)sum[i]=read();
    21 }
    22 void build(ll p,ll l,ll r){
    23     if(l==r){
    24        ans[p]=sum[l];return;
    25     }
    26     ll mid=(l+r)>>1;
    27     build(ls(p),l,mid);
    28     build(rs(p),mid+1,r);
    29     push_up(p);
    30 }
    31 void push_down(ll l,ll r,ll p){
    32     ll mid=(l+r)>>1;
    33     add(l,mid,ls(p),tag[p]);
    34     add(mid+1,r,rs(p),tag[p]);
    35     tag[p]=0;
    36 }
    37 void update(ll cl,ll cr,ll l,ll r,ll p,ll k){
    38     if(cl<=l&&cr>=r){
    39         add(l,r,p,k);return;
    40     }
    41     ll mid=(l+r)>>1;
    42     push_down(l,r,p);
    43     if(cl<=mid)update(cl,cr,l,mid,ls(p),k);
    44     if(cr>mid)update(cl,cr,mid+1,r,rs(p),k);
    45     push_up(p);
    46 }
    47 ll query(ll cl,ll cr,ll l,ll r,ll p){
    48     ll res=0;
    49     if(cl<=l&&cr>=r)return ans[p];
    50     ll mid=(l+r)>>1;
    51     push_down(l,r,p);
    52     if(cl<=mid)res+=query(cl,cr,l,mid,ls(p));
    53     if(cr>mid)res+=query(cl,cr,mid+1,r,rs(p));
    54     return res;
    55 }
    56 int main(){
    57 //    freopen("testdata.in","r",stdin);
    58 //    freopen("testdata.out","w",stdout);
    59     scan();
    60     build(1,1,n);
    61     while(m--){
    62         ll k=read();
    63         if(k==1){
    64             ll l=read(),r=read(),k=read();
    65             update(l,r,1,n,1,k);
    66         }
    67         else {
    68             ll l=read(),r=read();
    69             printf("%lld
    ",query(l,r,1,n,1));
    70         }
    71     }
    72     return 0;
    73 } 
    View Code
  • 相关阅读:
    KVM镜像管理利器-guestfish使用详解
    两台linux机器时间同步
    git配合tortoiseGit的基础使用
    使用yum来下载RPM包而不进行安装
    Linux解压缩总结
    Linux下使用git命令及github项目
    linux shell常用快捷键
    调用 sphinx-build生成HTML文件
    复制virtualenv环境到其他服务器环境配置的方法
    CentOS 6.5 PYPI本地源制作
  • 原文地址:https://www.cnblogs.com/lifeisabadword/p/11822701.html
Copyright © 2020-2023  润新知