hdu 1556.Color the ball 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556
题目意思:有 n 个气球从左到右排成一排,编号依次为1,2,3,...,n。给出 n 对 a, b,表示编号为 a ~b 的气球被涂过一次色。n 次之后,问所有气球被上色的次数分别是多少。
典型的树状数组题!这种是:每次修改一个区间,问的是最后每个点被修改的次数。要注意update() 函数对 a 处理 的时候,向上修改的过程中把不必要的区间都加了一遍。例如对a = 2时,update修改了c[2],c[4],c[8],那么还需要对 b+1 进行update,把不必要的被修改区间调整回来,即减去1。
树状数组还是很不熟悉,尤其对于get_sum() 函数。对样例
8
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
0
处理完之后得出以下这个图:
ans: 8 7 6 5 4 3 2 1
update 比较好理解,不再累赘。
get_sum(1) = c[1] = 8; get_sum(2) = c[2] = 7; get_sum(3) = c[3] + c[2] = -1 + 7 = 6
get_sum(4) = c[4] = 5; get_sum(5) = c[5] + c[4] = -1 + 5 =4; get_sum(6) = c[6] + c[4] = -2 + 5 = 3
get_sum(7) = c[7] + c[6] + c[4] = -1 -2 + 5 = 2; get_sum(8) = c[8] = 1
经过手动模拟,对这个求和函数了解了不少 (灵活运用还需要一段时间,继续努力吧[^_^])
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std; const int maxn = + ;
int n;
int c[maxn]; int lowbit(int x)
{
return x & (-x);
} void update(int x, int num)
{
while (x <= n) {
c[x] += num;
x += lowbit(x);
}
} int get_sum(int x)
{
int s = ;
while (x > ) {
s += c[x];
x -= lowbit(x);
}
return s;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE while (scanf("%d", &n) != EOF && n) {
memset(c, , sizeof(c));
int a, b;
for (int i = ; i < n; i++) {
scanf("%d%d", &a, &b);
update(a, );
update(b+, -);
}
for (int i = ; i <= n; i++) {
printf("%d%c", get_sum(i), (i == n ? '\n' : ' '));
}
}
return ;
}
hdu 1556.Color the ball 解题报告的更多相关文章
- hdu 1556:Color the ball(第二类树状数组 —— 区间更新,点求和)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556:Color the ball(线段树,区间更新,经典题)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU.1556 Color the ball (线段树 区间更新 单点查询)
HDU.1556 Color the ball (线段树 区间更新 单点查询) 题意分析 注意一下pushdown 和 pushup 模板类的题还真不能自己套啊,手写一遍才行 代码总览 #includ ...
- HDU 1556 Color the ball (数状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball【算法的优化】
/* 解题思路:每次仅仅求解一開始的第一个数字,让第一个数字加一,最后的一个数字的后面一个数减一.我们能够想想,最后加的时候,就是加上前面一个数出现的次数和自己本身出现的次数. 解题人:lingnic ...
- 线段树(求单结点) hdu 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- hdu 1556 Color the ball(区间更新,单点求值)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 1556 Color the ball(线段树区间更新)
Color the ball 我真的该认真的复习一下以前没懂的知识了,今天看了一下线段树,以前只会用模板,现在看懂了之后,发现还有这么多巧妙的地方,好厉害啊 所以就应该尽量搞懂 弄明白每个知识点 [题 ...
- hdu 1556 Color the ball (线段树+代码详解)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...
随机推荐
- CFgym Board Queries (旋转、翻转简化)
http://codeforces.com/gym/100497 codeforces 2014-2015 CT S02E04: Codeforces Trainings Season 2 Episo ...
- GetDlgItem
GetDlgItem是父窗口用来获取它的子窗口的句柄的. 如果是在一个对话框里想要获取另一个对话框中元素的句柄,假设B对话框的指针为pWnd,则pCtrl = pWnd->GetDlgItem( ...
- Nginx-tomcat-redis------负载均衡以及session共享
测试环境 Nginx 1.10.1 tomcat 7.0.70 Redis-x64-3.2.100 说明 tomcat 8 和 redis 实现session共享 有问题. 寻找源码 发现tomcat ...
- socket编程报异常java.io.EOFException
一个客户端连接服务器的小程序,服务器端可以正常读取客户端发来的数据 但是当客户端关闭时,服务端也关闭了,并且抛出如下的异常: java.io.EOFException at java.io.DataI ...
- Mastering Web Application Development with AngularJS 读书笔记(一)
第一章笔记 (一) 一.PS:运行时配置IIS <html> <head> <script src="angular.js"></scri ...
- [译]AngularJS Service vs Factory - Once and for all
原文: http://blog.thoughtram.io/angular/2015/07/07/service-vs-factory-once-and-for-all.html Service和Fa ...
- R语言练习(一)
b = seq(from=0, to=1, by=0.001) #一次方 l1 = function(b){ b^1 } y1 = l1(b) #二次方 l2 = function(b){ b^2 } ...
- ICloud没有密码怎么注销?
忘了密码的用这方法就可以啦1.现在我说你做 . 先进入 iCloud 点击 某某账户(要删的账户)2.第二栏密码那里删除原来的再输入ios(任意密码)3.然后点完成 之后会出现错误.请点 好.然后左上 ...
- VS2013 Community配置OpenCV3.0.0
配置环境:32位win7系统+VS2013 Community版本 1.首先从OpenCV官网上下载最新版本的OpenCV for Windows. 2.直接双击打开下载得到的opencv-3.0.0 ...
- centos 安装mysql
wget http://dev.mysql.com/get/mysql-community-release-el7-5.noarch.rpm rpm -ivh mysql-community-rele ...