• A Simple Problem with Integers-POJ3468


    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 abc" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
    "Q ab" 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<algorithm>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    using namespace std;
    #define N 100100
    #define Lson r<<1
    #define Rson r<<1|1
    
    struct node
    {
        int L,R;
        long long sum,e;
        int mid()
        {
            return (L+R)/2;
    
        }
        int len()
        {
            return R-L+1;
        }
    }a[N<<2];
    
    void BuildTree(int r,int L,int R)
    {
        a[r].L=L;
        a[r].R=R;
        a[r].e=0;
        if(L==R)
        {
            scanf("%lld",&a[r].sum);
            return;
        }
        BuildTree(Lson,L,a[r].mid());
        BuildTree(Rson,a[r].mid()+1,R);
        a[r].sum=a[Lson].sum+a[Rson].sum;
    }
    void Down(int r)
    {
        a[Lson].sum+=a[Lson].len()*a[r].e;
        a[Lson].e+=a[r].e;
        a[Rson].sum+=a[Rson].len()*a[r].e;
        a[Rson].e+=a[r].e;
        a[r].e=0;
    }
    void Add(int r,int L,int R,int e)
    {
        a[r].sum+=(R-L+1)*e;
        if(a[r].L==L && a[r].R==R)
        {
            a[r].e+=e;
            return;
        }
        Down(r);
        if(R<=a[r].mid())
            Add(Lson,L,R,e);
        else if(L>a[r].mid())
            Add(Rson,L,R,e);
        else
        {
            Add(Lson,L,a[r].mid(),e);
            Add(Rson,a[r].mid()+1,R,e);
        }
    }
    long long Qurry(int r,int L,int R)
    {
        if(a[r].L==L && a[r].R==R)
        {
            return a[r].sum;
        }
        Down(r);
        if(R<=a[r].mid())
            return Qurry(Lson,L,R);
        else if(L>a[r].mid())
            return Qurry(Rson,L,R);
        else
        {
            long long int a1=Qurry(Lson,L,a[r].mid());
            long long int a2=Qurry(Rson,a[r].mid()+1,R);
            return a1+a2;
        }
    }
    
    int main()
    {
        int n,m,d,b,c;
        while(scanf("%d %d",&n,&m)!=EOF)
        {
            BuildTree(1,1,n);
            char s[10];
            while(m--)
            {
                scanf("%s",s);
                if(s[0]=='Q')
                {
                    scanf("%d %d",&d,&b);
                    printf("%lld
    ",Qurry(1,d,b));
                }
                else
                {
                    scanf("%d %d %d",&d,&b,&c);
                    Add(1,d,b,c);
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    redis方法中文解释
    简单redis队列实现
    PHP中常用的字符串操作【转】
    SQL循环语句
    crontable 实例
    自动以当前时间命名文件
    tar命令详解
    PHP 数据类型验证和获取
    Nginx 配置文件nginx.conf的完整配置说明
    sql server DateFormat(转)
  • 原文地址:https://www.cnblogs.com/linliu/p/4953486.html
Copyright © 2020-2023  润新知