POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧。一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用树状数组做的题,只需多维护一个bit即可。具体的思路见下面的链接:
http://hi.baidu.com/billdu/item/053f6a15ca301b0a8ebde400
要理解里面的橙色块求的时候是打竖看的,不是打横看的。
#pragma warning(disable:4996)
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<algorithm>
#include<cmath>
#include<string>
#define ll long long
#define maxn 100000
#define lowbit(k) k&(-k)
using namespace std; ll bit[2][maxn + 50];
int n,q; void inc(ll bit[],int i, int m)
{
for (; i <= n; i += lowbit(i)) bit[i] += m;
} ll query(ll bit[],int i)
{
ll sum = 0;
for (; i > 0; i -= lowbit(i)){
sum += bit[i];
}
return sum;
} ll sum[maxn + 50]; int main()
{
while (cin >> n >> q)
{
memset(bit, 0, sizeof(bit));
for (int i = 1; i <= n; i++){
scanf("%lld", sum + i);
}
sum[0] = 0;
for (int i = 1; i <= n; i++) sum[i] += sum[i - 1];
char str[3];
int a,b,c;
for (int i = 0; i < q; i++){
scanf("%s", str);
if (str[0] == 'Q'){
scanf("%d%d", &a, &b);
ll ans = (query(bit[0], b)*(b + 1) - query(bit[1], b)) - (query(bit[0], a - 1)*a - query(bit[1], a - 1));
ans += sum[b] - sum[a - 1];
printf("%lld\n", ans);
}
else{
scanf("%d%d%d", &a, &b, &c);
inc(bit[0], a, c);
inc(bit[1], a, c*a);
inc(bit[0], b + 1, -c);
inc(bit[1], b + 1, -c*(b + 1));
}
}
}
return 0;
}
POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问的更多相关文章
- 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(树状数组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 ...
- 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:成段增减:区间求和
poj3468 A Simple Problem with Integers 题意:O(-1) 思路:O(-1) 线段树功能:update:成段增减 query:区间求和 Sample Input 1 ...
- hdu 2642二维树状数组 单点更新区间查询 模板题
二维树状数组 单点更新区间查询 模板 从零开始借鉴http://www.2cto.com/kf/201307/227488.html #include<stdio.h> #include& ...
- poj3468 A Simple Problem with Integers (树状数组做法)
题目传送门 A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 1 ...
- poj3468 A Simple Problem with Integers(线段树/树状数组)
Description You have N integers, A1, A2, ... , AN. You need to deal with two kinds of operations. On ...
- 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 ...
随机推荐
- Vs2010搭建directshow 环境
一:材料 1, vs2010 2, winsdk7.1 http://www.microsoft.com/en-us/download/details.aspx?id=8442 更具自己电脑选择32 ...
- ant条件逻辑
<condition property="sdk-folder" value="E:\android\android-sdk\adt-bundle-windows- ...
- Visual Studio 2012 使用免费的Team Foundation Service
VS2012提供了在线的TFS服务,免费支持五人小团队,收费情况尚未确定,下面本文演示如何申请和连接在线TFS 服务器. 一.申请TFS服务 首先,打开VS2012,看看是否有团队资源管理器,如果没有 ...
- js----对象的创建
js创建对象的三种方法 在介绍之前一定要弄清楚一个概念,比如var a = new Object(); 这里的a并不是一个对象,而是一个对象的实例. 一.用Json创造 var a = {b:1,c: ...
- 12个git实战建议和技巧
摘要:git无疑是现在最热门的版本控制工具,而且正在进一步侵占SVN以及CVS的市场.本文作者从国外技术问答社区Stack Overflow整理的12个很实用的git使用技巧和建议,希望对你有帮助. ...
- c# 刻度:毫米 英寸 像素转换
从目前所掌握的资料来看,c#程序中将毫米转换像素的方法无非两种: 第一种: 1: /// <summary> 2: /// 以毫米为单位的显示宽度 3: /// </summary& ...
- openerp学习笔记 错误、警告、提示、确认信息显示
1.检查业务逻辑中的错误,终止代码执行,显示错误或警告信息: raise osv.except_osv(_('Error!'), _('Error Message.')) 示例代码: #删除当前销售单 ...
- 浅谈 WPF控件
首先我们必须知道在WPF中,控件通常被描述为和用户交互的元素,也就是能够接收焦点并响应键盘.鼠标输入的元素.我们可以把控件想象成一个容器,容器里装的东西就是它的内容.控件的内容可以是数据,也可以是控件 ...
- Microsoft Azure 全球状态
除了mooncake的中国大陆区域 Azure status http://azure.microsoft.com/en-us/status/?rnd=1#current http://azure.m ...
- MySQL批量杀进程
OS: CentOS 6.3 DB:5.5.14 mysql> show processlist;+----+------+-----------+--------------------+-- ...