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 ...
随机推荐
- OpenJudge 666:放苹果
总时间限制: 1000ms 内存限制: 65536kB 描述 把M个同样的苹果放在N个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分法?(用K表示)5,1,1和1,5,1 是同一种分法. 输 ...
- linux下别名alias的设置
我有一个常用目录/volumes/mac/www’,每次都要输入这么长的路径,麻烦,所以有了以下配置 1.vi ~/.bash_profile 2.按住shift + i进入编辑状态 3.插入 ali ...
- JavaScript ==和===
== : 值等 === :恒等(引用等) ref: http://blog.csdn.net/wang171838/article/details/8554305 JavaScript支持“=”.“ ...
- Spring-Mybatis 异常记录(1)
Spring applicationconfig.xml如下 <?xml version="1.0" encoding="UTF-8"?> < ...
- Win7下MongoDB安装
一.下载MongoDB 下载地址:http://www.mongodb.org/downloads 注意:1.从2.2开始,MongoDB不再支持windows xp. 2.32位MongoDB最大支 ...
- PHP弱类型安全问题笔记
一.类型转换问题 intval(); var_dump(intval('1asdfasd')); //1 var_dump(intval('awqw12')); //0 var_dump(i ...
- 一款jQuery仿海尔官网全屏焦点图特效代码
一款jQuery仿海尔官网全屏焦点图特效代码,带有左右箭头的jQuery焦点图切换特效. 当焦点图切换时,下方的三块小图也相对应的进行切换.并且特效还兼容头疼的IE6.赶快去和谐了它吧! 适用浏览器: ...
- 每日一“酷”之bisect
作用:维护有序列表,而不必在每次想列表增加一个元素时调用sort排序 bisect 模块实现了一个算法用于向列表中插入元素,同时仍保持列表有序.有些情况下,这比反复对一个了表序列更高效,另外也比构建一 ...
- JSON对象配合jquery.tmpl.min.js插件,手动攒出一个table
jquery.tmpl.min.js 首先下载这个插件 1.绑定json那头的键 //TemplateDDMX 这个是这段JS的ID,这个必须写!!!!!! //${}为json的键的值,必须要填写正 ...
- 【转载】Powershell设置世纪互联Office365嵌套组发送权限
Start-Transcript ".\Set-GroupSendPermisionLog.txt" -Force function Get-DLMemberRecurse { $ ...