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. django的自定义约束

    准备阶段 在settings中的INSTALLED_APPS配置当前app,不然django无法找到自定义的simple_tag. 在app中创建templatetags模块(模块名只能是templa ...

  2. python补充之进制转换、exec、eval、compile

    目录 eval.exec和compile 1.eval函数 2.exec函数 eval()函数和exec()函数的区别 python中的进制转换 eval.exec和compile 1.eval函数 ...

  3. css3 宽度百分比减去固定宽度 无效问题

    一定要注意中间横线的间距才有效果 正确 width: calc(50% - 10px); 错误 width:calc(50%-10px);

  4. java 生成透明背景图片

    //开始绘图 graphics2d.setBackground(Color.WHITE); graphics2d.clearRect(0, 0, width, height); graphics2d. ...

  5. OpenGL立方体

    直接画 #include <windows.h> #include <GL/glut.h> #include <stdio.h> #include <stri ...

  6. css-按钮中有图片和文字,怎么才能让文字和图片都中??

    <div class="btn1 trans" > <img src="../../images/img/add.png"/> < ...

  7. webkit内核的浏览器常见7种分别是..

    Google Chrome Safari 遨游浏览器 3.x 搜狗浏览器 阿里云浏览器 QQ浏览器 360浏览器 ...

  8. Python Flask框架入门

    序言 Flask封装功能不及Django完善,性能不及Tornado,但是Flask的第三方开源组件比丰富. 如果你是一个追求极简风格的完美主义者,那么Flask适合你. 资料

  9. asp.net实现大视频上传

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag 客户端每次提交下载请求时,服务 ...

  10. JS onclick中this用法

    当在dom元素中使用onclick绑定事件的时候,可以使用this来指向该元素对象. 打印输出的内容为: 所以可以通过该this对象来获取子元素 //通过element获取该对象下的一个audio标签 ...