A Simple Problem with Integers

Time Limit:5000MS   Memory Limit:131072K
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.
 
题解:本题也是线段树系列的模板题之一,要求的是成段更新+懒惰标记。PS:原题的说明有问题,上面说的“C a b c”中的c的范围其实在int32之外,需要使用long long,否则定是WA,真心坑爹,连WA多次,还是在discuss中看到的原因。
 
稍微讲解下代码中的一些细节: step<<1 与 step<<1|1,意思分别是step*2 和step*+1,具体为什么,可以回去复习一下位运算
 
 #include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map> #define N 100010
#define M 15
#define mod 1000000007
#define mod2 100000000
#define ll long long
#define maxi(a,b) (a)>(b)? (a) : (b)
#define mini(a,b) (a)<(b)? (a) : (b) using namespace std; int n,q;
int x[N];
char ch;
int a,b,c; struct node
{
ll mark,sum;
}tree[*N]; void update(int l,int r,int i)
{
if(!tree[i].mark) return;
int mid=(l+r)/;
tree[*i].sum+=tree[i].mark*(ll)(mid-l+);
tree[*i+].sum+=tree[i].mark*(ll)(r-mid);
tree[*i].mark+=tree[i].mark;
tree[*i+].mark+=tree[i].mark;
tree[i].mark=; } ll query(int tl,int tr,int l,int r,int i)
{
if(tl>r || tr<l)
return ;
if(tl<=l && r<=tr)
return tree[i].sum;
update(l,r,i);
int mid=(l+r)/; return query(tl,tr,l,mid,*i)+query(tl,tr,mid+,r,*i+);
} void addd(int tl,int tr,int l,int r,int i,int val)
{
if(tl>r || tr<l)
return;
if(tl<=l && tr>=r){
tree[i].sum+=val*(ll)(r-l+);
tree[i].mark+=val;
return;
}
update(l,r,i);
int mid=(l+r)/;
addd(tl,tr,l,mid,*i,val);
addd(tl,tr,mid+,r,*i+,val);
tree[i].sum=tree[*i].sum+tree[*i+].sum;
} void build_tree(int l,int r,int i)
{
if(l==r){
tree[i].sum=x[l];
return;
}
int mid=(l+r)/;
build_tree(l,mid,*i);
build_tree(mid+,r,*i+);
tree[i].sum=tree[*i].sum+tree[*i+].sum;
} int main()
{
int i;
// freopen("data.in","r",stdin);
//scanf("%d",&T);
//for(int cnt=1;cnt<=T;cnt++)
//while(T--)
while(scanf("%d%d",&n,&q)!=EOF)
{
// printf(" %d %d \n",n,q);
for(i=;i<=n;i++) scanf("%d",&x[i]);
// printf(" 1\n");
memset(tree,,sizeof(tree));
build_tree(,n,); // printf(" 2\n");
getchar();
while(q--){
// printf(" 3\n");
scanf("%c",&ch);
// printf(" %c\n",ch);
if(ch=='C'){
//printf(" 31\n");
scanf("%d%d%d",&a,&b,&c);
getchar();
addd(a,b,,n,,c); }
else{
// printf(" 32\n");
scanf("%d%d",&a,&b);
getchar();
ll ans=query(a,b,,n,);
printf("%I64d\n",ans);
}
}
} return ;
}

最新文章

  1. Android WIFI 分析(一)
  2. HTML5第一讲
  3. 初试钓鱼工具Weeman+DNS欺骗的使用
  4. Beetl 1.25 发布,java模板引擎
  5. Spring MVC + MyBatis整合(IntelliJ IDEA环境下)
  6. CentOS mysql硬盘满了挂载阿里云硬盘
  7. U盘安装RedHat 5.3
  8. 数据库SQLiteDatabase
  9. 基于WCF大型分布式系统的架构设计
  10. Activiti5.16.4数据库表结构
  11. spring boot admin + spring boot actuator + erueka 微服务监控
  12. 【Qt编程】基于Qt的词典开发系列&lt;四&gt;--无边框窗口的缩放与拖动
  13. Pandas基本操作
  14. [蓝桥杯]PREV-22.历届试题_国王的烦恼
  15. [Laravel] 04 - Blade templates
  16. Python中列表(list)、字典(dict)排序的程序
  17. redis 基本性能测试说明
  18. 批量改名的多种方法stu_3_finished.jpg 去掉finished,stu_{1..20}_finished.jpg
  19. 浅析linux内核中timer定时器的生成和sofirq软中断调用流程(转自http://blog.chinaunix.net/uid-20564848-id-73480.html)
  20. Codeforces Beta Round #5 B. Center Alignment 模拟题

热门文章

  1. HDU 6069 Counting Divisors(区间素数筛法)
  2. Python-OpenCV:cv2.imread(),cv2.imshow(),cv2.imwrite()
  3. PyCharm如何配置断点调试功能
  4. shell脚本,awk如何处理文件中上下关联的两行。
  5. javase(14)_java基础增强
  6. java在线聊天项目 swt可视化窗口Design 重新设计好友列表窗口 增加菜单栏
  7. java在线聊天项目0.8版 实现把服务端接收到的信息返回给每一个客户端窗口中显示功能
  8. UIControlEvent
  9. 2018 CCF NOIP提高组&amp;&amp;普及组答案
  10. initcall机制