• 线段树 poj 3468 A Simple Problem with Integers 区间add更新


    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 41899   Accepted: 12169
    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>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #define N 222222
    
    using namespace std;
    
    int num[N];
    
    struct Tree
    {
        int l;
        int r;
        long long sum;
        long long col;
    } tree[N*4];
    
    void push_up(int rt)
    {
        tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
    }
    
    void push_down(int rt,int m)
    {
        if (tree[rt].col!=0)
        {
            tree[rt<<1].col+=tree[rt].col;
            tree[rt<<1|1].col+=tree[rt].col;
            tree[rt<<1].sum+=(long long)(m-(m/2))*tree[rt].col;
            tree[rt<<1|1].sum+=(long long)(m/2)*tree[rt].col;
            tree[rt].col=0;
        }
    }
    
    void build(int root,int l,int r)
    {
        tree[root].l=l;
        tree[root].r=r;
        tree[root].col=0;
        if(tree[root].l==tree[root].r)
        {
            tree[root].sum=num[l];
            return;
        }
        int mid=(l+r)/2;
        build(root<<1,l,mid);
        build(root<<1|1,mid+1,r);
        push_up(root);
    }
    
    void update(int root,int L,int R,int val)
    {
        if(L<=tree[root].l&&R>=tree[root].r)
        {
            tree[root].col+=val;
            tree[root].sum+=(long long)val*(tree[root].r-tree[root].l+1);
            return;
        }
        push_down(root,tree[root].r-tree[root].l+1);
        int mid=(tree[root].l+tree[root].r)/2;
        if (L<=mid)
            update(root<<1,L,R,val);
        if (R>mid)
            update(root<<1|1,L,R,val);
        push_up(root);
    }
    
    
    long long query(int root,int L,int R)
    {
        if(L<=tree[root].l&&R>=tree[root].r)
            return tree[root].sum;
        push_down(root,tree[root].r-tree[root].l+1);
        int mid=(tree[root].l+ tree[root].r)/2;
        long long ret=0;
        if(L<=mid) ret+=query(root<<1,L,R);
        if(R>mid) ret+=query(root<<1|1,L,R);
        return ret;
    }
    
    
    int main()
    {
        int n,q;
        char s[2];
        scanf("%d%d",&n,&q);
        for (int i=1; i<=n; i++) scanf("%d",&num[i]);
        build(1,1,n);
        while (q--)
        {
            int x,y,z;
            scanf("%s",s);
            if (s[0]=='Q')
            {
                scanf("%d%d",&x,&y);
                printf("%I64d\n",query(1,x,y));
            }
            else if (s[0]=='C')
            {
                scanf("%d%d%d",&x,&y,&z);
                update(1,x,y,z);
            }
        }
        return 0;
    }
    




  • 相关阅读:
    Oracle分析函数row_number()等的使用实例
    Oracle行列转换case when then方法案例
    Oracle日期函数
    Oracle转换函数
    Oracle数值函数
    Oracle字符函数length substr concat实例
    Oracle列转行函数LISTAGG()
    Oracle 条件判断函数decode和case when then案例
    Oracle NVL空值处理函数
    【学亮开讲】Oracle内外连接查询20181119
  • 原文地址:https://www.cnblogs.com/cyendra/p/3038419.html
Copyright © 2020-2023  润新知