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<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define maxn 100005
long long sum[maxn<<],add[maxn<<];
void pushup(int rt) {
sum[rt]=sum[rt<<]+sum[rt<<|];
}
void pushdown(int rt ,int l) {
if (add[rt]) {
add[rt<<]+=add[rt];
add[rt<<|]+=add[rt];
sum[rt<<]+=add[rt]*(l-(l>>));
sum[rt<<|]+=add[rt]*(l>>);
add[rt]=;
}
}
void build(int l,int r,int rt) {
if (l==r) {
scanf("%lld",&sum[rt]);
return ;
}
int m=(l+r)>>;
build(l,m,rt<<);
build(m+,r,rt<<|);
pushup(rt);
}
void updata(int x,int y,int z,int l,int r,int rt) {
if (x<=l && r<=y) {
add[rt]+=z;
sum[rt]+=(long long)z*(r-l+);
return ;
}
pushdown(rt,r-l+);
int m=(l+r)>>;
if (x<=m) updata(x,y,z,l,m,rt<<);
if (y>m) updata(x,y,z,m+,r,rt<<|);
pushup(rt);
}
long long query(int x,int y,int l,int r,int rt) {
long long ans=;
if (x<=l && r<=y ) return sum[rt];
pushdown(rt,r-l+);
int m=(l+r)>>;
if (x<=m) ans+=query(x,y,l,m,rt<<);
if (y>m ) ans+=query(x,y,m+,r,rt<<|);
return ans;
}
int main() {
int n,q;
while(scanf("%d%d",&n,&q)!=EOF) {
build(,n,);
char b[];
int x,y,z;
while(q--) {
scanf("%s",b);
if (b[]=='Q') {
scanf("%d%d",&x,&y);
printf("%lld\n",query(x,y,,n,));
} else {
scanf("%d%d%d",&x,&y,&z);
updata(x,y,z,,n,);
}
}
}
return ;
}

A Simple Problem with Integers~POJ - 3468的更多相关文章

  1. A Simple Problem with Integers poj 3468 多树状数组解决区间修改问题。

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

  2. A Simple Problem with Integers POJ - 3468 (分块)

    题目链接:https://cn.vjudge.net/problem/POJ-3468 题目大意:区间加减+区间查询操作. 具体思路:本来是一个线段树裸题,为了学习分块就按照分块的方法做吧. 分块真的 ...

  3. C - A Simple Problem with Integers - poj 3468(区间更新)

    题意:有一个比较长的区间可能是100000.长度, 每个点都有一个值(值还比较大),现在有一些操作,C abc, 把区间a-b内全部加上c, Qab,求区间ab的值. 分析:很明显我们不可能对区间的每 ...

  4. C - A Simple Problem with Integers POJ - 3468 线段树模版(区间查询区间修改)

    参考qsc大佬的视频 太强惹 先膜一下 视频在b站 直接搜线段树即可 #include<cstdio> using namespace std; ; int n,a[maxn]; stru ...

  5. A Simple Problem with Integers POJ - 3468 (线段树)

    思路:线段树,区间更新,区间查找 #include<iostream> #include<vector> #include<string> #include< ...

  6. A Simple Problem with Integers POJ - 3468 线段树区间修改+区间查询

    //add,懒标记,给以当前节点为根的子树中的每一个点加上add(不包含根节点) // #include <cstdio> #include <cstring> #includ ...

  7. C - A Simple Problem with Integers

    C - A Simple Problem with Integers POJ - 3468   思路:线段树区间修改区间查询.又出现了 C++ WA    G++ AC的尴尬局面. #include& ...

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

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

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

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

随机推荐

  1. python基础 列表 的使用

    列表 首先定义一个列表 声明列表 列表名字=[值1,值2] list=[1,2,3,4,5] 这是一个列表,列表中有五个元素(1,2,3,4,5) 显示list列表 print   list 输出   ...

  2. hdu 5909 Tree Cutting [树形DP fwt]

    hdu 5909 Tree Cutting 题意:一颗无根树,每个点有权值,连通子树的权值为异或和,求异或和为[0,m)的方案数 \(f[i][j]\)表示子树i中经过i的连通子树异或和为j的方案数 ...

  3. CF698C. LRU [容斥原理 概率]

    CF698C. LRU 题意:n种物品,大小为k的队列,\(p_i\)的概率选择第i种物品放入队尾,如果已经有i了就不放了.队列大小>k时弹出队首.求\(10^{100}\)次操作后每种物品在队 ...

  4. Laravel5.5核心架构理解

    1.依赖注入 方法传入组件名,框架会自动实例化,方法内可直接使用 例如最常用的requert对象 2.服务容器 其实,Laravel 的核心就是一个 IoC 容器,Laravel 的核心本身十分轻量, ...

  5. 使用Python的requests库进行接口测试——session对象的妙用

    from:http://blog.csdn.net/liuchunming033/article/details/48131051 在进行接口测试的时候,我们会调用多个接口发出多个请求,在这些请求中有 ...

  6. SSE图像算法优化系列十六:经典USM锐化中的分支判断语句SSE实现的几种方法尝试。

    分支判断的语句一般来说是不太适合进行SSE优化的,因为他会破坏代码的并行性,但是也不是所有的都是这样的,在合适的场景中运用SSE还是能对分支预测进行一定的优化的,我们这里以某一个算法的部分代码为例进行 ...

  7. React项目模板-从项目搭建到部署

    前一段时间做了一个小项目,时间比较紧,就一个人月.最终希望能够通过微信公众号链接启动应用. 项目的业务细节就不多说了,主要是想分享一下做这个项目技术方面的一些经验. 技术选型 参考范围大致三种:Ang ...

  8. Centos 6.9--配置python3.5

    安装python3.5可能使用的依赖 yum install openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlit ...

  9. 在CentOS 7中安装Jetty服务器

    Jetty 是一款纯Java的HTTP (Web) 服务器和Java Servlet容器. 通常在更大的网络框架中,Jetty经常用于设备间的通信,而其他Web服务器通常给"人类" ...

  10. Windows 系统下安装 dig 命令

    dig是一个Linux下用来DNS查询信息的小工具,dig全称是Domain Information Groper,与nslookup类似,但比nslookup功能更强大.Windows只有nsloo ...