HDU - 1556 树状数组(区间修改+单点更新)
2017-02-06 11:12
253 查看
题意:
n个数,初始化为0,每次给一段区间的数+1,最后输出n个数。思路:
树状数组区间修改+单点查询的入门题,每次更新区间[l,r]的时候,给区间左端点l处+1,右端点后一个r+1处减1,这样,最后求i位置的值,只要从左往右求一遍前缀和,小于l的位置都没有增量,l到r之间会有增量1,r之后+1,-1相互抵消也没有增量。代码:
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 10; int n; int bit[MAXN]; int lowbit(int x) { return x & -x; } void add(int x, int y) { while (x <= n) { bit[x] += y; x += lowbit(x); } } int sum(int x) { int res = 0; while (x) { res += bit[x]; x -= lowbit(x); } return res; } int main() { while (scanf("%d", &n), n) { for (int i = 1; i <= n; i++) bit[i] = 0; for (int i = 1; i <= n; i++) { int l, r; scanf("%d%d", &l, &r); add(l, 1); add(r + 1, -1); } for (int i = 1; i <= n; i++) printf("%d%c", sum(i), i == n ? '\n' : ' '); } return 0; }
相关文章推荐
- hdu 1556 Color the ball 【树状数组:区间修改、单点求值】
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)
- HDu 1556 Color the ball【线段树&&树状数组】区间更新,单点查询
- HDU 1556 Color the ball (一维树状数组,区间更新,单点查询)
- POJ 2352 Stars + HDU 1556 Color the ball(树状数组单点更新及伪区间更新)
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- HDU 1556 Color the ball(树状数组,区间修改,单点统计)
- HDU 1556 Color the ball 树状数组-区间更新-模板
- 树状数组的区间更新 HDU 1556 示例
- HDU 1556 Color the ball 很典型的更新区间查找点的题(线段树树状数组两种解法)
- hdu 4533(树状数组区间更新+单点查询)
- 【树状数组 单点修改,区间求值】hdu 1166 敌兵布阵
- 树状数组单点更新和区间更新,二维数组poj2155(区间更新,单点查询)(已加入区间修改区间查询)
- [HDU 4031]Attack[树状数组区间更新单点查询]
- HDU 1556(树状数组 区域更新,单点求值)
- HDU 1166 敌兵布阵 (树状数组--单点更新,区间求值)
- hdu 2642 二维树状数组 单点更新区间查询 模板水题
- POJ - 2155 Matrix (二维树状数组 + 区间修改 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)