A Simple Problem with Integers

Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 163977   Accepted: 50540
Case Time Limit: 2000MS

Description

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.
 
题意:  Q查询区间和;C,将区间[x,y]的数都加上z
 
#include<iostream>
#include<algorithm>
#include<string.h>
#include<string>
#define ll long long
using namespace std;
ll tree[], lazy[], len[];//tree[num]存的是节点num所在区间的区间和
void pushdown(ll num)
{
if (lazy[num] != )
{
tree[num * ] = tree[num * ] + lazy[num] * len[num * ];
tree[num * + ] = tree[num * + ] + lazy[num] * len[num * + ];
lazy[num * ] = lazy[num * ] + lazy[num];
lazy[num * + ] = lazy[num * + ] + lazy[num];
lazy[num] = ;
}
} void build(ll num, ll le, ll ri)
{
len[num] = ri - le + ;//区间长度
if (le == ri)
{
scanf("%lld", &tree[num]);
return;
}
ll mid = (le + ri) / ;
build(num * , le, mid);
build(num * + , mid + , ri);
tree[num] = tree[num * ] + tree[num * + ];
} void update(ll num, ll le, ll ri, ll x, ll y, ll z)
{
if (x <= le && ri <= y)
{
lazy[num] = lazy[num] + z;
tree[num] = tree[num] + len[num] * z;//更新区间和
return;
}
pushdown(num);
ll mid = (le + ri) / ;
if (x <= mid)
update(num * , le, mid, x, y, z);
if (y > mid)
update(num * + , mid + , ri, x, y, z);
tree[num]=tree[num*]+tree[num*+];
} ll query(ll num, ll le, ll ri, ll x, ll y)
{
if (x <= le && ri <= y)//查询区间在num节点所在区间内
return tree[num];
pushdown(num);
ll mid = (le + ri) / ;
ll ans = ;
if (x <= mid)
ans = ans + query(num * , le, mid, x, y);
if (y > mid)
ans = ans + query(num * + , mid + , ri, x, y);
return ans;
}
int main()
{
ll n, m;
scanf("%lld%lld", &n, &m);
build(, , n);
while (m--)
{
char c[];
scanf("%s", c);
if (c[] == 'Q')
{
ll x, y;
scanf("%lld%lld", &x, &y);
printf("%lld\n", query(, , n, x, y));
}
else
{
ll x, y, z;
scanf("%lld%lld%lld", &x, &y, &z);
update(, , n, x, y, z);
}
}
return ;
}

最新文章

  1. python的os模块
  2. tomcat找不到class的情况分析
  3. 通过js的console优雅的将php调试信息输出
  4. jquery mobile 和phonegap开发总结之三跨域加载页面
  5. SQL Server中,Numric,Decimal,Money三种字段类型的区别
  6. SUSE Linux 防火墙设置
  7. C# WinForm动态添加MSChart控件
  8. MySQL、SqlServer、Oracle三大主流数据库分页查询
  9. Android学习7--日志信息的使用
  10. JMS 问题java.lang.NoClassDefFoundError: weblogic/security/acl/UserInfo
  11. 4-20ma电流信号转0-5v()
  12. Android远程桌面助手(B1332)之文件管理器
  13. v8 GC机制
  14. poj2411 状态压缩-铺地板题型-轮廓线解法(最优)
  15. C# 多线程 Parallel.For 和 For 谁的效率高?那么 Parallel.ForEach 和 ForEach 呢?
  16. Python_命名空间和作用域_25
  17. Sprinig泛型依赖注入
  18. window.onbeforeunload() 事件调用ajax
  19. npm WARN saveError ENOENT: no such file or directory
  20. mysql导入source注意点

热门文章

  1. 为什么很多 Android 程序喜欢在存储卡根目录建文件夹来存储数据而不是 Android/data 目录下?
  2. Linux centosVMware运行告警系统、分发系统-expect讲解、自动远程登录后,执行命令并退出、expect脚本传递参数、expect脚本同步文件、指定host和要同步的文件、shell项目-分发系统-构建文件分发系统、分发系统-命令批量执行
  3. JSTL fn:replace()函数替换 换行符
  4. How to recover if NMC cound not connect
  5. Java生鲜电商平台-小程序或者APP拼团功能设计与架构实战
  6. python人脸对比
  7. python如何在文件每一行前面加字符串?
  8. 网络编程之TCP三次握手,四次断开
  9. Python自动化运维的职业发展道路(暂定)
  10. Linux系统资深运维工程师的进阶秘籍