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. centos7关闭运行的django项目

    1.查看django项目的端口对应的PID :sudo netstat -tulpn | grep :8000 2.杀死进程命令:kill -9 pid

  2. (转)Boyer-Moore算法

    转自:Boyer-Moore算法 一.简述 在当前用于查找子字符串的算法中,BM(Boyer-Moore)算法是当前有效且应用比较广的一中算法,各种文本编辑器的“查找”功能(Ctrl+F),大多采用B ...

  3. The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!

    Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use the ...

  4. 开发板上如何配置apahe2+mysql+php7

    1,安装apache2 sudo apt-get install apache2 修改webroot vim /etc/apache2/apache2.conf #在其中复制最后一个 <Dire ...

  5. Intellij IDEA 控制台中文乱码问题

    如果Intellij IDEA 控制台出现中文乱码: 1.修改Intellij IDEA 配置文件: 在安装目录的bin文件夹里找到 idea.exe.vmoptions 和 idea64.exe.v ...

  6. AcWing 898. 数字三角形

    //从上往下 #include <iostream> #include <algorithm> using namespace std; , INF = 1e9; int n; ...

  7. 牛客竞赛第二场D Kth Minimum Clique 贪心+bitmap

    Kth Minimum Clique 题意 给出n(n<100)个点的邻接表,和n个点的权值,求第k大的团(完全子图) 分析 n很小,并且好像没有什么算法和这个有关系,所以可以往暴力枚举的方向想 ...

  8. C. Grid game 思维+问题转化

    C. Grid game 思维+问题转化 题意 每当有一行或者一列方格的时候,都可以消气这一行或者这一列,一共有两种形状的方块,一种是横的两个,一种是竖着的两个,按时间顺序放在4*4的格子里面,问怎么 ...

  9. docker dial unix /var/run/docker.sock: connect: permission denied

    Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker. ...

  10. Lowest Common Multiple Plus 题解

    求n个数的最小公倍数. Input输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n个正整数. Output为每组测试数据输出它们的最小公倍数,每个测试实例的输出占一行.你可以假设最后的 ...