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 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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<vector>
#include<cmath> const int maxn=1e5+5;
typedef long long ll;
using namespace std; struct node
{
ll l,r,sum;
}tree[maxn<<2];
ll lazy[maxn<<2];
void pushup(int m)
{ tree[m].sum=tree[m<<1].sum+tree[m<<1|1].sum;
}
void pushdown(int m,int l)
{
if(lazy[m]!=0)
{
lazy[m<<1]+=lazy[m];
lazy[m<<1|1]+=lazy[m];
tree[m<<1].sum+=lazy[m]*(l-(l>>1));
tree[m<<1|1].sum+=lazy[m]*(l>>1);
lazy[m]=0;
}
}
void build(int m,int l,int r)
{
tree[m].l=l;
tree[m].r=r;
if(l==r)
{
scanf("%lld",&tree[m].sum);
return;
}
int mid=(l+r)>>1;
build(m<<1,l,mid);
build(m<<1|1,mid+1,r);
pushup(m);
}
void update(int m,int l,int r,int val)
{
if(tree[m].l==l&&tree[m].r==r)
{
lazy[m]+=val;
tree[m].sum+=(ll)val*(r-l+1);
return;
}
if(tree[m].l==tree[m].r)
return;
int mid=(tree[m].l+tree[m].r)>>1;
pushdown(m,tree[m].r-tree[m].l+1);
if(r<=mid)
{
update(m<<1,l,r,val);
}
else if(l>mid)
{
update(m<<1|1,l,r,val);
}
else
{
update(m<<1,l,mid,val);
update(m<<1|1,mid+1,r,val);
}
pushup(m);
}
ll query(int m,int l,int r)
{
if(tree[m].l==l&&tree[m].r==r)
{
return tree[m].sum;
}
pushdown(m,tree[m].r-tree[m].l+1);
int mid=(tree[m].l+tree[m].r)>>1;
ll res=0;
if(r<=mid)
{
res+=query(m<<1,l,r);
}
else if(l>mid)
{
res+=query(m<<1|1,l,r);
}
else
{
res+=(query(m<<1,l,mid)+query(m<<1|1,mid+1,r)); }
return res; }
int main()
{
std::ios::sync_with_stdio(false);
std::cin.tie(0);
int n,m;
cin>>n>>m;
build(1,1,n); char op[2];
int l,r,val;
for(int t=0;t<m;t++)
{
scanf("%s",op);
if(op[0]=='Q')
{
scanf("%d%d",&l,&r);
printf("%lld\n",query(1,l,r));
}
else
{
scanf("%d%d%d",&l,&r,&val);
update(1,l,r,val);
}
} return 0;
}

A Simple Problem with Integers(线段树区间更新复习,lazy数组的应用)-------------------蓝桥备战系列的更多相关文章

  1. poj 3468 A Simple Problem with Integers (线段树区间更新求和lazy思想)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 75541   ...

  2. (简单) POJ 3468 A Simple Problem with Integers , 线段树+区间更新。

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  3. A Simple Problem with Integers 线段树 区间更新 区间查询

    Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 115624   Accepted: 35897 Case Time Lim ...

  4. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

  5. POJ 3468A Simple Problem with Integers(线段树区间更新)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 112228 ...

  6. poj 3468 A Simple Problem with Integers 线段树区间更新

    id=3468">点击打开链接题目链接 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072 ...

  7. POJ 3468 A Simple Problem with Integers(线段树,区间更新,区间求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 67511   ...

  8. POJ 3468 A Simple Problem with Integers(线段树区间更新)

    题目地址:POJ 3468 打了个篮球回来果然神经有点冲动. . 无脑的狂交了8次WA..竟然是更新的时候把r-l写成了l-r... 这题就是区间更新裸题. 区间更新就是加一个lazy标记,延迟标记, ...

  9. POJ 3468 A Simple Problem with Integers(线段树区间更新,模板题,求区间和)

    #include <iostream> #include <stdio.h> #include <string.h> #define lson rt<< ...

随机推荐

  1. 登录xdebug

    1.配置 2.先不打断点,调至登录页面 3.在登录必经过处打断点,访问页面输入账号密码点击登录,进入代码追踪模式

  2. 专题2-通过按键玩中断\第1课-中断处理流程深度剖析-lesson1

    中断概念 1.中断生命周期 串口先产生一个事件,该事件传送到中断控制器里面,中断控制器会进行相应过滤,能通过过滤,那么就交给CPU去处理. 2.中断源 2440芯片手册 6410芯片手册 3.中断过滤 ...

  3. spark源码阅读之network(1)

    spark将在1.6中替换掉akka,而采用netty实现整个集群的rpc的框架,netty的内存管理和NIO支持将有效的提高spark集群的网络传输能力,为了看懂这块代码,在网上找了两本书看< ...

  4. 2.python IP/DNS地址处理之IPy/Dnspython模块

     1.IPy模块 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等,即便是专业的网络人员也要进行繁琐的计算,而IPy模块提供了专门针对IPV4地址与IPV6 ...

  5. Linux下的fdlisk - l 用法解析-入门篇

    fdlisk - l 的含义是查看linux下面的磁盘分区大小.这个大小包含了很多信息. 我们来看度娘的一则介绍: FDISK进行硬盘分区从实质上说就是对硬盘的一种格式化.当我们创建分区时,就已经设置 ...

  6. Terminologies in MVC: Part 2 (Razor Engine Syntax vs Web Form)

    By Abhishek Jaiswal :) on Mar 21, 2015 In this article we learn about Razor Engine Syntax vs Web For ...

  7. 应用Bundle捆绑压缩技术

    从MVC4开始,我们就发现,项目中对Global.asax进行了优化,将原来在MVC3中使用的代码移到了[App_Start]文件夹下,而Global.asax只负责初始化.其中的BundleConf ...

  8. Windows Server 2003 asp网页不能访问的常见问题

    1. [开始]--[程序]--[管理工具]--[Internet信息服务管理器],在服务器名下的“web服务扩展”的右窗口,单击active server pages -> 单击[允许].2. ...

  9. Snapshot--使用脚本创建快照

    USE master; SET NOCOUNT ON; GO ); --数据库名 );--快照名 );--保存路径 SET @dbname='DB1'; SET @snapname='DB1_SNAP ...

  10. The 'microsoft.jet.oledb.4.0' provider is not registered on the local machin

    1,2选取目标站点,然后3的高级设置,4启用32位的应用程序的属性变为true就可以了.当然,网络上还有其他的版本,你也可以尝试下. 原文地址:http://weblogs.asp.net/kenco ...