zoukankan      html  css  js  c++  java
  • poj3468

                                                                                                    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 47204   Accepted: 13856
    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.
     
     这题跟http://www.cnblogs.com/Deng1185246160/p/3247466.html  有非常相似之处 !!
     
    #include<iostream>
    #include<cstring>
    #include<string>
    #include<cstdio>
    #include<algorithm>
    #include<cmath>
    using namespace std;
    const int maxn = 100010;
    int a[maxn];__int64 ans ;
    struct Node
    {
        int left,right;
        __int64 sum,add;
    }tree[maxn*5];
    
    void build(int l,int r,int n)
    {
        tree[n].left = l;
        tree[n].right = r;
        tree[n].add = 0; 
        if(l == r)
        {
            tree[n].sum = a[l];
            return ;
        }
        int mid = (l+r)/2;
        build(l,mid,2*n);
        build(mid+1,r,2*n+1);
        tree[n].sum = tree[2*n].sum + tree[2*n+1].sum;
    }
    
    void update(int l,int r,int add,int n)
    {
        if(tree[n].left > r || tree[n].right < l) return ;
        if(tree[n].left >= l && tree[n].right <= r) 
        {
            tree[n].sum += (tree[n].right - tree[n].left + 1)*add;
            tree[n].add += add;
            return ;
        }
        if(tree[n].add)
        {
            tree[2*n].sum += (tree[2*n].right - tree[2*n].left + 1)* tree[n].add;
            tree[2*n].add += tree[n].add;
            tree[2*n+1].sum += (tree[2*n+1].right - tree[2*n+1].left + 1)* tree[n].add;
            tree[2*n+1].add += tree[n].add;
            tree[n].add = 0;
        }
        update(l,r,add,2*n);
        update(l,r,add,2*n+1);
        tree[n].sum = tree[2*n].sum + tree[2*n+1].sum ;
    }
    
    void query(int l,int r,int n)
    {
        if(tree[n].left > r || tree[n].right < l) return ;
        if(tree[n].left >= l && tree[n].right <= r) 
        {
            ans += tree[n].sum;
             return ;
        }
        if(tree[n].add)
        {
            tree[2*n].sum += (tree[2*n].right - tree[2*n].left + 1)* tree[n].add;
            tree[2*n].add += tree[n].add;
            tree[2*n+1].sum += (tree[2*n+1].right - tree[2*n+1].left + 1)* tree[n].add;
            tree[2*n+1].add += tree[n].add;
            tree[n].add = 0;
        }
        query(l,r,2*n);
        query(l,r,2*n+1);
        tree[n].sum = tree[2*n].sum + tree[2*n+1].sum ;
    }
    int main()
    {
        int n,m;
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            int i,x,y,z;
            char c;
            for(i=1;i<=n;i++)
            {
                scanf("%d",&a[i]);
            }
            build(1,n,1);
            scanf("%*c");
            for(i=1;i<=m;i++)
            {
                scanf("%c",&c);
                if(c=='Q')
                {
                    scanf("%d %d%*c",&x,&y);
                    ans = 0;
                    query(x,y,1);
                    printf("%I64d
    ",ans);
                }
                else 
                {
                    scanf("%d %d %d%*c",&x,&y,&z);
                    update(x,y,z,1);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    Math类的用法
    c#中和java中可变参数对比
    c#中泛型集合directory和java中map集合对比
    C#调用短信接口(通过简单的工厂模式整合多个短信平台)
    C#用网易邮箱发送邮件(同步异步)
    新浪云-PHP实现上传原图,缩略图
    PHP 继承,组合,单模式,GUID,等混合实例
    ASP.Net MVC @Html类
    ASP.net MVC 无法初始化 ASP.NET Simple Membership 数据库
    PHP 单列模式实例讲解以及参考网址
  • 原文地址:https://www.cnblogs.com/Deng1185246160/p/3257596.html
Copyright © 2011-2022 走看看