• POJ 3468 区间加减 区间求和


     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define lson l,m,rt<<1
     6 #define rson m+1,r,rt<<1|1
     7 #define INF 0x7fffffff
     8 #define maxn 101000
     9 #define LL long long
    10 using namespace std;
    11 LL sum[maxn<<2];
    12 int add[maxn<<2],n,Q,u,v,w,m;
    13 void pushup(int rt){
    14     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    15 }
    16 void pushdown(int rt,int m){
    17     if(add[rt]){
    18         add[rt<<1]+=add[rt];
    19         add[rt<<1|1]+=add[rt];
    20         sum[rt<<1]+=(LL)add[rt]*(m-(m>>1));
    21         sum[rt<<1|1]+=(LL)add[rt]*(m>>1);
    22         add[rt]=0;
    23     }
    24 }
    25 void build(int l,int r,int rt){
    26     add[rt]=0;    
    27     if(l==r){
    28         scanf("%lld",&sum[rt]);
    29         return;
    30     }
    31     int m=(l+r)>>1;
    32     build(lson);
    33     build(rson);
    34     pushup(rt);
    35 }
    36 void update(int L,int R,int c,int l,int r,int rt){
    37     if(l>=L&&r<=R){
    38         add[rt]+=c;
    39         sum[rt]+=(LL)c*(r-l+1);
    40         return ;
    41     }
    42     pushdown(rt,r-l+1);
    43     int m=(l+r)>>1;
    44     if(L<=m)update(L,R,c,lson);
    45     if(R>m)update(L,R,c,rson);
    46     pushup(rt);
    47 }
    48 LL Query(int L,int R,int l,int r,int rt){
    49     if(l>=L&&r<=R){
    50         return sum[rt];
    51     }
    52     pushdown(rt,r-l+1);
    53     LL res=0;
    54     int m=(l+r)>>1;
    55     if(L<=m) res+=Query(L,R,lson);
    56     if(R>m) res+=Query(L,R,rson);
    57     return res;
    58 }
    59 int main(){
    60     cin>>n>>Q;
    61     build(1,n,1);
    62     char opt[20];
    63     for(int i=1;i<=Q;i++){
    64         cin>>opt;
    65         if(opt[0]=='C'){
    66             scanf("%d%d%d",&u,&v,&w);
    67             update(u,v,w,1,n,1);
    68         }
    69         else{
    70             scanf("%d%d",&u,&v);
    71             cout<<Query(u,v,1,n,1)<<endl;
    72         }
    73     }
    74     return 0;
    75 }
  • 相关阅读:
    Flex 布局
    vue学习之用 Vue.js + Vue Router 创建单页应用的几个步骤
    vue学习起步:了解下
    vue学习一:新建或打开vue项目(vue-cli2)
    adb环境变量配置
    数据类型判断和数据类型转换代码工具
    日期工具集合
    postman变量的使用和设置
    浮点数运算和金额处理
    07- Linux常用命令
  • 原文地址:https://www.cnblogs.com/poler/p/7281436.html
Copyright © 2020-2023  润新知