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 2296 Ring [AC自动机 DP 打印方案]

    Ring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submissio ...

  2. jenkins入门系列之一 jenkins的安装

    Jenkins是一个CI(持续集成环境)工具.它可以根据设定持续定期编译,运行相应代码:运行UT或集成测试:将运行结果发送至邮件,或展示成报告... 这样做的最终目的是: 让项目保持健康的状态.如果任 ...

  3. Python高级用法总结

    Python很棒,它有很多高级用法值得细细思索,学习使用.本文将根据日常使用,总结介绍Python的一组高级特性,包括:列表推导式.迭代器和生成器.装饰器. 列表推导(list comprehensi ...

  4. LeetCode - 492. Construct the Rectangle

    For a web developer, it is very important to know how to design a web page's size. So, given a speci ...

  5. 异步请求时有时会让js不起作用,那么重新加载js

    function reloadSmartMenu() { var jsElem = document.createElement('script'); jsElem.src= path+'/syste ...

  6. ConcurrenHashMap源码分析(二)

    本篇博客的目录: 一:put方法源码 二:get方法源码 三:rehash的过程 四:总结 一:put方法的源码 首先,我们来看一下segment内部类中put方法的源码,这个方法它是segment片 ...

  7. Linux常用命令手册

    Linux常用命令手册 NO 分类 PS1 命令名 用法及参数 功能注解 对应章节 1 文件管理 # ls ls -a 列出当前目录下的所有文件,包括以.头的隐含文件     文件管理 # ls ls ...

  8. 一致性哈希(附带C++实现)

    在分布式集群中,对机器的添加删除,或者机器故障后自动脱离集群这些操作是分布式集群管理最基本的功能.如果采用常用的hash(object)%N算 法,那么在有机器添加或者删除后,就需要大范围的移动原有数 ...

  9. Yii2中DAO

    数据库访问 (DAO) 创建数据库连接 执行 SQL 查询 引用表和列名称 执行事务 复制和读写分离 操纵数据库模式 Yii 包含了一个建立在 PHP PDO 之上的数据访问层 (DAO).DAO为不 ...

  10. python针对端口11211进行全网收集

    前言: 最近Memcached分布式系统DRDoS拒绝服务攻击 一夜之内流量暴增.各种网站给打挂.原先打算写 一个poc可惜失败了. 0x01 requests模块 0x02 去钟馗之眼注册一个账号, ...