poj 3468

                                                                                   A Simple Problem with Integers

一直在数据类型上出错。

终于知道long long 是 “%lld” 来输出输入了。

time 1732ms
#include <iostream>
#include <stdio.h>
#define LL long long
using namespace std;
const int N=100005;
LL add[N<<2];
LL sum[N<<2];
void push_up(int rt)
{  sum[rt]=sum[rt<<1]+sum[rt<<1|1]; }
void push_down(int rt,int m)
{  if(add[rt])
   {  add[rt<<1]+=add[rt];
      add[rt<<1|1]+=add[rt];
      sum[rt<<1]+=add[rt]*(m-(m>>1));
      sum[rt<<1|1]+=add[rt]*(m>>1) ;
      add[rt]=0;
    }
}
void build(int l,int r,int rt)
{  add[rt]=0;
   if(l==r)
   {  scanf("%lld",&sum[rt]) ;
      return ;
   }
   int m=(l+r)>>1;
   build(l,m,rt<<1);
   build(m+1,r,rt<<1|1);
   push_up(rt);
}
void updata(int l,int r,int rt,int L,int R,int v)
{  if(L<=l&&R>=r)
   {   add[rt]+=v;
       sum[rt]+=(LL)v*(r-l+1);
       return ; 
   }
   push_down(rt,r-l+1);
   int m=(l+r)>>1;
   if(L<=m)
     updata(l,m,rt<<1,L,R,v);
   if(R>m)
     updata(m+1,r,rt<<1|1,L,R,v);
   push_up(rt);
}
LL query(int l,int r,int rt,int L,int R)
{   if(L<=l&&R>=r) 
      return sum[rt];
    push_down(rt,r-l+1);
    int m=(l+r)>>1;
    LL ans=0;
    if(L<=m)
       ans+=query(l,m,rt<<1,L,R);
    if(R>m)
       ans+=query(m+1,r,rt<<1|1,L,R);
    return ans;
}
int main()
{
    int n,m,a,b,c;
    char op[2];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        build(1,n,1);
        while(m--)
        {
            scanf("%s",op);
            if(op[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(1,n,1,a,b));
            }
            else
            {
                scanf("%d%d%d",&a,&b,&c);
                updata(1,n,1,a,b,c);
            }
        }
    }
}



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值