• poj3468A Simple Problem with Integers


    题目链接:http://poj.org/problem?id=3468

    区间更新,区间求和。

     1 #include<cstdio>
     2 #include<algorithm>
     3 using namespace std;
     4 #define ll long long
     5 #define lson l,m,rt<<1
     6 #define rson m+1,r,rt<<1|1
     7 const int maxn=100010;
     8 ll add[maxn<<2];
     9 ll sum[maxn<<2];
    10 
    11 
    12 void pushup(int rt)
    13 {
    14     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    15 }
    16 void pushdown(int rt,int m)
    17 {
    18     if(add[rt])
    19     {
    20         add[rt<<1]+=add[rt];
    21         add[rt<<1|1]+=add[rt];
    22         sum[rt<<1]+=add[rt]*(m-(m>>1));
    23         sum[rt<<1|1]+=add[rt]*(m>>1);
    24         add[rt]=0;
    25     }
    26 }
    27 
    28 void build(int l,int r,int rt)
    29 {
    30             add[rt]=0;
    31     if(l==r) {
    32         scanf("%lld",&sum[rt]);
    33         return;
    34     }
    35     int m=(l+r)>>1;
    36     build(lson);
    37     build(rson);
    38     pushup(rt);
    39 }
    40 
    41 void update(int L,int R,int c,int l,int r,int rt)
    42 {
    43     if(L<=l&&r<=R){
    44              add[rt]+=c;
    45         sum[rt]+=(ll)c*(r-l+1);
    46         return ;
    47     }
    48     pushdown(rt,r-l+1);
    49     int m=(l+r)>>1;
    50     if(L<=m) update(L,R,c,lson);
    51     if(R>m) update(L,R,c,rson);
    52     pushup(rt);
    53 }
    54 
    55 ll query(int L,int R,int l,int r,int rt)
    56 {
    57     if(L<=l&&r<=R)
    58     {
    59         return sum[rt];
    60     }
    61     pushdown(rt,r-l+1);
    62     ll ans=0;
    63     int m=(l+r)>>1;
    64     if(L<=m) ans+=query(L,R,lson);
    65     if(R>m) ans+=query(L,R,rson);
    66     return ans;
    67 }
    68 
    69 int main()
    70 {
    71     int n,m;
    72     while(scanf("%d%d",&n,&m)!=EOF)
    73     {
    74 
    75         build(1,n,1);
    76         while(m--)
    77         {
    78             int a,b,c;
    79             char s[2];
    80             scanf("%s",s);
    81             if(s[0]=='Q')
    82             {
    83                 scanf("%d%d",&a,&b);
    84                 printf("%lld
    ",query(a,b,1,n,1));
    85             }
    86             else {
    87                 scanf("%d%d%d",&a,&b,&c);
    88                 update(a,b,c,1,n,1);
    89             }
    90 
    91         }
    92     }
    93     return 0;
    94 }
  • 相关阅读:
    获取Mysql-jdbc驱动Driver类的两种方式
    Misha and Changing Handles
    What Are You Talking About (map)
    Let the Balloon Rise <map>的应用
    ignitius and princess 2(全排列)
    大一下学期计划
    大一上学期总结
    algorithm的基本注意事项
    STL的注意事项
    STL的基本操作指令
  • 原文地址:https://www.cnblogs.com/yijiull/p/6619105.html
Copyright © 2020-2023  润新知