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=0,f=0;char ch=' ';while(!isdigit(ch)){f|=(ch=='-');ch=getchar();}while(isdigit(ch)){s=(s<<3)+(s<<1)+(ch^48);ch=getchar();}return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(long long x)
{if(x<0){putchar('-');x=-x;}if(x<10){putchar(x+'0');return;}write(x/10);putchar((x%10)+'0');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=1000005;
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=0;int PP=Pos;while(PP>0){Sum+=1LL*(1LL*(Pos+1)*S1[PP]-S2[PP]);PP-=lowbit(PP);}return Sum;}
}T;
int main()
{int i;R(n); R(m);for(i=1;i<=n;i++){R(a[i]);T.Ins(i,a[i]-a[i-1]);}for(i=1;i<=m;i++){int opt,a,b,Tag;R(opt); R(a); R(b);switch (opt){case 1:R(Tag);T.Ins(a,Tag);T.Ins(b+1,-Tag);break;case 2:Wl(1LL*T.Que(b)-1LL*T.Que(a-1));break;}}return 0;
}
/*
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
*/

View Code

转载于:https://www.cnblogs.com/gaojunonly1/p/10351984.html

一本通1548【例 2】A Simple Problem with Integers相关推荐

  1. 【线段树】【模板】讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值)

    [线段树][模板]讲解 + 例题1 HDU - 1754 I Hate It (点修改分数)+ 例题二 POJ - 3468 A Simple Problem with Integers(区间加值) ...

  2. poj 3243:A Simple Problem with Integers

    3243:A Simple Problem with Integers 查看 提交 统计 提示 提问 总时间限制:  5000ms  单个测试点时间限制:  2000ms  内存限制:  131072 ...

  3. poj3468 A Simple Problem with Integers

    http://acm.hust.edu.cn:8080/judge/problem/viewProblem.action?id=14607 题意:题目给你n个数,m个操作,接下来一行给你这n个数,接下 ...

  4. (线段树模板)A Simple Problem with Integers --POJ--3468

    链接: http://poj.org/problem?id=3468 代码: 1 #include<stdio.h> 2 #include<algorithm> 3 #incl ...

  5. POJ3468 A Simple Problem with Integers【线段树 成段更新+求和 lazy标志】

    用longlong替换__int64也成. #define LL long long 输入输出用%lld Problem: 3468   User: qq1203456195 Memory: 4284 ...

  6. A Simple Problem with Integers

    http://poj.org/problem?id=3468 http://acm.hdu.edu.cn/showproblem.php?pid=4267 C++版本一 /* *@Author: ST ...

  7. poj 3468 A Simple Problem with Integers(线段树区区)

    题目链接:  http://poj.org/problem?id=3468 题目大意:  给出N个数,和M次查询 C a b c  区间[a,b]的值都加上c Q a b     查询区间[a,b]值 ...

  8. A Simple Problem with Integers POJ - 3468(线段树+区间查询+区间修改+建树+懒惰标记模板)+(树状数组)

    题意: 有一个数组,有两种操作.1: Q a b 求[a,b]的和 2:C a b c 给[a,b] 的所有元素都加上c. 题目: You have N integers, A1, A2, ... , ...

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

    http://poj.org/problem?id=3468 题意: 给出一串数,每次在一个区间内增加c,查询[a,b]时输出a.b之间的总和. 思路: 总结一下懒惰标记的用法吧. 比如要对一个区间范 ...

  10. 线段树专辑—— pku 3468 A Simple Problem with Integers

    http://poj.org/problem?id=3468 典型的一道基于lazy传递的线段树题目,这题和一般题目不同的地方在于,它的每次操作不是简单的覆盖线段,而是累加.记得第一次写的时候纠结了好 ...

最新文章

  1. CSDN博文中完美地去掉图片水印、调整图片位置和大小
  2. WCF 附录 高级主题 系列文章
  3. 鸿蒙生态系统第一梯队,鸿蒙系统9月11日,将有望正式成为国际第三大手机操作生态系统...
  4. 可穿戴计算机硬件技术研究,可穿戴计算机硬件技术应用探究.doc
  5. 这是我见过的最全的训练数据集,没有之一!
  6. PyTorch框架学习十八——Layer Normalization、Instance Normalization、Group Normalization
  7. C#LeetCode刷题之#225-用队列实现栈(Implement Stack using Queues)
  8. Unity5.1 新的网络引擎UNET(十五) Networking 引用--下
  9. java 同步块原理_Java同步代码块和同步方法原理与应用案例详解
  10. 顶级分区软件Acronis Disk Director 11最新版官方下载
  11. 基于智能电网的电力线载波通信研究
  12. 用74161设计十二进制计数器
  13. c语言输入角度求sin,从键盘输入一个角度x,求10sin(x)的值 c语音编程
  14. 怎样理解OOP?OOP又是什么?
  15. 怎么把PWM信号转为模拟量
  16. 宋体能力从业的一些感悟
  17. python调用短信宝API发送短信(附python代码 易理解)
  18. linux清理日志 hack,Linux系统的LOG日志文件及入侵后日志的清除
  19. sudo dolphin_如何使用Dolphin在PC上玩Wii和GameCube游戏
  20. 客户:我考虑一下!销售高手教你该怎么逼单!

热门文章

  1. 智能服装:引爆2016智能穿戴新发展
  2. Nodejs使用ffi调用so库
  3. Win10 高分屏软件界面字体模糊问题解决
  4. CF1132D Stressful Training (binary search)(greedy)
  5. *CF1132D.Stressful Training(二分+队列+贪心)
  6. flac格式歌曲如何转换成mp3格式,flac转mp3详细图文教程
  7. html网页嵌入shiny,用Shiny生态快速搭建交互网页应用
  8. html怎么引用网页链接,浅谈网页中各种链接引用方法
  9. Latex 多图片排版--排版代码生成器
  10. 团队管理8--管理工作框架及技能图谱