(树状数组)hdu1556 Color the ball(区间更新,单点查询)
来源:互联网 发布:上海招聘seo引擎优化 编辑:程序博客网 时间:2024/06/07 01:35
树状数组最基本的应用是单点更新,区间和查询,但本题略有不同,本题是区间更新,单点查询。我建立树状数组时用的是向上更新,向下求和,因此,只需在输入时将a到n这一区间都+1,再将b+1到n这一区间都-1即可完成数组的维护。最后输出每点对应的sum值即为被染色的次数。具体代码如下:
#include<bits/stdc++.h>typedef long long ll;const int maxn = 100010;int n;int a[maxn];int lowbit(int k) { return k & (-k);}void update1(int pos, int num) {//单点更新 while(pos <= n) { a[pos] += num;//更新 pos += lowbit(pos);//向上更新受到影响的祖先结点 } return;}int sum(int e) {//求一段区间的和 int sum = 0; while(e > 0) { sum += a[e]; e -= lowbit(e); } return sum;}int query(int a, int b) {//查询区间和 return sum(b) - sum(a-1);}int main() { while(scanf("%d", &n) != EOF && n) { int x, y; memset(a, 0, sizeof(a)); for(int i = 1;i <= n;i++) { scanf("%d %d", &x, &y); update1(x, 1); update1(y+1, -1); } for(int i = 1;i < n;i++) printf("%d ", sum(i)); printf("%d\n", sum(n)); /*for(int i = 1;i <= n;i++) printf("%d\n", sum(i)); for(int i = 1;i <= cas;i++) { char ch; scanf("\n%c", &ch); int a, b, c; if(ch == 'q') { scanf("%d %d", &a, &b); printf("%d\n", query(a, b)); } else if(ch == 'c') { scanf("%d %d %d", &a, &b, &c); update1(a, c); update1(b+1, -c); } }*/ } return 0;}
阅读全文
0 0
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- hdu1556 color the ball 树状数组区间更新单点查询(附线段树做法)与二维扩展
- HDU1556 Color the ball 树状数组(区间更新单点求值)
- HDU1556 Color the ball(树状数组BIT 区间修改单点询问)
- HDU1556:Color the ball(线段树区间更新单点求值)&&树状数组解法
- HDU1556——Color the ball(区间更新OR树状数组)
- HDU1556 Color the ball【树状数组】【区间更新】
- 【树状数组-区间更新】hdu1556 Color the ball
- hdu1556 Color the ball(树状数组)
- 【HDU1556】Color the ball(树状数组)
- 【HDU1556-color the ball】(树状数组)
- HDU1556 color the ball (树状数组)
- Hdu oj 1556 Color the ball(树状数组区间更新转单点更新)
- [HDU]1556 Color the ball (树状数组--区间更新,单点求值)
- Color the ball ---树状数组(区间更新及单点求值)
- HDU 1556-Color the ball(树状数组-区间修改 单点查询)
- HDU1556 Color the ball 树状数组 修改区间值 查找单点值
- Android Audio System
- vs2017打包安卓项目
- Java垃圾回收机制--java GC
- C++基于控制台的迷宫实现(下)
- python测试代码报错:Ran 0 test in 0.00s
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- UrhoSharp Android入门
- 【关于适配】PopupWindow弹出时背景不变暗 或者 PopupWindow背景一闪一闪问题处理
- 集合之List
- git rebase
- python入门(一):变量及存储
- 一个集合选奇数个和偶数个情况研究
- pycharm 模板添加作者时间信息
- 【JZOJ5244】【NOIP2017模拟8.8A组】Daydreamin