一本通1548【例 2】A Simple Problem with Integers
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的更多相关文章
- 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)
A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...
- POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)
A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...
- POJ 3468 A Simple Problem with Integers(线段树/区间更新)
题目链接: 传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Description Yo ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
- POJ3648 A Simple Problem with Integers(线段树之成段更新。入门题)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 53169 Acc ...
- 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 ...
- POJ 3468 A Simple Problem with Integers(线段树区间更新区间查询)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92632 ...
随机推荐
- 【UVA 11404】Palindromic Subsequence
UVA 11404 我用了最暴力的做法:考虑\(dp[i][j]\)表示\(S[i..j]\)的最长回文子序列的长度以及字典序最小的那个. 然后转移的时候如下处理:首先\(dp[i][j]\)要取\( ...
- kubernetes 里面pod时间修改
yaml文件中设置时区同步,只需要映射主机的“/etc/localtime”文件. apiVersion: extensions/v1beta1kind: Deploymentmetadata: na ...
- Luogu4770 NOI2018 你的名字 SAM、主席树
传送门 UPD:发现之前被smy误导的一个细节,改过来之后就AC了-- 一道比较套路的SAM题,虽然我连套路都不会-- 先考虑前\(68pts\),也就是\(l=1 , r=|S|\)的情况.我们对\ ...
- (转)Ubuntu无法找到add-apt-repository问题的解决方法
原文 网上查了一下资料,原来是需要 python-software-properties 于是 apt-get install python-software-properties 除此之外还要安装 ...
- Vue与Element走过的坑。。。。带上Axios
1.Axios中post传参数组(java后端接收数组) 虽然源数据本身就是数组,但是传参时会自动变成key:数值或者服务器无法接收的对象,如下 如果不仔细看,很容易认为这两种情况没毛病..(后端不背 ...
- npm install xxx --save-dev 与npm install xxx --save 的区别
正常情况下: 当你为你的模块安装一个依赖模块时 1.你得先安装他们(在模块根目录下npm install module-name) 2.连同版本号手动将他们添加到模块配置文件package.json中 ...
- 51Nod 1668 非010串
这是昨天上课ChesterKing dalao讲线代时的例题 当时看到这道题就觉得很水,记录一下后面两位的情况然后讨论一下转移即可 由于之前刚好在做矩阵题,所以常规的矩阵快速幂优化也很简单 好我们开始 ...
- SAAS云平台搭建札记: (一) 浅论SAAS多租户自助云服务平台的产品、服务和订单
最近在做一个多租户的云SAAS软件自助服务平台,途中遇到很多问题,我会将一些心得.体会逐渐分享出来,和大家一起探讨.这是本系列的第一篇文章. 大家知道,要做一个全自助服务的SAAS云平台是比较复杂的, ...
- Jlink使用技巧之合并烧写文件
前言 IAP(In-application-programming),即在应用中编程.当产品发布之后,可以通过网络方便的升级固件程序,而不需要拆机下载程序.IAP系统的固件一般由两部分组成,即Boot ...
- MGR主从不一致问题排查与修复
运行环境 linux:CentOS release 6.8 (Final) kernel:2.6.32-642.6.2.el6.x86_64 mysql Server version: 5.7.21- ...