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. POJ2387(dijkstra堆优化)

    Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...

  2. Android 调用系统相机拍照并获取原图

    第一步:调用相机 Intent getImageByCamera = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); St ...

  3. 异步方法(promise版)出错自调用

    /** * [*promisePlus promise封装的异步,既然是异步,必然会成功或者失败,理论上失败了就失败了,但是 * 失败后能否让他过多长时间自动再调用自己呢,如果调用指定的次数还是失败, ...

  4. 18-(基础入门篇)GPRS(Air202)拨打电话--(由于板子做修订,所以暂停更新)

    https://www.cnblogs.com/yangfengwu/p/9968883.html 这个直接用官方给的demo就可以 先睹为快 现在说个需求哈,是当初一个人给提出的需求 例如存入的号码 ...

  5. python中#!/usr/bin/python与#!/usr/bin/env python的区别

    目的是在运行python脚本的时候告诉操作系统我们要用python解释器去运行py脚本 所以我们在第一句往往会写如下两句中的其中一句: #!/usr/bin/python 或 >#!/usr/b ...

  6. ASP.NET的生命周期

    我主要参考了这些文章 ASP.NET应用程序与页面生命周期, IIS处理Asp.net请求和 Asp.net页面生命周期 asp.net页面的生命周期 页面生命周期开始 (一)页面生命周期的主要阶段包 ...

  7. 【LeetCode106】Construct Binary Tree from Inorder and Postorder Traversal★★

    1.题目 2.思路 思路和LeetCode105类似,见上篇. 3.java代码 //测试 public class BuildTreeUsingInorderAndPostorder { publi ...

  8. Luogu P3959 宝藏

    这道题正解是状压DP,不过我不会所以写一下随机化算法来骗骗分. 听说当时考场上就有很多写prim然后挂掉的神仙,其实这道题是可以prim过的 prim是一种基于贪心的算法,在本题中由于盲目的选择当前最 ...

  9. Centos 6.9下部署Oracle 11G数据库环境的操作记录

    操作系统:Centos6.9(64Bit)Oracle:11g .11.2.0.4.0版本Ip地址:172.16.220.139 废话不多说了,下面记录安装过程:1)安装桌面环境 [root@vm01 ...

  10. C-代码笔记-输入输出

    .ACSII 字符实质和整数存储方式相同 //2018年9月16日01:35:54 # include <stdio.h> int main(void) { '; // printf(&q ...