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

时间:2022-05-05
本文章向大家介绍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, A1, A2, ... , 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 A1, A2, ... , 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

POJ Monthly--2007.11.25, Yang Yi

代码:

#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("%I64dn",Query(a,b,1));
         else{
           scanf("%d",&c);
           Update(a,b,1,c);
         }
       }
    }
  return 0;
}