1548:【例 2】A Simple Problem with Integers

题目描述

这是一道模板题。

给定数列 a[1],a[2],…,a[n],你需要依次进行 q 个操作,操作有两类:

  • 1 l r x:给定 l,r,x,对于所有 i∈[l,r],将 a[i] 加上 x(换言之,将 a[l],a[l+1],…,a[r] 分别加上 x);
  • 2 l r:给定 l,r,求 a[i]∑i=[l,r].​a[i] 的值(换言之,求 a[l]+a[l+1]+⋯+a[r] 的值)。

输入格式

第一行包含 2 个正整数 n,q,表示数列长度和询问个数。保证 1≤n,q≤10^6。
第二行 n 个整数 a[1],a[2],…,a[n],表示初始数列。保证 ∣a[i]∣≤10^6。
接下来 q 行,每行一个操作,为以下两种之一:

  • 1 l r x:对于所有 i∈[l,r],将 a[i] 加上 x;
  • 2 l r:输出 a[i]∑i=[l,r]​a[i] 的值。

保证 1≤l≤r≤n, ∣x∣≤10^6。

输出格式

对于每个 2 l r 操作,输出一行,每行有一个整数,表示所求的结果。

样例

样例输入

5 10
2 6 6 1 1
2 1 4
1 2 5 10
2 1 3
2 2 3
1 2 2 8
1 2 3 7
1 4 4 10
2 1 2
1 4 5 6
2 3 4

样例输出

15
34
32
33
50

数据范围与提示

对于所有数据,1≤n,q≤10^6, ∣a[i]∣≤10^6, 1≤l≤r≤n, ∣x∣≤10^6。

sol:树状数组模板题 想想怎么支持区间修改,

1)【区间修改单点查询】例如[L,R]这段区间+Tag,就是a[L]+Tag,a[R+1]-Tag

2)【区间修改区间查询】基于差分的思想 先想象一个d数组维护差分值 d[i]=a[i]-a[i-1],基于差分的思想

a[i]=d[1]+d[2]+···+d[i-1]+d[i],所以a[1~p]就是,其中d[1]用了p次,d[2]用了p-1次,

转化一下可得,所以我们可以维护两个前缀和,

S1[i]=d[i],S2[i]=d[i]*i

查询:位置Pos的前缀和就是(Pos+1)*S1中1到Pos的和 减去 S2中1到Pos的和,[L,R]=SS[R]-SS[L-1]

修改:[L,R]   S1:S1[L]+Tag,S1[R+1]-Tag   S2:S2[L]+Tag*L ,S2[R+1]-Tag*(R+1)

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int s=,f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-');
ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^);
ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(long long x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x<)
{
putchar(x+'');
return;
}
write(x/);
putchar((x%)+'');
return;
}
inline void writeln(long long x)
{
write(x);
putchar('\n');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) writeln(x)
const int N=;
int n,m,a[N];
struct BIT
{
long long S1[N],S2[N];
#define lowbit(x) ((x)&(-x))
inline void Ins(int Pos,int Tag)
{
int PP=Pos;
while(PP<=n)
{
S1[PP]+=Tag;
S2[PP]+=1LL*Pos*Tag;
PP+=lowbit(PP);
}
return;
}
inline long long Que(int Pos)
{
long long Sum=;
int PP=Pos;
while(PP>)
{
Sum+=1LL*(1LL*(Pos+)*S1[PP]-S2[PP]);
PP-=lowbit(PP);
}
return Sum;
}
}T;
int main()
{
int i;
R(n); R(m);
for(i=;i<=n;i++)
{
R(a[i]);
T.Ins(i,a[i]-a[i-]);
}
for(i=;i<=m;i++)
{
int opt,a,b,Tag;
R(opt); R(a); R(b);
switch (opt)
{
case :
R(Tag);
T.Ins(a,Tag);
T.Ins(b+,-Tag);
break;
case :
Wl(1LL*T.Que(b)-1LL*T.Que(a-));
break;
}
}
return ;
}
/*
input
5 10
2 6 6 1 1
2 1 4
1 2 5 10
2 1 3
2 2 3
1 2 2 8
1 2 3 7
1 4 4 10
2 1 2
1 4 5 6
2 3 4
output
15
34
32
33
50
*/

一本通1548【例 2】A Simple Problem with Integers的更多相关文章

  1. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  2. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

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

    题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS     Memory Limit: 131072K Description Yo ...

  4. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

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

  5. ACM: A Simple Problem with Integers 解题报告-线段树

    A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...

  6. poj3468 A Simple Problem with Integers (线段树区间最大值)

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

  7. POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)

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

  8. BZOJ-3212 Pku3468 A Simple Problem with Integers 裸线段树区间维护查询

    3212: Pku3468 A Simple Problem with Integers Time Limit: 1 Sec Memory Limit: 128 MB Submit: 1278 Sol ...

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

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

随机推荐

  1. 理解 DocumentFragment

    理解 DocumentFragment 含义:创建文档片段,它继承了Node的所有方法,对DOM操作性能非常好.创建文档片段 如下方法: var frag = document.createDocum ...

  2. QT 布局管理器的使用

    很多的时候,需要布局管理器的使用, 在此介绍一下布局管理器的使用,直接上代码 #include "widget.h" #include "ui_widget.h" ...

  3. android之WIFI小车编程详述

    有了前几篇wifi模块eps8266的使用,单片机设置eps8266程序,android TCP客户端,现在就做一个wifi小车 先上图 小车是四个轮子的,每个轮子上都有电机驱动,前进后退的时候四个轮 ...

  4. Foreach循环输出索引值

    循环输邮索引值,使用for是没有任何问题: class Bh { public string[] str { get; set; } public void TestFor() { ; i < ...

  5. 从一些代码方法中,去学习C#委托

    先来看看下面一个类中的一些方法: class Bc { public double Add(double number1, double number2) { return number1 + num ...

  6. kafka学习2:kafka集群安装与配置

    在前一篇:kafka学习1:kafka安装 中,我们安装了单机版的Kafka,而在实际应用中,不可能是单机版的应用,必定是以集群的方式出现.本篇介绍Kafka集群的安装过程: 一.准备工作 1.开通Z ...

  7. [Spark][Python]DataFrame select 操作例子

    [Spark][Python]DataFrame中取出有限个记录的例子 的 继续 In [4]: peopleDF.select("age")Out[4]: DataFrame[a ...

  8. aurora 64B/66B ip核设置与例程代码详解

    见网页https://blog.csdn.net/u014586651/article/details/84349328 https://blog.csdn.net/u012135070/articl ...

  9. JDK的一个关于stack的小bug

    在一个项目中,使用了一个java.util.Stack,总所周知,栈是先入后出的,那么遍历其中元素的时候,也应该按照这个顺序遍历才对,但是实际情况确不是,以下是测试代码. Stack stack = ...

  10. Terraform:创建 Azure 虚机

    笔者在前文<Terraform 简介>中简单介绍了 Terraform 相关的概念,本文让我们使用 Terraform 在 Azure 上创建一个虚机,以此来直观体验一下 Terrafor ...