poj3468 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, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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
Source
#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;
}
}
}
}
poj3468 A Simple Problem with Integers (树状数组做法)的更多相关文章
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- POJ3468 A Simple Problem with Interger [树状数组,差分]
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- A Simple Problem with Integers_树状数组
Problem Description Let A1, A2, ... , AN be N elements. You need to deal with two kinds of operation ...
- poj3468 A Simple Problem with Integers(线段树/树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 线段树---poj3468 A Simple Problem with Integers:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- 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 ...
- poj3468 A Simple Problem with Integers (线段树区间最大值)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 92127 ...
随机推荐
- 使用EntityFramework调用存储过程并获取存储过程返回的结果集
[实习]刚入职,公司要求完成两个任务,任务要求使用存储过程和事务,其中一个问题要获取存储过程的查询结果集.经过多方查找和自己的实践,终于找到了方法.这里记录一下. 看到的这篇文章中给出的例子是查询单个 ...
- 20個命令行工具監控 Linux 系統性能
對於每個系統管理員或網路管理員來說,每天要監控和調試 Linux 系統性能問題都是非常困難的工作.我已經有5年 Linux 管理員的工作經歷,知道如何監控系統使其保持正常運行.為此,我們編寫了對於 L ...
- raw, SOCK_RAW - Linux IPv4 raw socket.
总 览 #include <sys/socket.h> #include <netinet/in.h> raw_socket = socket(PF_INET, SOCK_RA ...
- css 当文字过多时以....省略
<!-- 公告 --> <p class="rst-promotion">公告: {{shopInfo.rst.promotion_info}}</p ...
- AGC001[BCDE] 题解
A没意思 F太难 所以大概近期的AGC题解都是BCDE的 然后特殊情况再说 开始刷AGC的原因就是计数太差 没有脑子 好几个学长都推荐的AGC所以就开始刷了 = = 大概两天三篇的速度?[可能也就最开 ...
- openlayers学习笔记(十三)— 异步调用JSON数据画点、文字标注与连线
使用Openlayers 3实现调用本地json数据在地图上添加点.文字标注以及连线. 生成底图地图 首先得有一个地图作为底图,代码如下: let vectorSource = new ol.sour ...
- centos 6.5 解压 tar.gz
只查看 tar.gz 文件内容而不解压 tar -tvf filename.tar.gz 解压为 tar 文件 gzip -d filename.tar.gz 解压为文件或目录 tar xvf fil ...
- cocos2D-X 线程注意事项
{ 在子线程种是无法创建纹理的,也就是精灵 }
- Sql server 2008 的完成备份和差异备份还原
当数据库数据量不大的情况下用 Sqlserver 的完全备份就完全可以了 步骤为: 1.在需要还原的数据库上右键选择如图 2.在“常规”选项中点击“源设备”选取磁盘上备份好的.bak文件后,勾上“还原 ...
- 【HDU5306】【DTOJ2481】Gorgeous Sequence【线段树】
题目大意:给你一个序列a,你有三个操作,0: x y t将a[x,y]和t取min:1:x y求a[x,y]的最大值:2:x y求a[x,y]的sum 题解:首先很明显就是线段树裸题,那么考虑如何维护 ...