一本通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 ...
随机推荐
- PAT A1076 Forwards on Weibo (30 分)——图的bfs
Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may ...
- FreeRTOS学习笔记--任务优先级
FreeRTOSConfig.h 中的常量configMAX_PRIORITIES的值就是任务优先级的最大数值,这个数值可以按照自己的需要改动,当然值越大,内核对内存的开销就越大,一般设置一个满足自己 ...
- docker核心概念(镜像、容器、仓库)及基本操作
概要 docker是一种linux容器技术.容器有效的将由单个操作系统挂管理的资源划分到孤立的组中,以便更好的在组之间平衡有冲突的资源使用需求.可简单理解为一种沙盒 .每个容器内运行一个应用,不同的容 ...
- VD: error VERR_FILE_NOT_FOUND
virtualbox制作的镜像文件如果移动了位置,比如从C盘移到D盘,那么再次打开时会提示找不到文件. 解决办法: 打开virtualbox,在“管理”菜单中打开“虚拟介质管理”,在“虚拟硬盘”选项卡 ...
- 通过chrome浏览器分析网页加载时间
今天趁着下班的时间看了下chrome浏览器的网页加载时间分析工具和相关文档,简单写点儿东西记录一下. 以百度首页加载为例,分析下一张图片1.jgp(就是背景图)的加载时间 看右侧的Timing标签,从 ...
- POJ1845
这还是一道综合了许多数论的知识点的,做完也涨了不少姿势 但还是因为约数和公式这个鬼东西去找了度娘 题意很简单,就是求\(A^B\)的约数之和\(mod\ 9901\). 但是这种题意越是简单的题目越是 ...
- vue 中使用 async/await 将 axios 异步请求同步化处理
1. axios 常规用法: export default { name: 'Historys', data() { return { totalData: 0, tableData: [] } }, ...
- VS2015 搭建 Asp.net core 开发环境
1.首先你得装个vs2015 并且保证已经升级至 update3及以上(此处附上一个vs2015带up3的下载链接: ed2k://|file|cn_visual_studio_enterprise_ ...
- 在线排错之curl命令详解
春回大地万物复苏,好久不来,向各位博友问好. 简介 cURL是一个利用URL语法在命令行下工作的文件传输工具,1997年首次发行.它支持文件上传和下载,所以是综合传输工具,但按传统,习惯称cURL为下 ...
- Ceph分布式存储-运维操作笔记
一.Ceph简单介绍1)OSDs: Ceph的OSD守护进程(OSD)存储数据,处理数据复制,恢复,回填,重新调整,并通过检查其它Ceph OSD守护程序作为一个心跳 向Ceph的监视器报告一些检测信 ...