POJ 3468_A Simple Problem with Integers(树状数组)
来源:互联网 发布:黑龙江省人工智能学会 编辑:程序博客网 时间:2024/05/24 22:45
完全不知道该怎么用,看书稍微懂了点。
题意:
给定序列及操作,求区间和。
分析:
树状数组可以高效的求出连续一段元素之和或更新单个元素的值。但是无法高效的给某一个区间的所有元素同时加个值。
不能直接用树状数组求,就处理一下。用两个树状数组维护两个数据,一个维护前
- 对于
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~
0 0
- POJ 3468_A Simple Problem with Integers(树状数组)
- POJ 3468_A Simple Problem with Integers(线段树)
- POJ 3468 A Simple Problem with Integers (树状数组)
- POJ 3468 A Simple Problem with Integers(树状数组)
- POJ 3468 A Simple Problem with Integers (树状数组写法)
- POJ 3468 A Simple Problem with Integers 【树状数组】
- [POJ 3468] A Simple Problem with Integers [树状数组]
- POJ 3468 A Simple Problem with Integers 树状数组解法
- POJ 3468 A Simple Problem with Integers 树状数组
- poj 3468A Simple Problem with Integers(树状数组区间修改)
- Poj 3468 A Simple Problem with Integers(树状数组&&线段树)
- POJ 3468 A Simple Problem with Integers 树状数组(区间更新,区间查询)
- poj-3468-A Simple Problem with Integers(树状数组更新区间查区间)
- poj-3468 A Simple Problem with Integers(线段树,树状数组区间求和)
- POJ 3468 A Simple Problem with Integers (树状数组解法 树状数组区间更新 区间查询)
- poj 3468 A Simple Problem with Integers(线段树#6 / 树状数组)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- POJ 3468 A Simple Problem with Integers 树状数组 区间修改 区间查询
- Ubuntu14.04 下apt-get安装Code::Blocks
- POJ 2239 Selecting Courses(二分图最大匹配)
- 多人聊天室
- 算法学社 -- "这是一道难题"第一期
- 《VMWARE核心技术网卡三种模式》
- POJ 3468_A Simple Problem with Integers(树状数组)
- Android屏幕适配全攻略
- 2016蓝桥杯算法训练——出现次数最多的整数
- bzoj1831【AHOI2008】逆序对
- 安卓总结 Activity生命周期
- 用Ant实现Java项目的自动构建和部署
- 在jdk1.7安装目录下jre\lib找不到tools.jar的解决办法
- 我眼中的艺术
- 管道的使用---参见Windows核心编程