HDU 1556 Color the ball(树状数组)
来源:互联网 发布:淘宝客推广使用教程 编辑:程序博客网 时间:2024/06/05 17:22
依旧是树状数组,只不过题意是区间修改,单点查询
网上的模板的写法感觉很不直观,按照自己的理解写了,和模板不太一样不过也A了
其实可以转化为区间查询单点修改。
一次[a,b,+1]的修改可以拆分为[1,b,+1]和[1,a-1,-1]两个修改,这样就转化为所有修改都是[1, i]这种了
令arr[i]表示对[1, i]的修改的累加值
则所求i涂色几次求的就是arr[i]+arr[i+1]+...+arr[n]([1, p](p < i)这种修改是影响不到arr[i]的)
即常规树状数组的query(n)-query(i-1)
问题就可以解决了
#include <bits/stdc++.h>using namespace std;const int maxn = 110000;int tree[maxn], n;int lowbit(int x){ return x & -x;}int query(int pos){ if (pos == 0) return 0; int ret = 0; while (pos) { ret += tree[pos]; pos -= lowbit(pos); } return ret;}void update(int pos, int delta){ if (pos == 0) return; while (pos <= n) { tree[pos] += delta; pos += lowbit(pos); }}int main(){ while (~scanf("%d", &n) && n) { memset(tree, 0, sizeof(tree)); for (int i = 1; i <= n; i++) { int fr, to; scanf("%d%d", &fr, &to); update(to, 1); update(fr-1, -1); } for (int i = 1; i <= n; i++) printf("%d%c", query(n) - query(i-1), (i == n) ? '\n' : ' '); } return 0;}
0 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【树状数组】
- 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(树状数组)
- 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 (树状数组简单应用)
- HDU 1556 Color the ball (简单树状数组)
- hdu 1556 Color the ball (扫描线+树状数组)
- hdu 1556 Color the ball(树状数组)
- scrapy 环境搭建与试运行
- Codeacademy Python-07-Student Becomes the Teacher
- Android开发:系统自动实现开机启动
- 回朔法——穷举n位二进制数
- 面试题之字符串反转
- HDU 1556 Color the ball(树状数组)
- Haskell笔记(5)
- 清除浮动的几个方法
- 五布局之帧布局FrameLayout
- 解决sublime3 中文乱码问题
- Linux系统编程-文件IO(一)
- 字符串反转 转换大小
- Android LocalBroadcastManager 的使用总结
- 响应式开发从原理到实战案例(十二):响应式改版完整案例-文章列表