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.

区间求和:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
typedef long long LL;
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int maxn=100000;
int num[maxn];
LL sum[maxn<<2],add[maxn<<2];
int N,Q;
void pushup(int rs)
{
sum[rs]=sum[rs<<1]+sum[rs<<1|1];
}
void pushdown(int rs,int l)
{
if(add[rs])
{
add[rs<<1]+=add[rs];
add[rs<<1|1]+=add[rs];
sum[rs<<1]+=add[rs]*(l-(l>>1));
sum[rs<<1|1]+=add[rs]*(l>>1);
add[rs]=0;
}
}
void build(int rs,int l,int r)
{
if(l==r)
{
scanf("%I64d",&sum[rs]);
return ;
}
int mid=(l+r)>>1;
build(rs<<1,l,mid);
build(rs<<1|1,mid+1,r);
pushup(rs);
}
void update(int c,int x,int y,int l,int r,int rs)
{
if(l>=x&&r<=y)
{
add[rs]+=c;
sum[rs]+=(LL)c*(r-l+1);
return ;
}
pushdown(rs,r-l+1);
int mid=(l+r)>>1;
if(x<=mid) update(c,x,y,l,mid,rs<<1);
if(y>mid) update(c,x,y,mid+1,r,rs<<1|1);
pushup(rs);
}
LL query(int x,int y,int l,int r,int rs)
{
if(l>=x&&r<=y)
return sum[rs];
pushdown(rs,r-l+1);
int mid=(l+r)>>1;
LL ans=0;
if(x<=mid) ans+=query(x,y,l,mid,rs<<1);
if(y>mid) ans+=query(x,y,mid+1,r,rs<<1|1);
return ans;
}
int main()
{
int x,y,z;
std::ios::sync_with_stdio(false);
while(~scanf("%d%d",&N,&Q))
{
CLEAR(sum,0);
CLEAR(add,0);
build(1,1,N);
char str[2];
while(Q--)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%d",&x,&y,&z);
update(z,x,y,1,N,1);
}
else
{
scanf("%d%d",&x,&y);
printf("%I64d\n",query(x,y,1,N,1));
}
}
}
return 0;
}

POJ 3468 A Simple Problem with Integers(线段树区间求和)的更多相关文章

  1. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  2. poj 3468 A Simple Problem with Integers 线段树区间加,区间查询和(模板)

    A Simple Problem with Integers Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://poj.org/problem?i ...

  3. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  4. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  5. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  6. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  7. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  8. POJ 3468 A Simple Problem with Integers 线段树区间修改

    http://poj.org/problem?id=3468 题目大意: 给你N个数还有Q组操作(1 ≤ N,Q ≤ 100000) 操作分为两种,Q A B 表示输出[A,B]的和   C A B ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  10. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. 【转】C++实现RTMP协议发送H.264编码及AAC编码的音视频

    RTMP(Real Time Messaging Protocol)是专门用来传输音视频数据的流媒体协议,最初由Macromedia 公司创建,后来归Adobe公司所有,是一种私有协议,主要用来联系F ...

  2. Oracle学习笔记(1)——查询及删除重复数据

      1.查找表中多余的重复记录(根据单个字段studentid)   select * from table_name where studentid in (select studentid fro ...

  3. Ffmpeg和SDL如何同步视频(转)

    ong> PTS和DTS 幸运的是,音频和视频流都有一些关于以多快速度和什么时间来播放它们的信息在里面.音频流有采样,视频流有每秒的帧率.然而,如果我们只是简单的通过数帧和乘以帧率的方式来同步视 ...

  4. 杭电 3887 Counting Offspring

    根据上篇翻译的文章以及很多个帖子,都讲述了树状数组最基本的功能就是tree[i]保存的是位置i左边小于等于a[i]的数的个数. 这样也就可以解释代码中为什么有f[i]=getsum(sd[i-1])- ...

  5. css系列教程--margin padding column(完结)

    margin/margin-left/margin-right/margin-top/margin-bottom设置边距属性margin:0;--所有外边距0margin:0 1px;--margin ...

  6. 【转】SSIS 2012 – Package Configurations Menu Option Missing

    原文:http://dataqueen.unlimitedviz.com/2012/01/ssis-2012-package-configurations-menu-option-missing/ I ...

  7. Hibernate 关联关系映射实例

    双向多对一/一对多(many-to-one/one-to-many) 例子,多个学生对应一个班级,一个班级对应多个学生: 班级类,Grade.java: public class Grade { pr ...

  8. BullseyeCoverage:代码覆盖率。

    1,安装和使用步骤 阅读READER文档.并安装(非常简单,README中有详细指令说明) 配置环境:同样可以阅读相关文档. 增加PATH环境变量.需要注意,此路径需要增加在PATH的最前列.即< ...

  9. google visit

    http://emuch.net/bbs/viewthread.php?tid=7630684&fpage=3&target=blank 内Facebook,twitter,dropb ...

  10. Hadoop配置文件-hdfs-site.xml

     name  value Description  dfs.default.chunk.view.size 32768 namenode的http访问页面中针对每个文件的内容显示大小,通常无需设置. ...