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. SQL Server CLR全功略之一---CLR介绍和配置

    Microsoft SQL Server 现在具备与 Microsoft Windows .NET Framework 的公共语言运行时 (CLR) 组件集成的功能.CLR 为托管代码提供服务,例如跨 ...

  2. 面试题:volatile关键字的作用、原理

    在只有双重检查锁,没有volatile的懒加载单例模式中,由于指令重排序的问题,我确实不会拿到两个不同的单例了,但我会拿到“半个”单例. 而发挥神奇作用的volatile,可以当之无愧的被称为Java ...

  3. 【Docker官方文档】理解Docker

    本文来自Docker的官方文档,详细介绍了Docker的体系结构.重要概念.内部工作机理等内容,推荐不了解Docker内部原理的同学阅读. 什么是Docker? Docker是一个用于开发.交付和运行 ...

  4. sublime text 3安装 vue插件

    1.上一个章节讲到Vue.js的环境安装,这一章节主要是针对ST3 如何安装vue插件,来快速的进行vue组件代码的编写. (内容转载自:https://www.cnblogs.com/bluedoc ...

  5. 7.python实现高效端口扫描器之nmap模块

    对于端口扫描,使用的最多的就是nmap这个工具,不想python已经强大到,提供了nmap这个扫描端口的模块. 本片文章主要介绍nmap模块的两个常用类: PortScanner()类,实现一个nma ...

  6. qt-vs-addin:Qt4和Qt5之VS插件如何共存与使用(转)

    原则上,两者是不可以同时存在的,但是如果都安装了,该如何分别使用他们呢? Qt4 Visual Studio Add-in:官网可以下载安装程序,qt-vs-addin-1.1.11-opensour ...

  7. 2014-4-2解决无法访问github和google的问题

    github是个好地方,但是上不去就蛋疼了. 今天github上不去,果断f12下,看下network,发现里面好多请求都是指向 github.global.ssl.fastly.net这个域名的,然 ...

  8. 微软日志工厂 Microsoft.Extensions.Logging 中增加 log4net 的日志输出

    前提: 需要nuget   Microsoft.Extensions.Logging.Log4Net.AspNetCore   2.2.6: 描述:解决 .net core 微软日志工厂 Micros ...

  9. 百度离线地图API开发V2.0版本

    全面介绍,请看下列介绍地址,改写目前最新版本的百度V2.0地图,已全面实现离线操作,能到达在线功能的95%以上 http://api.jjszd.com:8081/apituiguang/gistg. ...

  10. C#多线程编程实战1.3等待线程

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...