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. 改变UITableViewCell按下去的颜色

    UIView *bgColorView = [[UIView alloc] init]; [bgColorView setBackgroundColor:[UIColor redColor]]; [c ...

  2. 只允许指定IP访问指定端口 ufw

    那天工作需要在服务器上指定ip才可以访问指定端口,配置命令如下: (服务器是ubuntu 14.04系统) apt-get install ufw ufw enable ufw default den ...

  3. Linux-0.11内核源代码分析系列:内存管理get_free_page()函数分析

    Linux-0.11内存管理模块是源码中比較难以理解的部分,如今把笔者个人的理解发表 先发Linux-0.11内核内存管理get_free_page()函数分析 有时间再写其它函数或者文件的:) /* ...

  4. 在unity的scene中画五角星

    使用Gizmos的DrawLine方法画线. 首先在场景中找到五角星的五个定点的坐标,按照一笔画的顺序命名为1,2,3,4,5,如图所示: 接下来就是编写代码了,代码很少,如下所示: using Un ...

  5. LFS:kernel panic VFS: Unable to mount root fs

    说明: 使用Vm虚拟机构建自己的LFS系统时,系统引导不成功,提示 kernel panic VFS: Unable to mount root fs 参考链接:http://www.52os.net ...

  6. PHP: configure: error: mysql configure failed. Please check config.log for more information.

    为php增加mysql模块时报错 configure: error: mysql configure failed. Please check config.log for more informat ...

  7. 【后缀数组】【poj2774】【 Long Long Message】

    题意: 求两个串的最长连续子串. 我的想法: 枚举第二个串...在第一个串的后缀数组中二分查找. 复杂度NlogN.最坏情况N^2 题解: (3)height 数组:定义height[i]=suffi ...

  8. MySql优化-你的SQL命中索引了吗

    在项目开发中SQL是必不可少的,表索也一样.这些SQL的运行性能不知道吗?有多少是命中了索引的?命中哪个索引?索引中有哪个是无效索引?这些无效索引是否会影响系统的性能?带着这些问题我们一起来学习一下. ...

  9. Asp.net MVC razor语法参考

    Razor语法的快捷参考http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/ 只是copy下来便于查阅! I ...

  10. Object转换为JSON格式字符串

    简介: 把JS的Object转换为Json字符串. 代码: function ObjectToJson(object) { // Object转换为josn var json = "&quo ...