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

区间求和,成段加减

#include<iostream>
#include<cstring>
#include<cstdio> using namespace std;
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
typedef long long LL;
const int N = + ;
LL T[N<<],add[N<<]; void PushUP(int rt){
T[rt] = T[rt<<] + T[rt<<|];
} void PushDown(int rt, int m){
if(add[rt]){
add[rt<<] += add[rt];
add[rt<<|] += add[rt];
T[rt<<] += add[rt]*(LL)(m - (m >> ));
T[rt<<|] += add[rt]*(LL)(m >> );
add[rt] = ;
}
}
void Build(int l, int r, int rt){
add[rt] = ;
if(l == r){
scanf("%lld",&T[rt]);
return ;
}
int m = (l + r) >> ;
Build(lson);
Build(rson);
PushUP(rt);
} void Updata(int L,int R, int c,int l, int r, int rt){
if(L <= l && r <= R){
add[rt] += c;
T[rt] += (LL)c*(r - l + );
return ;
}
PushDown(rt, r - l + );
int m = (l + r) >> ;
if(L <= m) Updata(L, R, c, lson);
if(R > m) Updata(L, R, c, rson);
PushUP(rt);
} LL Query(int L, int R, int l, int r, int rt){
if(L <= l && r <= R) return T[rt];
PushDown(rt, r - l + );
int m = (l + r) >> ;
LL ret = ;
if(L <= m) ret += Query(L, R, lson);
if(R > m) ret += Query(L, R, rson);
return ret;
} int main(){
int n,m;
while(scanf("%d %d",&n,&m)==){
Build(, n, );
char ch[];
int a,b,c;
while(m--){
scanf("%s %d %d",ch,&a,&b);
if(ch[] == 'Q')printf("%lld\n",Query(a, b, , n, ));
else{
scanf("%d",&c);
Updata(a, b, c, , n, );
}
}
}
return ;
}
 

POJ-3468 A Simple Problem with Integers (区间求和,成段加减)的更多相关文章

  1. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  2. POJ 3468 A Simple Problem with Integers (线段树成段更新)

    题目链接:http://poj.org/problem?id=3468 题意就是给你一组数据,成段累加,成段查询. 很久之前做的,复习了一下成段更新,就是在单点更新基础上多了一个懒惰标记变量.upda ...

  3. 【POJ】3468 A Simple Problem with Integers ——线段树 成段更新 懒惰标记

    A Simple Problem with Integers Time Limit:5000MS   Memory Limit:131072K Case Time Limit:2000MS Descr ...

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

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  5. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  6. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  7. POJ 3468 A Simple Problem with Integers(线段树功能:区间加减区间求和)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

  8. poj 3468 A Simple Problem with Integers(线段树+区间更新+区间求和)

    题目链接:id=3468http://">http://poj.org/problem? id=3468 A Simple Problem with Integers Time Lim ...

  9. POJ 3468 A Simple Problem with Integers(分块入门)

    题目链接:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit ...

随机推荐

  1. linux下安装MySQL(mariadb)

    MySQL(mariadb) MariaDB数据库管理系统是MySQL的一个分支,主要由开源社区在维护,采用GPL授权许可. 开发这个分支的原因之一是:甲骨文公司收购了MySQL后,有将MySQL闭源 ...

  2. Django orm(1)

    目录 一.orm查询 1.1配置测试脚本 1.1.1Django终端打印SQL语句的配置 1.2单表操作 1.2.1创建数据 1.2.2修改数据 1.2.3删除数据 1.2.4查询数据 1.2.5计数 ...

  3. CSS3——制作带动画效果的小图片

    下了一个软件:ScreenToGif用来截取动态图片,终于可以展示我的小动图啦,嘻嘻,敲开心! main.html <!DOCTYPE html> <html lang=" ...

  4. cookie、session和会话保持

    1.会话 在程序中,会话跟踪是很重要的事情.理论上,一个已登录用户,在这次登录后进行的所有请求操作都应该属于同一个会话,而另一个用户的所有请求操作则应该属于另一个会话,二者不能混淆.例如,用户 A 在 ...

  5. sass、less中的scoped属性

    1.scoped 的实现原理 Vue中的Less 中的 scoped 属性的效果主要是通过 PostCss 实现的.代码示例: //编译前 <template> <div class ...

  6. luogu P1068 分数线划定 x

    P1068 分数线划定 题目描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对 所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根 据 ...

  7. Nginx动静分离-tomcat

    一.动静分离 1.通过中间件将动态请求和静态请求分离. 2.为什么? 分离资源,减少不必要的请求消耗,减少请求延时. 3.场景 还可以利用php,fastcgi,python 等方式 处理动态请求 # ...

  8. Internet History, Technology, and Security(week3)——History: The Web Makes it Easy to Use

    前言: 上周学习了第一个网络NSFnet,美国国家科学基金会(National Science Foundation,简称NSF)在全美国建立了6个超级计算机中心所互联的一个网络,这周继续学习网络的发 ...

  9. window 连接服务器工具

    Xshell xftp 下载网址 以上两个软件均免费, 只需要邮件激活即可. 其中 xshell 主要用来连接服务器,方便使用命令行.xftp 方便传输文件.

  10. RabbitMQ幂等性概念(七)

    幂等性是什么? 我们可以借鉴数据库的乐观锁机制 比如我们执行一条更新库存的sql语句update user set count=count-1,version=version+1 where vers ...