• poj------(3468)A Simple Problem with Integers(区间更新)


    A Simple Problem with Integers
    Time Limit: 5000MS   Memory Limit: 131072K
    Total Submissions: 60745   Accepted: 18522
    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.

    Source

     
    代码:
    #include<cstdio>
    #include<cstring>
    const int maxn=100005;
    struct node
    {
     int lef,rig;
     __int64 sum,cnt;
     int mid(){
       return lef+(rig-lef>>1);
      }
    };
     node reg[maxn<<2];
    
    void Build(int left ,int right,int pos)
    {
      reg[pos]=(node){left,right,0,0};
      if((left==right))
      {
        scanf("%I64d",&reg[pos].sum);
        return ;
      }
      int mid=reg[pos].mid();
      Build(left,mid,pos<<1);
      Build(mid+1,right,pos<<1|1);
      reg[pos].sum=reg[pos<<1].sum+reg[pos<<1|1].sum;
    }
    void Update(int left,int right,int pos,int val)
    {
        if(reg[pos].lef>=left&&reg[pos].rig<=right)
        {
          reg[pos].cnt+=val;
          reg[pos].sum+=val*(reg[pos].rig-reg[pos].lef+1);
          return ;
        }
        if(reg[pos].cnt)
        {
          reg[pos<<1].cnt+=reg[pos].cnt;
          reg[pos<<1|1].cnt+=reg[pos].cnt;
          reg[pos<<1].sum+=reg[pos].cnt*(reg[pos<<1].rig-reg[pos<<1].lef+1);
          reg[pos<<1|1].sum+=reg[pos].cnt*(reg[pos<<1|1].rig-reg[pos<<1|1].lef+1);
          reg[pos].cnt=0;
        }
        int mid=reg[pos].mid();
        if(left<=mid)
            Update(left,right,pos<<1,val);
        if(right>mid)
            Update(left,right,pos<<1|1,val);
      reg[pos].sum=reg[pos<<1].sum+reg[pos<<1|1].sum;
    }
    __int64 Query(int left,int right,int pos)
    {
        if(left<=reg[pos].lef&&reg[pos].rig<=right)
        {
          return reg[pos].sum;
        }
        if(reg[pos].cnt)  //再向下更新一次
        {
          reg[pos<<1].cnt+=reg[pos].cnt;
          reg[pos<<1|1].cnt+=reg[pos].cnt;
          reg[pos<<1].sum+=reg[pos].cnt*(reg[pos<<1].rig-reg[pos<<1].lef+1);
          reg[pos<<1|1].sum+=reg[pos].cnt*(reg[pos<<1|1].rig-reg[pos<<1|1].lef+1);
          reg[pos].cnt=0;
        }
        int mid=reg[pos].mid();
        __int64 res=0;
        if(left<=mid)
            res+=Query(left,right,pos<<1);
        if(mid<right)
            res+=Query(left,right,pos<<1|1);
       return res;
    }
    int main()
    {
        int n,m,a,b,c;
        char ss;
        while(scanf("%d%d",&n,&m)!=EOF)
        {
            Build(1,n,1);
          while(m--)
           {
            getchar();
            scanf("%c %d%d",&ss,&a,&b);
            if(ss=='Q')
                printf("%I64d
    ",Query(a,b,1));
             else{
               scanf("%d",&c);
               Update(a,b,1,c);
             }
           }
        }
      return 0;
    }
  • 相关阅读:
    Struts2的原理,配置和使用
    tomcat启动异常之----A child container failed during start
    Oracle两表关联更新其中一张表的数据
    jsp隐藏字符串中间部分信息,只显示前后字段
    浏览器会缓存js文件
    tomcat启动项目很快,且不报错,访问报404,项目实际上没起来,起的是空tomcat
    jd-eclipse插件的安装
    Junit4使用总结
    json中dump()与dumps()里的参数解释
    python时间函数和常用格式化
  • 原文地址:https://www.cnblogs.com/gongxijun/p/3899033.html
Copyright © 2020-2023  润新知