A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 89818   Accepted: 27967
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
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;
const int MAXN=;
struct Node{
ll sum,lazy;
int l,r;
};
struct SegmentTree{
private:
Node a[MAXN*];
public:
void build(int rt,int l,int r)
{
a[rt].l=l;
a[rt].r=r;
a[rt].lazy=;
if(l==r)
{
scanf("%lld",&a[rt].sum);
return ;
}
int mid=(a[rt].l+a[rt].r)>>;
build(rt<<,l,mid);
build(rt*+,mid+,r);
a[rt].sum=a[rt<<].sum+a[rt*+].sum;
}
void pushDown(int rt)
{
int mid=(a[rt].l+a[rt].r)>>;
a[rt<<].sum+=(mid-a[rt].l+)*a[rt].lazy;
a[rt*+].sum+=(a[rt].r-mid)*a[rt].lazy;
a[rt<<].lazy+=a[rt].lazy;
a[rt*+].lazy+=a[rt].lazy;
a[rt].lazy=;
}
void change(int rt,int l,int r,int val)
{
if(a[rt].l==l&&a[rt].r==r)
{
a[rt].sum+=(r-l+)*val;
a[rt].lazy+=val;
return ;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
{
change(rt<<,l,r,val);
}
else if(mid<l)
{
change(rt*+,l,r,val);
}
else
{
change(rt<<,l,mid,val);
change(rt*+,mid+,r,val);
}
a[rt].sum=a[rt<<].sum+a[rt*+].sum;
}
ll query(int rt,int l,int r)
{
if(a[rt].l==l&&a[rt].r==r)
{
return a[rt].sum;
}
if(a[rt].lazy!=)
{
pushDown(rt);
}
int mid=(a[rt].l+a[rt].r)>>;
if(r<=mid)
return query(rt<<,l,r);
else if(mid<l)
return query(rt*+,l,r);
else
{
return query(rt<<,l,mid)+query(rt*+,mid+,r);
}
}
};
SegmentTree solver;
int n,m;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
solver.build(,,n);
for(int i=;i<m;i++)
{
scanf("%*c");
char op;
scanf("%c",&op);
if(op=='Q')
{
int l,r;
scanf("%d%d",&l,&r);
printf("%lld\n",solver.query(,l,r));
}
else
{
int l,r,val;
scanf("%d%d%d",&l,&r,&val);
solver.change(,l,r,val);
}
}
} return ;
}

最新文章

  1. hdu 1106 排序(水题)
  2. PSP
  3. .Net分页实现
  4. 问题-PopupMenu是哪个控件调用弹出的?
  5. Windows XP系统安装SQL Server 2005(开发版)图解
  6. Oracle EBS-SQL (AR-1):检查应收收款发生额
  7. Javascript高级程序设计学习笔记一
  8. 【Alpha】Daily Scrum Meeting——Day5
  9. CentOS 7 安装Graphite
  10. makefile的命令包定义及使用
  11. Pandas逐行读取Dateframe并转为list
  12. 解决VS2015单元测试“未能设置用于运行测试的执行上下文”问题
  13. 二叉查找树及B-树、B+树、B*树变体
  14. Opencv基本数据类型
  15. More Effective C++ Item14:明智运用exception specifications
  16. socket 中午吃的啥 socket 并发服务器 fork
  17. Nodejs -- 使用koa2搭建数据爬虫
  18. [ios]&quot;The identity used to sign the executable is no longer valid&quot;错误解决方法
  19. nginx信号量
  20. jmeter+ant+jenkins+mac报告优化(二):添加90% Line和QPS

热门文章

  1. nginx学习之简化安装篇(一)
  2. AFN多文件进度下载
  3. 核函数 深度学习 统计学习 强化学习 神经网络 xx
  4. Bootstrap学习-网格系统
  5. Ext.Ajax的用法
  6. Python partial function 偏函数
  7. Redis高级进阶(二)
  8. 手机端网页web开发要点
  9. vim配置与使用
  10. systemd基本使用