前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >poj------(3468)A Simple Problem with Integers(区间更新)

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

作者头像
Gxjun
发布2018-03-26 10:40:38
5370
发布2018-03-26 10:40:38
举报
文章被收录于专栏:mlml

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

代码语言:javascript
复制
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

代码语言:javascript
复制
4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

POJ Monthly--2007.11.25, Yang Yi

代码:

代码语言:javascript
复制
#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\n",Query(a,b,1));
         else{
           scanf("%d",&c);
           Update(a,b,1,c);
         }
       }
    }
  return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2014-08-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档