A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions: 142198   Accepted: 44136
Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.

Source

题意:给一个数列,Q个操作,遇到C则往[l,r]中加上x,遇到Q则求[l,r]的和
题解:树状数组做法
树状数组基本功能是实现单点更新和求前缀和,本题是要实现快速实现区间的更新。
首先,我们来看,在[l,r]上加上x的话,i<l时,所求的前缀和不变,l<=i<=r时,所求的前缀和增加了x*(i-l+1)=x*i-x*(l-1);
i>r时,增加了x*(r-l+1)=x*r-x*(l-1);
这时我们定义两个树状数组
bit1:前缀和树状数组
bit0:加法树状数组
从上面的前缀和增加量来看,从大于L后的前缀和都会增加一个东西,就是-x*(l-1),那么维护这个东西就是updata(bit0,l,-x*(l-1));
这样就实现了l以后的前缀和都会加上这个东西,然后我们发现x*r这个也可以提前维护,同理就是updata(bit0,r,x*r);但是这里还差
一个x*i;因为i是变量,所以这个只能在求和的时候去加上,而加法数组中{updata(bit1,l,x);updata(bit1,r,x);}我们发现这个刚
好实现了在[l,r]的前缀和中加上x,那么对于每个所求的i,在求前缀和的时候就*i就行了
则sum[i]=sum(bit1,i)*i+sum(bit0,i);
代码:
#include<iostream>
#include<string.h>
using namespace std;
typedef long long ll;
#define MAX 100005
int n,q;
int a[MAX];
ll bit0[MAX],bit1[MAX];
void updata(ll *b,int i,int val)
{
while(i<=n)
{
b[i]+=val;
i+=i&-i;
}
}
ll query(ll *b,int i)
{
ll res=;
while(i>)
{
res+=b[i];
i-=i&-i;
}
return res;
}
int main()
{
ios_base::sync_with_stdio();
cin.tie();
cout.tie();
while(cin>>n>>q)
{
memset(bit0,,sizeof(bit0));
memset(bit1,,sizeof(bit1));
for(int i=;i<=n;i++)
{
cin>>a[i];
updata(bit0,i,a[i]);
}
while(q--)
{
int l,r,x;
char ch;
cin>>ch;
cin>>l>>r;
if(ch=='C')
{
cin>>x;
updata(bit0,l,-x*(l-));
updata(bit1,l,x);
updata(bit0,r+,x*r);
updata(bit1,r+,-x);
}
else
{
ll sum=;
sum+=query(bit0,r)+query(bit1,r)*r;
sum-=query(bit0,l-)+query(bit1,l-)*(l-);
cout<<sum<<endl;
}
}
}
}

参考博客:https://www.cnblogs.com/detrol/p/7586083.html

https://blog.csdn.net/Puppet__/article/details/79098936

poj3468 A Simple Problem with Integers (树状数组做法)的更多相关文章

  1. POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问

    今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...

  2. A Simple Problem with Integers(树状数组HDU4267)

    A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...

  3. POJ3468 A Simple Problem with Interger [树状数组,差分]

    题目传送门 A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 1 ...

  4. HDU 4267 A Simple Problem with Integers --树状数组

    题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val  操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...

  5. A Simple Problem with Integers_树状数组

    Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...

  6. poj3468 A Simple Problem with Integers(线段树/树状数组)

    Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...

  7. 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和

    poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...

  8. kuangbin专题七 POJ3468 A Simple Problem with Integers (线段树或树状数组)

    You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. One type of op ...

  9. poj3468 A Simple Problem with Integers (线段树区间最大值)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 92127   ...

随机推荐

  1. GeneXus笔记本——部分环境属性设置项

    这些属性的设置是我们在做项目的过程中都会设置的属性 当然也因项目而异 这里也只是单纯的记录一下 知识库 属性设置“Maximun numeric length" 效果:设置数值型最大值 版本 ...

  2. 免费资源(CDN,顶级域名)汇集

    CloudFlare:免费CDN,需要将域名指向到cloudflare服务器.付费的可以使用二级域名 https://www.cloudflare.com/ Freenom:freenom会提供免费提 ...

  3. Sass:RGB颜色函数-Mix()函数

    Mix 函数是将两种颜色根据一定的比例混合在一起,生成另一种颜色.其使用语法如下: mix($color-1,$color-2,$weight); $color-1 和 $color-2 指的是你需要 ...

  4. mysql官网下载安装

    1.官网下载zip压缩文件 2.解压到指定文件夹 3.配置环境变量 4.新建my.ini 5.管理员权限打开cmd命令行,mysqld install mysql,mysql --initialize ...

  5. python tkinter的Label

    from tkinter import * window=Tk() window.title("my first window") window.geometry("50 ...

  6. SpringBoot---缓存支持Cache

    1.概述 1.1.在  Spring中使用缓存技术   的  关键   是配置CacheManager: SpringBoot为我们   自动配置了多个  CacheManager实现: Spring ...

  7. hdu 1695: GCD 【莫比乌斯反演】

    题目链接 这题求[1,n],[1,m]gcd为k的对数.而且没有顺序. 设F(n)为公约数为n的组数个数 f(n)为最大公约数为n的组数个数 然后在纸上手动验一下F(n)和f(n)的关系,直接套公式就 ...

  8. Sticks

    题目链接 题意:给你一组等长木棒,然后他随意砍断成n个木棒,木棒长度不一,但你知道分别是多少,要你求出原始木棒可能的最小长度. 思路:首先那个原始木棒的长度肯定是其总长度的约数,然后也肯定大于等于所有 ...

  9. 做一个简单的scrapy爬虫

    前言: 做一个简单的scrapy爬虫,带大家认识一下创建scrapy的大致流程.我们就抓取扇贝上的单词书,python的高频词汇. 步骤: 一,新建一个工程scrapy_shanbay 二,在工程中中 ...

  10. 五. jenkins部署springboot项目(2)--windows环境--服务

    前提:jenkins和springboot运行在同一台机器 springboot 通过winsw部署为服务 winsw 下载地址:https://github.com/kohsuke/winsw/re ...