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. poj 2446 (二分匹配)

    题意:除了所给的一些点外,问能不能用1*2的矩形覆盖所有的点,矩形间不能重叠. 思路:简单二分匹配,,,,,,, #include<stdio.h> #include<string. ...

  2. ubuntu 下安装伪分布式 hadoop

    安装准备: (1)hadoop安装包:hadoop-1.2.1.tar.gz (2)jdk安装包:jdk-7u60-linux-i586.gz (3)要是须要eclipse开发的话 还须要eclips ...

  3. 面向对象3-this的用法

    1.当有定时器时 this会指向window <script type="text/javascript"> function Aaa(){ var _this=thi ...

  4. Head First C#(赛狗日)

    实验背景: 人:Joe.Bob和AI希望参见赛狗赌博.最初,Joe有50元,Bob有75元,AI有45元.每次比赛前,他们都会各自决定是否下注以及所押的赌金.直到比赛前,他们都可以改变赌金,但是一旦比 ...

  5. 新生赛(2) problem 2 丁磊养猪

    Problem B Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Sub ...

  6. net 2.0使用ajax

    asp.net ajax中用到了几个dll文件,这些可以从网上下载.http://ajax.asp.net站点下可以找到相关的下载.这其中包括:System.Web.Extensions.dll.Sy ...

  7. synchronized关键字的用法

    synchronized用于给方法或者块加锁用的,只有获得该对象或者块的锁的对象才能够执行里面的代码,否则将阻塞在那里,等待该锁被释放,然后获得该锁继续执行.比如下面模拟售票的代码: /** * 模拟 ...

  8. JQ 更改li 颜色

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. Core Bluetooth下实现两个设备进行互联

    一.外设管理者 - 发布广告 如果两部手机在BLE的基础上进行连接,需要让其中一部手机作为外设,外设需要进行广播自己需要发布的数据,以供中心设备的接收和处理. 实现外设广播数据并且处理发送过程当中的流 ...

  10. C# 限制Text只能输入数字

    private void InputNumber(KeyPressEventArgs e) { //如果输入的不是数字键,也不是回车键.Backspace键,则取消该输入 && e.K ...