A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions:147133   Accepted: 45718
Case Time Limit: 2000MS

Description

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<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring> using namespace std;
int n,m;
const int N=1e5+;
long long s1[N],s2[N];
int lowbit(int x)
{
return x&(-x);
} void updata(int p,long long x)
{
for(int i=p;i<=n;i+=lowbit(i)){
s1[i]+=x;
s2[i]+=x*p;
}
}
long long sum(int p)
{
long long ans=;
for(int i=p;i>;i-=lowbit(i)){
ans+=s1[i]*(p+)-s2[i];
}
return ans;
}
int main()
{
while(scanf("%d %d",&n,&m)==){
memset(s1,,sizeof(s1));
memset(s2,,sizeof(s2));
for(int i=;i<=n;i++){
long long x;
scanf("%lld",&x);
updata(i,x);
updata(i+,-x);
}
while(m--){
char s[];
scanf("%s",s);
if(s[]=='C'){
int a,b;
long long c;
scanf("%d %d %lld",&a,&b,&c);
updata(a,c);
updata(b+,-c);
}
else{
int a,b;
scanf("%d %d",&a,&b);
printf("%lld\n",sum(b)-sum(a-));
}
}
}
return ;
}
 

最新文章

  1. sql server 查询表结构
  2. poj1062昂贵的聘礼(Dijkstra**)
  3. JavaScript-闭包注意事项
  4. Android-ImageView.ScaleType
  5. Python超简单的HTTP服务器
  6. OpenGL入门学习(转)
  7. DreamWeaver文件保存时,提示&quot;发生共享违例&quot;问题的解决方法
  8. C#枚举器接口IEnumerator的实现
  9. 动态可视化库Vis.js:社交关系谱
  10. (iOS)Storyboard/xib小技巧
  11. MicroStrategy笔试
  12. Linux学习 -- 用户和用户组管理
  13. P3197 [HNOI2008]越狱
  14. DFS算法(——模板习题与总结)
  15. CSS-技巧
  16. web环境中微信JS-SDK配置
  17. java实现文件的断点续传的下载
  18. &lt;转&gt;python字典排序 关于sort()、reversed()、sorted()
  19. spark 与 Hadoop 融合后 Neither spark.yarn.jars nor spark.yarn.archive is set
  20. CodeM资格赛5

热门文章

  1. 阿里云centOS7.4上MySql5.6安装
  2. mysql使用数据库
  3. shutil.rmtree()
  4. Docker Nginx安装(centos7)
  5. 转载:CSS的组成,三种样式(内联式,嵌入式,外部式),优先级
  6. 2.3 摄像头驱动_vivi驱动程序分析
  7. Flask基本介绍
  8. 最小生成树——Kruscal(克鲁斯卡尔算法)
  9. 关于verilog中的signed类型
  10. Touch table