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. HDU 2141 Can you find it? [二分]

    Can you find it? Give you three sequences of numbers A, B, C, then we give you a number X. Now you n ...

  2. Netty(一):入门篇

    匠心零度 转载请注明原创出处,谢谢! 说在前面 上篇文章对Netty进行了初探:Netty初探,主要介绍了下我们为什么需要学习netty.netty介绍等:本篇文章接着上篇文章的内容.本篇为了方便大家 ...

  3. Python tutorial阅读之函数的定义与使用

    函数的定义 Python 使用关键字def定义函数,格式与C语言类似,但是没有返回类型,参数也不需要设置类型. def add(a, b): """这是函数的文档字符串& ...

  4. docker-compose快速搭建lnmp+redis服务器环境

    因为我用的是MacOS 安装docker sudo yum update sudo tee /etc/yum.repos.d/docker.repo <<-'EOF' [dockerrep ...

  5. 企业Nginx+Keepalived双主架构案例实战

    通过上一次课程的学习,我们知道Nginx+keepalived主从配置,始终有一台服务器处于空余状态,那如何更好的利用起来呢,我们需要借助Nginx+keepalived双主架构来实现,如下图通过改装 ...

  6. Linux下LNMP启动不了的问题总结(2015.05)

    [1] *****@*****-VirtualBox:~$ sudo /etc/init.d/mysql.server start Starting MySQL * Couldn't find MyS ...

  7. angular2-qrcode (转)

    插件选择 angular2-qrcode npm install angular2-qrcode --savecnpm install angular2-qrcode --save 参考github ...

  8. easyui验证扩展

    问题描述: 如上所示:当用户添加信息时,必须保证一个队伍一天只能有一条数据.所以在选择了报表日期的时候必须查询数据库里面当前队伍这一天的数据是否存在.如果不存在,即当前日期队伍没有数据,就可以进行数据 ...

  9. 一位IT男的7年工作经验总结

    一位IT男的7年工作经验总结 1.分享第一条经验:"学历代表过去.能力代表现在.学习力代表未来." 其实这是一个来自国外教育领域的一个研究结果.相信工作过几年.十几年的朋友对这个道 ...

  10. HashMap原理阅读

    前言 还是需要从头阅读下HashMap的源码.目标在于更好的理解HashMap的用法,学习更精炼的编码规范,以及应对面试. 它根据键的hashCode值存储数据,大多数情况下可以直接定位到它的值,因而 ...