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. Servlet接口应用(开发servlet三种方式)

    参见 文库/java/javaEE全新学习教程2.2节 1.通过URL调用 2通过提交表单 3超链接 4 javascript写一个函数,调用这个函数 1,首先在工程的WebRoot文件夹下建立一个j ...

  2. 4-拷贝我的eclipse写安卓的配置说明

    1.下载加压: 2.配置关于jdk的javahome路径,配置过eclipse的到这里就可以了,否则百度ecplise安装配置环境变量即可: 3.以安卓项目方式加入appcompat-v7; 4.每次 ...

  3. Mac anzhuangxgboost

    2. 从Github库安装XGBoost 第一步, 克隆最新的XGBoost到本地 git clone --recursive https://github.com/dmlc/xgboost 第二步, ...

  4. 对于 yii2 高级模板 生成文件入口

    安装的 advanced 模板web下是没有index.php 方法: 在advanced 目录下有个init.bat 应用程序  双击即可如下 查看advanced 目录 (刷新)如下 已有:

  5. jquery 遮罩层指定位置

    .css .datagrid-mask-msg { position: absolute; top: %; margin-top: -20px; padding: 12px 5px 10px 30px ...

  6. BBS后台发送邮件&修改文章

    一:Django发送邮件 在setting中配置 # EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST ...

  7. iPhone的home键进果汁了,按起来粘粘的感觉

    解决办法是按住home键转动一下,再用棉签蘸点水或者酒精都行(注意:水不要太多,不能让水渗进去),用棉签按压home 键多转几圈就好了.

  8. Debian7安装后的配置(英文环境chromium浏览器中汉字变成方块的问题)

    原文来自:http://www.programgo.com/article/3272573017/ 1.安装文泉宋体 sudo aptitude install xfonts-wqy sudo apt ...

  9. 常用Linux命令:netstat

    一.netstat:显示各种网络相关信息 1.命令格式 netstat [参数] 2.常用参数 -a   :(all)显示所有选项,默认不现实LISTEN相关 -t    :(tcp)仅显示tcp相关 ...

  10. markdown syntax

    Markdown 语法 转载自https://zh.mweb.im/markdown.html 首先应该了解的 每一个 Markdwon 使用者都应该了解的,是 Markdown 最基本的版本,也就是 ...