zoukankan      html  css  js  c++  java
  • POJ --3468 A Simple Problem with Integers

    Description
    You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

    Input
    The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
    The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    “C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
    “Q a b” means querying the sum of Aa, Aa+1, … , Ab.

    Output
    You need to answer all Q commands in order. One answer in a line.

    Sample Input
    10 5
    1 2 3 4 5 6 7 8 9 10
    Q 4 4
    Q 1 10
    Q 2 4
    C 3 6 3
    Q 2 4

    Sample Output
    4
    55
    9
    15

    Hint
    The sums may exceed the range of 32-bit integers.
    应该算是板子题,主要就是加上懒标记,加快但是我在poj上1998ms,在百炼oj上607ms不知为何,
    AC code:

    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    
    using namespace std;
    typedef long long ll;
    
    const int maxn = 1e5+5;
    
    struct segtree{
        int l,r,lazy;
        ll sum,val;
    }ss[maxn<<2];
    int n,q;
    
    void pushDown(int rt){
        if(ss[rt].lazy) {
            ss[rt<<1].lazy = ss[rt<<1|1].lazy = 1;
            ss[rt<<1].val += ss[rt].val;
            ss[rt<<1|1].val += ss[rt].val;
            ss[rt<<1].sum += (ss[rt<<1].r-ss[rt<<1].l + 1) * ss[rt].val;
            ss[rt<<1|1].sum += (ss[rt<<1|1].r-ss[rt<<1|1].l + 1) * ss[rt].val;
            ss[rt].lazy = 0; ss[rt].val = 0;
        }
    }
    
    void pushUp(int rt){
        ss[rt].sum = ss[rt<<1].sum + ss[rt<<1|1].sum;
    }
    
    void build(int l,int r,int rt){
        ss[rt].l = l,ss[rt].r = r;
        if ( l == r ) {
            scanf("%lld",&ss[rt].sum);ss[rt].val = 0;ss[rt].lazy = 0;
            return;
        }
        int mid = (l+r)>>1;
        build(l,mid,rt<<1); build(mid+1,r,rt<<1|1);
        pushUp(rt); ss[rt].lazy = ss[rt].val = 0;
    }
    
    void update(int l,int r,int rt,int val){
        if( ss[rt].l == l && ss[rt].r == r) {
            ss[rt].sum += (ss[rt].r - ss[rt].l + 1) * val;
            ss[rt].lazy = 1, ss[rt].val += val;
            return;
        }
        pushDown(rt);
        int mid = (ss[rt].l+ss[rt].r)>>1;
        if ( r <= mid ) {
            update(l ,r ,rt<<1 ,val );
        } else if ( l > mid ){
            update(l ,r ,rt<<1|1 ,val );
        } else {
            update(l ,mid ,rt<<1 ,val );
            update(mid+1 ,r ,rt<<1|1 ,val );
        }
        pushUp(rt);
    }
    
    ll query(int l,int r,int rt){
        if (ss[rt].l == l && ss[rt].r == r  ) {
            return ss[rt].sum;
        }
        pushDown(rt);
        int mid = (ss[rt].l+ss[rt].r)>>1; ll sum = 0;
        if ( r <= mid ) {
            return sum + query(l,r,rt<<1);
        } else if ( l>mid ) {
            return sum  + query(l,r,rt<<1|1);
        } else {
            return sum + query(l,mid,rt<<1) + query(mid+1,r,rt<<1|1);
        }
    }
    
    int main(){
        char str;
        int a,b,c;
        while(~scanf("%d %d",&n,&q))
        {
            build(1,n,1);
            while(q--){
                getchar();
                scanf("%c",&str);
                if ( str == 'Q' ) {
                    scanf("%d %d",&a,&b);
                    if(a>b) { int t = a; a = b; b = t;  }
                    printf("%lld
    ",query(a,b,1));
                } else if ( str == 'C' ) {
                    scanf("%d %d %d",&a,&b,&c);
                    if(a>b) { int t = a; a = b; b = t;  }
                    update(a,b,1,c);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Group_concat介绍与例子
    国家大学科技园名单
    shell截取第五行第三列
    (Deep) Neural Networks (Deep Learning) , NLP and Text Mining
    28款GitHub最流行的开源机器学习项目,推荐GitHub上10 个开源深度学习框架
    SpeeDO —— 并行深度学习系统
    BrandZ:2016年全球最具价值品牌百强榜(完整报告)
    jdk1.7升级到jdk1.8后出错: [ERROR] javadoc: warning
    伪基站收集方圆500至1000米范围内的手机卡信息,并且通过伪基站设备伪装成银行、中国移动、中国联通等任意号码向手机用户发送短信或拨打电话
    360手机助手内部资料曝光,63张PPT纯干货
  • 原文地址:https://www.cnblogs.com/Nlifea/p/11745974.html
Copyright © 2011-2022 走看看