• poj 3468 线段树成段更新


    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 54012   Accepted: 16223
    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<iostream>
    #include<cstdio>
    using namespace std;
    
    #define lson left,mid,i<<1
    #define rson mid+1,right,i<<1|1
    
    typedef __int64 LL;
    int a[100005];
    
    struct IntervalTree
    {
        int left,right;
        LL sum,add;
    }f[300005];
    
    void pushdown(int i)
    {
        if(f[i].add != 0)
        {
            f[i<<1].add+=f[i].add;
            f[i<<1|1].add+=f[i].add;
            f[i<<1].sum+=(f[i<<1].right-f[i<<1].left+1)*f[i].add;
            f[i<<1|1].sum+=(f[i<<1|1].right-f[i<<1|1].left+1)*f[i].add;
            f[i].add=0;
        }
    }
    
    void pushup(int i)
    {
        f[i].sum=f[i<<1].sum+f[i<<1|1].sum;
    }
    
    void bulid(int left,int right,int i)
    {
        f[i].left=left,f[i].right=right,f[i].add=0;
        if(left==right)
        {
            f[i].sum=a[left];return ;
        }
        int mid=(left+right)>>1;
        bulid(lson);
        bulid(rson);
        pushup(i);
        return ;
    }
    
    void update(int left,int right,int add,int i)
    {
        if(f[i].left==left && f[i].right==right)
        {
            f[i].add+=add;
            f[i].sum+=(right-left+1)*add;
            return ;
        }
        pushdown(i);
        if(f[i<<1].right>=right) update(left,right,add,i<<1);
        else if(f[i<<1|1].left<=left) update(left,right,add,i<<1|1);
        else { update(left,f[i<<1].right,add,i<<1);update(f[i<<1|1].left,right,add,i<<1|1);}
        pushup(i);
        return ;
    }
    
    LL query(int left,int right,int i)
    {
        if(f[i].left==left && f[i].right==right) return f[i].sum;
        pushdown(i);
        if(f[i<<1].right>=right) return query(left,right,i<<1);
        else if (f[i<<1|1].left<=left) return query(left,right,i<<1|1);
        else return query(left,f[i<<1].right,i<<1)+query(f[i<<1|1].left,right,i<<1|1);
    }
    
    int main()
    {
        int i,n,m,ai,bi,d;
        char ch[2];
        while(~scanf("%d %d",&n,&m))
        {
            for(i=1;i<=n;i++) scanf("%d",a+i);
            bulid(1,n,1);
            while(m--)
            {
                scanf("%s",ch);
                if(strcmp(ch,"C")==0)
                {
                    scanf("%d %d %d",&ai,&bi,&d);
                    update(ai,bi,d,1);
                }
                else
                {
                    scanf("%d %d",&ai,&bi);
                    printf("%I64d
    ",query(ai,bi,1));
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    字符串,format格式化及列表的相关进阶操作---day07
    利用wiile双层循环打印各种星星---day06
    双层循环练习,pass_break_continue,和for循环---day06
    类型判断,代码块,流程控制及循环---day05
    频繁项集算法
    Unity 物体移动的理解
    Game1---游戏设计
    精读Hadamard Response论文
    java 创建线程
    Unity游戏开发面试基础知识
  • 原文地址:https://www.cnblogs.com/xiong-/p/3590251.html
Copyright © 2020-2023  润新知