• A Simple Problem with Integers 线段树 区间更新 区间查询


    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 115624   Accepted: 35897
    Case Time Limit: 2000MS

    Description

    You have N integers, A1A2, ... , 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 A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
    Each of the next Q lines represents an operation.
    "C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q a b" means querying the sum of AaAa+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.

    Source

     

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<sstream>
    #include<algorithm>
    #include<queue>
    #include<deque>
    #include<iomanip>
    #include<vector>
    #include<cmath>
    #include<map>
    #include<stack>
    #include<set>
    #include<fstream>
    #include<memory>
    #include<list>
    #include<string>
    using namespace std;
    typedef long long LL;
    typedef unsigned long long ULL;
    #define MAXN 100009
    #define L 31
    #define INF 1000000009
    #define eps 0.00000001
    /*
    线段树 区间更新区间查询
    */
    LL a[MAXN],pre[MAXN];
    struct node
    {
        LL l, r;
        LL data, sum, laz;
    }T[MAXN*4];
    void build(LL p, LL l, LL r)
    {
        T[p].data = T[p].sum = T[p].laz = 0;
        T[p].l = l, T[p].r = r;
        if (l == r) return;
        LL mid = (l + r) / 2;
        build(p * 2, l, mid);
        build(p * 2 + 1, mid + 1, r);
    }
    void update(LL p, LL l, LL r, LL v)
    {
        //cout << p << ' ' << l << ' ' << r << ' ' << v << endl;
        if (T[p].l >= l && T[p].r <= r)
        {
            T[p].data += v;
            T[p].laz = 1;
            T[p].sum += (T[p].r - T[p].l + 1) * v;
            return;
        }
        LL mid = (T[p].l + T[p].r) / 2;
        if (T[p].laz)
        {
            T[p].laz = 0;
            update(p * 2, T[p].l, mid, T[p].data);
            update(p * 2 + 1, mid + 1, T[p].r, T[p].data);
            T[p].data = 0;
        }
        if (r <= mid)
            update(p * 2, l, r, v);
        else if (l > mid)
            update(p * 2 + 1, l, r, v);
        else
        {
            update(p * 2, l, mid, v);
            update(p * 2 + 1, mid + 1, r, v);
        }
        T[p].sum = T[p * 2].sum + T[p * 2 + 1].sum;
    }
    LL query(LL p, LL l, LL r)
    {
        if (l == T[p].l&&r == T[p].r)
            return T[p].sum;
        LL mid = (T[p].l + T[p].r) / 2;
        if (T[p].laz)
        {
            T[p].laz = 0;
            update(p * 2, T[p].l, mid, T[p].data);
            update(p * 2 + 1, mid + 1, T[p].r, T[p].data);
            T[p].data = 0;
        }
        if (r <= mid)
            return query(p * 2, l, r);
        else if (l > mid)
            return query(p * 2 + 1, l, r);
        else
            return query(p * 2, l, mid) + query(p * 2 + 1, mid + 1, r);
    }
    LL n, q;
    int main()
    {
        scanf("%lld%lld", &n, &q);
        for (LL i = 1; i <= n; i++)
            scanf("%lld", &a[i]), pre[i] = pre[i - 1] + a[i];
        char c[2];
        LL a, b, d;
        build(1,1,n);
        while (q--)
        {
            scanf("%s", c);
            if (c[0] == 'Q')
                scanf("%lld%lld", &a, &b), printf("%lld
    ", query(1, a, b) + pre[b] - pre[a-1]);
            else
                scanf("%lld%lld%lld", &a, &b, &d), update(1, a, b, d);
        }
    }
  • 相关阅读:
    Flask快速入门(14) — 请求上下文2
    Flask快速入门(13) — 请求上下文1
    24 python异常机制
    11 python socket网络编程
    21 python调用外部系统命令
    10 python从键盘获取输入、刷新缓冲区
    18 python文件、目录操作
    17 python内置特殊方法
    15 python之ORM sqlalchemy模块使用
    14 python类的继承
  • 原文地址:https://www.cnblogs.com/joeylee97/p/7339582.html
Copyright © 2020-2023  润新知