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

    http://poj.org/problem?id=3468

    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 75149   Accepted: 23149
    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.
    #include<stdio.h>
    #include<math.h>
    #include<string.h>
    #include<algorithm>
    #define N 100010
    #define Lson root<<1, L, tree[root].Mid()
    #define Rson root<<1|1, tree[root].Mid() + 1, R
    
    using namespace std;
    
    struct Tree
    {
        int L, R;
        long long sum, e;
        bool op;
        int Mid()
        {
            return (L + R) / 2;
        }
        int Len()
        {
            return (R - L + 1);
        }
    } tree[N * 4];
    
    long long al[N];
    
    void Update(int root)
    {
        if(tree[root].op && tree[root].L != tree[root].R)
        {
            tree[root].op = false;
            tree[root<<1].op = tree[root<<1|1].op = true;
    
            tree[root<<1].e += tree[root].e;
            tree[root<<1|1].e += tree[root].e;
    
            tree[root<<1].sum += tree[root<<1].Len() * tree[root].e;
            tree[root<<1|1].sum += tree[root<<1|1].Len() * tree[root].e;
    
            tree[root].e = 0;
        }
    }
    
    void Build(int root, int L, int R)
    {
        tree[root].L = L, tree[root].R = R;
        tree[root].op = false;
        if(L == R)
        {
            tree[root].sum = al[L];
            return ;
        }
        Build(Lson);
        Build(Rson);
    
        tree[root].sum = tree[root<<1].sum + tree[root<<1|1].sum;
    }
    
    void Insert(int root, int L, int R, long long e)
    {
        Update(root);
        tree[root].sum += (R - L + 1) * e;
        if(tree[root].L == L && tree[root].R == R)
        {
            tree[root].op = true;
            tree[root].e = e;
            return ;
        }
        if(R <= tree[root].Mid())
            Insert(root<<1, L, R, e);
        else if(L > tree[root].Mid())
            Insert(root<<1|1, L, R, e);
        else
        {
            Insert(Lson, e);
            Insert(Rson, e);
        }
    }
    
    long long Query(int root, int L, int R)
    {
        Update(root);
        if(tree[root].L == L && tree[root].R == R)
            return tree[root].sum;
        if(R <= tree[root].Mid())
            return Query(root<<1, L, R);
        else if(L > tree[root].Mid())
            return Query(root<<1|1, L, R);
        else
            return Query(Lson) + Query(Rson);
    }
    
    int main()
    {
        int n, q, a, b, i;
        long long c;
        char s[10];
        while(scanf("%d%d", &n, &q) != EOF)
        {
            for(i = 1 ; i <= n ; i++)
                scanf("%I64d", &al[i]);
            Build(1, 1, n);
            while(q--)
            {
                scanf("%s%d%d", s, &a, &b);
                if(s[0] == 'Q')
                    printf("%I64d
    ", Query(1, a, b));
                else
                {
                    scanf("%I64d", &c);
                    Insert(1, a, b, c);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    开源框架---tensorflow c++ API 一个卡了很久的问题
    开源框架---tensorflow c++ API 运行第一个“手写字的例子”
    版本问题---Bazel与tensorflow的对应关系
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
    jQuery火箭图标返回顶部代码
  • 原文地址:https://www.cnblogs.com/qq2424260747/p/4691173.html
Copyright © 2011-2022 走看看