zoukankan      html  css  js  c++  java
  • POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers

    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 112228   Accepted: 34905
    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


    题意就是如题,对线段树的区间进行更新,可增可减可修改,并查询区间和,此题就是增,具体实现看代码吧.
    AC代码:
    Source Code
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    using namespace std;
    #define maxsize 100005
    #define LL long long
    LL num[maxsize];
    struct node
    {
        LL l,r;
        LL maxn,add;
        int mid()
        {
            return (l+r)/2;
        }
    } tree[maxsize<<2];
    void Build_Tree(LL root,LL l,LL r)
    {
        tree[root].l=l;
        tree[root].r=r;
        tree[root].add=0;
        if(l==r)
        {
            tree[root].maxn=num[l];
            return ;
        }
        Build_Tree(root*2,l,tree[root].mid());
        Build_Tree(root*2+1,tree[root].mid()+1,r);
        tree[root].maxn=tree[root*2].maxn+tree[root*2+1].maxn;
    }
    void Update(LL root,LL l,LL r,LL czp)
    {
        if(tree[root].l==l&&tree[root].r==r)
        {
            tree[root].add+=czp;
            return ;
        }
        tree[root].maxn+=((r-l+1)*czp);
        if(tree[root].mid()>=r) Update(root<<1,l,r,czp);
        else if(tree[root].mid()<l)     Update(root<<1|1,l,r,czp);
        else
        {
            Update(root<<1,l,tree[root].mid(),czp);
            Update(root<<1|1,tree[root].mid()+1,r,czp);
        }
    }
    long long Query(LL root,LL l,LL r)
    {
        if(tree[root].l==l&&tree[root].r==r)
        {
            return tree[root].maxn+(r-l+1)*tree[root].add;
        }
        tree[root].maxn+=(tree[root].r-tree[root].l+1)*tree[root].add;
        Update(root<<1,tree[root].l,tree[root].mid(),tree[root].add);
        Update(root<<1|1,tree[root].mid()+1,tree[root].r,tree[root].add);
        tree[root].add=0;
        if(tree[root].mid()>=r) return Query(root*2,l,r);
        else if(tree[root].mid()<l) return Query(root*2+1,l,r);
        else
        {
            LL Lans=Query(root*2,l,tree[root].mid());
            LL Rans=Query(root*2+1,tree[root].mid()+1,r);
            return Lans+Rans;
        }
    }
    int main()
    {
        /*ios::sync_with_stdio(false);*/
        char str[5];
        LL m,n,a,b,c;
        while(scanf("%lld%lld",&m,&n)!=EOF)
        {
            for(LL i=1; i<=m; i++)    scanf("%I64d",&num[i]);
            Build_Tree(1,1,m);
            for(LL i=1; i<=n; i++)
            {
                scanf("%s%I64d%I64d",str,&a,&b);
                if(str[0]=='Q')    printf("%I64d
    ",Query(1,a,b));
                else    if(str[0]=='C')
                {
                    scanf("%I64d",&c);
                    Update(1,a,b,c);
                }
            }
        }
        return 0;
    }
    
    
    


  • 相关阅读:
    ftp实现普通账号和vip账号限速
    CentOS7 无人值守服务环境搭建(PXE + DHCP+TFTP+ Kickstart+ FTP)
    rsync 系统用户/虚拟用户 备份web服务器数据及无交互定时推送备份
    LVM逻辑卷管理
    Linux允许、禁止ping包
    javascript call 与 apply
    js 内存进阶 function扫描解析
    事件绑定之鼠标悬停 -入门-进阶-精通-骨灰(来自锋利的jQuery)
    show(),hide()和display在一起的用法
    Nat模式下网卡配置及Xshell连接
  • 原文地址:https://www.cnblogs.com/weimeiyuer/p/7218904.html
Copyright © 2011-2022 走看看