POJ 3468_A Simple Problem with Integers(树状数组)
完全不知道该怎么用,看书稍微懂了点。
题意:
给定序列及操作,求区间和。
分析:
树状数组可以高效的求出连续一段元素之和或更新单个元素的值。但是无法高效的给某一个区间的所有元素同时加个值。
不能直接用树状数组求,就处理一下。用两个树状数组维护两个数据,一个维护前i项和,一个维护增加的值。设:
那么[l,r]区间上同时加上x就可以表示为:
- 对于bit0来说,在l位置上加上−x∗(l−1),在r+1位置上加上x∗r
- 对于bit1来说,在l位置上加上x,在r+1位置上加上−x
代码:
#include<cstdio>
const int maxn = 300005;
typedef long long ll;
//[l,r]
ll bit[2][maxn];
int v[maxn];
int n, q;
ll sum(int i, int t)
{
ll tot = 0;
while(i>0){
tot += bit[t][i];
i -= i&-i;
}
return tot;
}
void update(int i, int x, int t)
{
while(i <= n){
bit[t][i] += x;
i += i&-i;
}
}
int main (void)
{
scanf("%d%d",&n,&q);
int a, b, c;
for(int i = 1; i <= n; i++){
scanf("%d",&v[i]);
update(i, v[i], 0);
}
for(int i = 0; i < q; i++){
getchar();
if(getchar()=='C'){
scanf("%d%d%d", &a, &b, &c);
update(a, - c * (a - 1), 0);
update(b + 1, c * b, 0);//bit0
update(a, c, 1);
update(b + 1, - c,1); //bit1
}else{
scanf("%d%d",&a, &b);
ll res = 0;
res += sum(b, 0) + sum(b ,1) * b;
res -=sum(a-1, 0) +sum(a-1, 1) * (a-1);
printf("%I64d\n",res);
}
}
}
如果操作得到的结果可以用i的n次多项式表示,那么就可以用n+1个树状数组维护了。
1700ms比线段树快了700ms~
POJ 3468_A Simple Problem with Integers(树状数组)的更多相关文章
- A Simple Problem with Integers(树状数组HDU4267)
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- HDU 4267 A Simple Problem with Integers --树状数组
题意:给一个序列,操作1:给区间[a,b]中(i-a)%k==0的位置 i 的值都加上val 操作2:查询 i 位置的值 解法:树状数组记录更新值. 由 (i-a)%k == 0 得知 i%k == ...
- POJ3468 A Simple Problem With Integers 树状数组 区间更新区间询问
今天学了很多关于树状数组的技巧.一个是利用树状数组可以简单的实现段更新,点询问(二维的段更新点询问也可以),每次修改只需要修改2个角或者4个角就可以了,另外一个技巧就是这题,原本用线段树做,现在可以用 ...
- 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 ...
- POJ 3468_A Simple Problem with Integers(线段树)
题意: 给定序列及操作,求区间和. 分析: 线段树,每个节点维护两个数据: 该区间每个元素所加的值 该区间元素和 可以分为"路过"该区间和"完全覆盖"该区间考虑 ...
- Poj 3468-A Simple Problem with Integers 线段树,树状数组
题目:http://poj.org/problem?id=3468 A Simple Problem with Integers Time Limit: 5000MS Memory Limit ...
- POJ A Simple Problem with Integers 线段树 lazy-target 区间跟新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 105742 ...
- POJ 3468A Simple Problem with Integers(线段树区间更新)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 112228 ...
随机推荐
- 关于OPPO手机的生存和程序员的发展
关于程序员私下讨论最多的话题,除了哪个编程最牛逼之外,哪款品牌的手机最牛逼也是我们谈论最多的话题之一吧!有的喜欢罗永浩,自然就是锤粉:有的喜欢苹果,称它为工业时代最优美的艺术品:当然,我想也有很多的人 ...
- leetcode764 Largest Plus Sign
思路: 首先使用dp计算出在每个位置(i, j)上下左右最多有多少个连续的1,得到up[i][j], down[i][j], left[i][j], right[i][j].然后计算这四个值中的最小值 ...
- struts 2.5 There is no Action mapped for namespace [/] and action name [user_find] associated with context path [/struts2_crm].
遇到了这个错误. There is no Action mapped for namespace [/] and action name [user_find] associated with con ...
- R in action读书笔记(5)-第七章:基本统计分析
7.1描述性统计分析 > vars<-c("mpg","hp","wt") > head(mtcars[vars]) ...
- 关于Farseer.net轻量级ORM开源框架 V1.0 概念版本开发的消息
V0.2版的开源距离今天(05年03月)已有近3年的时间.可以说这个版本已经有点落伍的感觉了,呵呵. V0.2版至今一直处于BUG的修复及一些细小功能的增加,所以版本号上一直没有变化. 其实在这1.2 ...
- OpenGL Column-Major Matrix 使用注意事项
这column major的矩阵是彻底把我搞晕了,以后右乘规则下的矩阵应该这么用 假设我想创建一个2x2的矩阵,数学上我这么写: 1 2 3 4 用代码创建的话这么写 // 按照 row major ...
- 核武器代理CC工具V3.42最新版本!
软件说明 !!!有新版本更新,请移步到更新地址:https://www.cnblogs.com/cnhacker/p/10878688.html ########################### ...
- 循环冗余校验(CRC)算法入门
http://blog.csdn.net/liyuanbhu/article/details/7882789 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌入式软件开发中,经常要 ...
- 单文件组件.vue---父子组件通信
每一个.vue 文件就是一个 组件,组件和组件相互组合,就成了一个应用,这就涉及到的组件和组件之间的通信,最常用的就是父子之间的通信.在vue 中, 在一个组件中通过 import 引入另一个组件,这 ...
- 10C++类和对象
类和对象 8.1 面向对象程序设计方法概述 到目前为止,我们介绍的是C++在面向过程的程序设计中的应用.对于规模比较小的程序,编程者可以直接编写出一个面向过程的程序,详细地描述每一瞬时的数据结构及对其 ...