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

    区间修改,询问,带有 lazy tag。

    # include <stdio.h>
    
    # define ls ((r) << 1)
    # define rs ((r) << 1 | 1)
    # define mid (((x)+(y))>>1)
    
    # define MAXN 100005
    
    typedef long long int LL;
    
    int a[MAXN];
    LL sum[MAXN * 4];
    LL lazy[MAXN * 4];
    
    void update(int r)
    {
        sum[r] = sum[ls] + sum[rs];
    }
    
    void build(int x, int y, int r)
    {
        sum[r] = 0;
        lazy[r] = 0;
        if (x == y)
        {
            sum[r] = a[x];
            return ;
        }
        build(x, mid, ls);
        build(mid+1, y, rs);
        update(r);
    }
    
    void pushdown(int x, int y, int r)
    {
        if (lazy[r])
        {
            lazy[ls] += lazy[r], lazy[rs] += lazy[r];
            sum[ls] += lazy[r]*(mid-x+1);
            sum[rs] += lazy[r]*(y-mid);
            lazy[r] = 0;
        }
    }
    
    void add(int x, int y, int r, int s, int t, LL val)
    {
        if (s<=x && y<=t)
        {
            lazy[r] += val;
            sum[r] += val*(y-x+1);
            return ;
        }
        pushdown(x, y, r);
        if (s<=mid) add(x, mid, ls, s, t, val);
        if (mid+1<=t) add(mid+1, y, rs, s, t, val);
        update(r);
    }
    
    void query(int x, int y, int r, int s, int t, LL *p)
    {
        if (s<=x && y<=t)
        {
            *p += sum[r];
            return ;
        }
        pushdown(x, y, r);
        if (s<=mid) query(x, mid, ls, s, t, p);
        if (mid+1<=t) query(mid+1, y, rs, s, t, p);
    }
    
    int main()
    {
        LL ans;
        char od[5];
        int i, n, m, s, t, val;
        
        while (~scanf("%d%d", &n, &m))
        {
            for (i = 1; i <= n; ++i) scanf("%d", &a[i]);
            build(1, n, 1);
            for (i = 1; i <= m; ++i)
            {
                scanf("%s%d%d", od, &s, &t);
                if (od[0] == 'C') scanf("%d", &val), add(1, n, 1, s, t, val);
                else ans = 0, query(1, n, 1, s, t, &ans), printf("%lld\n", ans);
            }
        }
        
        return 0;
    }
  • 相关阅读:
    Java RunTime Environment (JRE) or Java Development Kit (JDK) must be available in order to run Eclipse. ......
    UVA 1597 Searching the Web
    UVA 1596 Bug Hunt
    UVA 230 Borrowers
    UVA 221 Urban Elevations
    UVA 814 The Letter Carrier's Rounds
    UVA 207 PGA Tour Prize Money
    UVA 1592 Database
    UVA 540 Team Queue
    UVA 12096 The SetStack Computer
  • 原文地址:https://www.cnblogs.com/JMDWQ/p/2647415.html
Copyright © 2011-2022 走看看