HDU1556 color the ball(树状数组)向下查询,向上统计
来源:互联网 发布:网络审计设备 编辑:程序博客网 时间:2024/06/10 16:29
解题思路:
其实这道题可以把每次染色的点抽象为每次涂改的区间,然后对要查询的点所在区间的更新次数进行求和
这样就可以在时间上,大大缩短,查询和统计的时间复杂度都为log(n)
树状数组中的每个节点都代表了一段线段区间,每次更新的时候,根据树状数组的特性可以把b以前包含的所有区间都找出来,然后把b以前的区间全部加一次染色次数。然后,再把a以前的区间全部减一次染色次数,这样就修改了树状数组中的[a,b]的区间染色次数,查询每一个点总的染色次数的时候,就可以直接向上统计每个父节点的值,就是包含这个点的所有区间被染色次数,这就是树状数组中向下查询,向上统计的典型应用
Ps:根据个人理解层次的不同,这道题也可以向上查询,向下统计,还可以向下查询,向下统计。
比如这样一组数据:
1
8 8
想想第四个气球是多少次的过程,C[8]被改成了1, C[7]被改成了-1,c[6]被改成了-1, c[4]被改成了-1,那么在查询c[4]的时候会上升(加上)到c[8],就是上面说的包含这个点的所有区间被染色次数。
#include<iostream>#include<cstdio>#include<cstring>using namespace std;const int maxn = 100005;int C[maxn];void Update(int i, int value){ while(i != 0) { C[i] += value; i -= i & (-i); }}int getsum(int i){ int s = 0; while(i < maxn) { s += C[i]; i += i & (-i); } return s;}int main(){ int n; while(cin >> n && n) { memset(C, 0, sizeof(C)); for(int i = 1; i <= n; i++) { int l, r; scanf("%d%d", &l, &r); Update(r, 1); Update(l-1, -1); } for(int i = 1; i < n; i++) cout << getsum(i) << " "; cout << getsum(n) << endl; } return 0;}
0 0
- HDU1556 color the ball(树状数组)向下查询,向上统计
- 树状数组:HDU1556 Color the ball 【区间更新,单点查询]
- hdu1556 Color the ball (树状数组)
- HDU1556 Color the ball (树状数组)
- [树状数组]HDU1556 Color the ball
- hdu1556 Color the ball(树状数组)
- Hdu1556 Color the ball [分块][树状数组]
- [HDU1556] Color the ball - 树状数组
- HDU1556 Color the ball (树状数组)
- 【HDU1556】Color the ball(树状数组)
- hdu1556 Color the ball 树状数组
- 【HDU1556-color the ball】(树状数组)
- HDU1556 color the ball (树状数组)
- (树状数组)hdu1556 Color the ball(区间更新,单点查询)
- HDU1556 color the ball【树状数组】解题报告
- HDU1556 Color the ball【树状数组】【区间更新】
- hdu1556 Color the ball(树状数组 修改区间,查找值)
- HDU1556 Color the ball(树状数组+抖机灵)
- 【转】gcc动态链接库*.so文件的生成与使用方法
- 第八周项目2-建立链串的算法库
- vmware fusion 8.0上成功安装雨林木风的windows10 企业版 64位gost版
- (OK) Building Ice Cream Sandwich (Android v4) for VirtualBox on EC2
- Error1error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
- HDU1556 color the ball(树状数组)向下查询,向上统计
- 计算机算法之穷举法
- 《TCP/IP详解:卷一》第1章:概述
- LinearLayout和Relativelayout的右对齐问题
- Sencha Touch 2 官方文档翻译之 Sencha Touch 2应用程序简介
- 《初入linux》--第九部分-linux的日志管理
- linux串口收发程序
- 用Spring MVC搭建REST风格的服务
- Windows核心编程 第十二章 纤程