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. centos 6 中恢复删除的文件

    CentOS 6 安裝 extundelete:: ##############测试的时候需要2块硬盘############### yum install e2fsprogs-devel e2fsp ...

  2. node.js 基础二 开启服务器监听

    1.server.js 2.监听 一 server.js 二 监听 运行server.js后,浏览器打开:http://localhost:8888/ //====================== ...

  3. ESP32 电容式触摸按键设计

    手指和电容器接触时,相当于增加了电容,电容增加量与总电容的商就是电容的变化幅值,如果这个幅值超过门限,就认为触摸按键被激发了:

  4. 大牛blog

    分布式: 分布式基础学习[一] —— 分布式文件系统 分布式基础学习[二] —— 分布式计算系统(Map/Reduce) Java分布式应用技术架构介绍

  5. Unity 消息管理(观察煮模式)

    一.首先定义一份消息号(消息号用来标记发出的每一条消息,接收者通过注册要监听的消息号来监听相应的消息) public enum MSG_IDS { NONE = -, MSG_TEST01 = , M ...

  6. excel保存为制表符分隔的文本文件 js无法完整读取

    excel保存为制表符分隔的文本文件 js无法完整读取 excel另存为文本有两个选项,一个是制表符分隔的文本文件,一个是unicode文本.生成的文件Unicode更大一些.但是这里需要注意的是[制 ...

  7. Luogu P1439 【模板】最长公共子序列

    又是模板题呵,但这次的难度有点增加. 先看题目第一个想到DP的经典算法,要O(n^2),然后想其它的算法. 其实我们衢州市一次联考有一题很像这题,不过还要难一点. 思想是离散化+最长不下降子序列(在这 ...

  8. docker部署rabbitMQ

    获取rabbit镜像: docker pull rabbitmq:management 创建并运行容器: docker run -d --hostname my-rabbit --name rabbi ...

  9. Nagios数据存储插件NDOUtils部署和测试

    1. 概述 NDOUTILS,Nagios Data Output Utils,Nagios数据输出工具,允许用户从Nagios导出状态和事件信息到数据库中,便于以后的检索和加工 它包括几个部分: N ...

  10. BugkuCTF SQL注入1

    前言 写了这么久的web题,算是把它基础部分都刷完了一遍,以下的几天将持续更新BugkuCTF WEB部分的题解,为了不影响阅读,所以每道题的题解都以单独一篇文章的形式发表,感谢大家一直以来的支持和理 ...