You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
#define maxn 100004
ll a[maxn],c1[maxn],c2[maxn];
ll n,m;
ll lowbit(ll x){
return x&(-x);
}
void updata(ll index,ll x,ll val)
{
if(index==)
for(int i=x;i<=n;i+=lowbit(i))
c1[i]+=val;//建立树状数
else
for(int i=x;i<=n;i+=lowbit(i))
c2[i]+=(x-)*val;//区间改变
} ll getsum(ll x)
{
ll sum1=,sum2=;
for(int i=x;i>;i-=lowbit(i))//无论变不变化都可用来求和
sum1+=c1[i],sum2+=c2[i];//区间求和//(l,r)的和为getsum(r)-getsum(l-1)
return x*sum1-sum2;
} int main()
{
a[]=;
scanf("%lld%lld",&n,&m);
for(int i = ; i <= n; i++){
scanf("%lld",&a[i]);
ll t=a[i]-a[i-];
updata(,i,t);
updata(,i,t); //输入初值的时候,也相当于更新了值
}
char v;
ll ans1,ans2;
ll l,r,add;
for(int i=;i<=m;i++){
getchar();
scanf("%c",&v);
if(v=='C'){
scanf("%lld%lld%lld",&l,&r,&add);
updata(,l,add);
updata(,r+,-add);
updata(,l,add);
updata(,r+,-add);
}
else{
scanf("%lld%lld",&l,&r);
ans1=getsum(r);
ans2=getsum(l-);
printf("%lld\n",ans1-ans2);
}
}
return ;
}
 
 

A Simple Problem with Integers(树状数组区间变化和区间求和)的更多相关文章

  1. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  2. HDU 4267 A Simple Problem with Integers --树状数组

    题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val  操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...

  3. POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问

    今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...

  4. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  5. A Simple Problem with Integers_树状数组

    Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...

  6. luogu 2519 [HAOI2011]problem a 动态规划+树状数组

    发现每一次 $[b[i]+1,n-a[i]]$ 这个区间的分数必须相同,否则不合法. 而一个相同的区间 $[l,r]$ 最多只能出现区间长度次. 于是,就得到了一个 $dp:$ 将每一种区间的出现次数 ...

  7. 【ZOJ2112】【整体二分+树状数组】带修改区间第k大

    The Company Dynamic Rankings has developed a new kind of computer that is no longer satisfied with t ...

  8. POJ3468——树状数组支持两个区间操作

    题目:http://poj.org/problem?id=3468 推断过程可自己查,得式子:fixsum(x) = (x+1) * ∑(i=1,x)fi - ∑(i=1,x)i*fi; 其中 f 是 ...

  9. nyoj 123 士兵杀敌(四) 树状数组【单点查询+区间修改】

    士兵杀敌(四) 时间限制:2000 ms  |  内存限制:65535 KB 难度:5   描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战 ...

  10. 牛客小白月赛6 F 发电 树状数组单点更新 求区间乘积 模板

    链接:https://www.nowcoder.com/acm/contest/136/F来源:牛客网  HA实验是一个生产.提炼“神力水晶”的秘密军事基地,神力水晶可以让机器的工作效率成倍提升.   ...

随机推荐

  1. 【教程】Visual Studio2019安装教程

    Visual Studio正式版早就可以使用了,但是好多小伙伴还是不会用,今天小编就给大家带来了最新的安装教程和激活方法. Visual Studio 2019包含社区版(Community).专业版 ...

  2. sql查询 ——聚合函数

    --聚合函数 -- sum() -- 求和 select sum(age) from student; -- count() -- 求数量 -- 数据量 select count(*) as '数量' ...

  3. 关于真机测试root权限问题

    普通用户好像也无伤大雅: 输入后看到这样的有个美元符号,说明你的虚拟机获取不了权限,有权限的应该是root和#都有,这个时候你输入su 如果还是没有变化,看看你的虚拟机是不是android7.0,An ...

  4. crowdfunding项目02——server无法启动

    错误描述:在maven工程下使用tomcat启动项目时,一直报错,排除代码问题(可以打包成功) 原因:jar包在下载过程中网断或者其他原因发生错误,导致server服务无法启动(简单理解:jar包下载 ...

  5. java8 四大核心函数式接口Function、Consumer、Supplier、Predicate(转载)

    Function<T, R> T:入参类型,R:出参类型 调用方法:R apply(T t); 定义函数示例:Function<Integer, Integer> func = ...

  6. 1025 PAT Ranking (25分)

    1025 PAT Ranking (25分) 1. 题目 2. 思路 设置结构体, 先对每一个local排序,再整合后排序 3. 注意点 整体排序时注意如果分数相同的情况下还要按照编号排序 4. 代码 ...

  7. P1582 倒水(贪心 + lowbbit)

    https://www.luogu.com.cn/problem/P1582 #include <bits/stdc++.h> using namespace std; #define i ...

  8. 解决linux 中文乱码

    解决办法是在文件/etc/profile末尾添加一行 echo 'export LC_ALL="en_US.UTF-8"' >> /etc/profile source ...

  9. Android 获取手机的厂商、型号、Android系统版本号等工具类(转载)

    Android 获取手机的厂商.型号.Android系统版本号等工具类 1.获取手机制造厂商 2.获取手机型号 3.获取手机系统当前使用的语言 4.获取Android系统版本号 5.获取手机IMEI串 ...

  10. setInterval 和 setTimeout 定时器

    前端定时器 setInterval 和 setTimeout setInterval 循环执行 循环执行就是设置一个时间间隔,每过一段时间都会执行一次这个方法,直到这个定时器被销毁掉. 用法是setI ...