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. 批量添加数据SqlBulkCopy

    using System.Data.SqlClient; class Program { static void Main() { string connectionString = GetConne ...

  2. 互联网+大数据解决方案(ppt)

    from: 互联网+大数据解决方案(ppt) 导读:大数据(bigdata),或称巨量资料,指的是所涉及的资料量规模巨大到无法透过目前主流软件工具,在合理时间内达到撷取.管理.处理.并整理成为帮助企业 ...

  3. HttpUploader2 -chrome 45+安装教程

    HttpUploader2-Chrome 45+安装说明 步骤如下: a.先安装HttpUploader2.exe,HttpUploader2.exe为插件集成安装包. b.再安装HttpUpload ...

  4. 为docker设置国内镜像

    docker的默认镜像(https://hub.docker.com/)地址,拉取镜像时是比较慢的,经常会超时,有时拉取几个小时.为了加快拉取的时间和速度,需要添加中国的镜像地址: 国内的加速地址: ...

  5. POJ-3481 Double Queue (splay)

    The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest, equipped w ...

  6. 三羊献瑞——第六届蓝桥杯C语言B组(省赛)第三题

    原创 三羊献瑞 观察下面的加法算式: 祥 瑞 生 辉 + 三 羊 献 瑞 ------------------- 三 羊 生 瑞 气 (如果有对齐问题,可以参看[图1.jpg]) 其中,相同的汉字代表 ...

  7. 使用JMeter测试基于WebSocket协议的服务

    使用JMeter测试基于WebSocket协议的服务 :first-child{margin-top:0!important}img.plugin{box-shadow:0 1px 3px rgba( ...

  8. 快速获取.NET DLL文件编译时间

    当用户现场汇报问题给我们, 我们比较关心的就有用户现场的DLL是什么版本号,是什么时候编译的. 有没有什么办法得到呢?办法是有的. 在网上找了很久终端找到这个软件非常地好用. 直接把文件拖到软件里就行 ...

  9. webrequest、httpwebrequest、webclient、HttpClient 四个类的区别

    一.在 framework 开发环境下: webrequest.httpwebreques  都是基于Windows Api 进行包装, webclient 是基于webrequest 进行包装:(经 ...

  10. MariaDB 数据库迁移

    一.为什么要迁移 我的七月小说站点放在JCloud上,恕我直言,配合我的Aliyun服务器进行数据交互,那是相当的慢,没办法,京东云上面十几块钱的公网ip,也就这样了. 所以我决定把web服务器和数据 ...