[POJ 2368 A Simple Problem with Integers] 树状数组区间修改、区间查询
来源:互联网 发布:淘宝哪些站外网站 编辑:程序博客网 时间:2024/05/23 23:41
[POJ 2368 A Simple Problem with Integers] 树状数组区间修改、区间查询
知识点:data structure
binary index tree
1. 题目链接
[POJ 2368 A Simple Problem with Integers]
2. 题意描述
对数组
3. 解题思路
这个用线段树做也就是一个无脑题。之前这类区间查询的还真的很少用树状数组做。
原理:原数组为
所以,
通过维护差分数组
4. 实现代码
#include <bits/stdc++.h>using namespace std;typedef long long LL;const int MAXN = 100000 + 5;int n, q;template<class T>struct BIT { T C[MAXN]; void I() { memset(C, 0, sizeof(C)); } inline T lowbit(T x) { return x & (-x); } void update(int x, LL val) { while(x < MAXN) { C[x] += val; x += lowbit(x); } } T query(int x) { T ret = 0; while(x > 0) { ret += C[x]; x -= lowbit(x); } return ret; }};BIT<LL> a, b;void update(int x, LL v) { a.update(x, v); b.update(x, v * x); }void update(int l, int r, LL v) { update(l, v); update(r + 1, -v); }LL presum(int x) { return (LL)(x + 1) * a.query(x) - b.query(x); }LL query(int l, int r) { return presum(r) - presum(l - 1); }int main() {#ifdef ___LOCAL_WONZY___ freopen("input.txt", "r", stdin);#endif // ___LOCAL_WONZY___ char op[5]; LL l, r, v; while(~scanf("%d %d", &n, &q)) { a.I(); b.I(); vector<LL> A(n + 1); A[0] = 0; for(int i = 1; i <= n; i++) { scanf("%lld", &A[i]); update(i, A[i] - A[i - 1]); /// 注意这里 } while(q --) { scanf("%s %lld %lld", op, &l, &r); if(op[0] == 'Q') { printf("%lld\n", query(l, r)); } else { scanf("%lld", &v); update(l, r, v); } } } return 0;}
1 0
- [POJ 2368 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(树状数组区间修改)
- A Simple Problem with Integers(树状数组,更新区间查询区间模板)
- poj 3468 A Simple Problem with Integers(线段树区间更新 or 树状数组区间更新)
- 20140719 「树状数组 - 区间更新,区间求和」 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 (线段树 入门题 区间修改 区间查询)
- HDU 4267 - A Simple Problem with Integers 树状数组区间修改
- hdu4267 A Simple Problem with Integers(树状数组区间更新点查询)
- [POJ 3468]A Simple Problem with Integers[树状数组区间更新+求和]
- 机器学习系列-Generative Learning Algorithms
- http链接的问题
- 每日一结,Servlet
- PAT(A-LEVEL) 1001 A+B Format
- J2ME的网络通信和J2SE有区别吗?
- [POJ 2368 A Simple Problem with Integers] 树状数组区间修改、区间查询
- 怎么得到时间,并且存入到String变量中
- 图片浏览,内从不足的问题!急!
- Nginx 服务器隐藏 index.php 配置
- j2me入门问题,关于HelloMIDlet问题
- flume的概念和基本使用
- 手机上运行酷欧天气,显示加载失败?
- 懒虫小鑫
- python win系统下 打开TXT文件