Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >HDUOJ -----Color the ball

HDUOJ -----Color the ball

作者头像
Gxjun
发布于 2018-03-22 05:06:30
发布于 2018-03-22 05:06:30
83300
代码可运行
举报
文章被收录于专栏:mlml
运行总次数:0
代码可运行

Color the ball

Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7497    Accepted Submission(s): 3865

Problem Description

N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?

Input

每个测试实例第一行为一个整数N,(N <= 100000).接下来的N行,每行包括2个整数a b(1 <= a <= b <= N)。 当N = 0,输入结束。

Output

每个测试实例输出一行,包括N个整数,第I个数代表第I个气球总共被涂色的次数。

Sample Input

3 1 1 2 2 3 3 3 1 1 1 2 1 3 0

Sample Output

1 1 1 3 2 1

Author

8600

Source

HDU 2006-12 Programming Contest

简单的树状数组。

依旧树状数组的原理,如果直接采用ope(a,b),显然会超时,而且若是在树状数组中划定界限来统计,图画的个数,这样这样明显是统计不到的,

我们知道,树状数组是一个向上修改的的过程,然后向下统计求sum , 若果我们要夹出这个数值,那么我们必须要进行两次的修改,来最终确定有多少

代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 #define maxn  100000
 5 int aa[maxn+5];
 6 int nn;
 7 int lowbit(int x)
 8 {
 9   return x&(-x);
10 }
11 void ope(int x,int val)
12 {
13     while(x<=nn)
14     {
15         aa[x]+=val;
16         x+=lowbit(x);
17     }
18 }
19 int sum(int x)
20 {
21     int ans=0;
22     while(x>0)
23     {
24         ans+=aa[x];
25         x-=lowbit(x);
26     }
27     return ans;
28 }
29 int main()
30 {
31   int i,a,b;
32   while(scanf("%d",&nn),nn)
33   {
34       memset(aa,0,sizeof(aa));
35       for(i=0;i<nn;i++)
36       {
37           scanf("%d%d",&a,&b);
38           ope(a,1);
39           ope(b+1,-1);
40       }
41         printf("%d",sum(1));
42       for(i=2;i<=nn;i++)
43         printf(" %d",sum(i));
44         putchar(10);
45   }
46     return 0;
47 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-04-16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
HDUOJ-----2852 KiKi's K-Number(树状数组+二分)
KiKi's K-Number Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2393    Accepted Submission(s): 1101 Problem Description For the k-th number, we all should be very familiar with it. Of course,to k
Gxjun
2018/03/22
6130
HDUOJ-----1166敌兵布阵
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 36506    Accepted Submission(s): 15432 Problem Description C国的死对头A国这段时间正在进行军事演习,所以C国间谍头子Derek和他手下Tidy又开始忙乎了。A国在海岸线沿直线布置了N个工兵营地,Derek和Tidy的任务就是要
Gxjun
2018/03/22
5330
HDUOJ-----1541 Stars
Stars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3680    Accepted Submission(s): 1449 Problem Description Astronomers often examine star maps where stars are represented by points on a plane
Gxjun
2018/03/21
6700
HDUOJ-----1541   Stars
hdoj 1166 敌兵布阵
暴力超时,这道题可以用线段树做,因为更新的是单个节点,我们也可以用数组数组来做,我将两种方法的代码都给出
xindoo
2021/01/22
2760
HDUOJ-----1556Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 6787    Accepted Submission(s): 3549 Problem Description N 个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始
Gxjun
2018/03/22
5740
HDUOJ---------Kia's Calculation
Kia's Calculation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 624    Accepted Submission(s): 178 Problem Description Doctor Ghee is teaching Kia how to calculate the sum of two integers. But Kia
Gxjun
2018/03/21
7100
POJ 1195 Mobile phones (二维树状树组)
由于英语极差,看了半天也没看懂题目,最后参考了其他人的题解才搞懂题目,我就直接把题意贴过来了
xindoo
2021/01/22
3390
差分标记-HDU1556 Color the ball
什么是差分标记? 差分标记,是一种和前缀和相对的离线算法。 所谓差分就是将数组a每一项与前一项做差,记作差分数组b,易得对数组b做一遍前缀和就得到了原来的a数组。 因为是离线算法所以修改操作一定要在查询操作之前,它可以维护多次对序列的一个区间加减上一个数,最后询问某一位的数。
唔仄lo咚锵
2020/09/15
5080
HDU 1556 Color the ball
Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 18404    Accepted Submission(s): 9166 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到
Angel_Kitty
2018/04/08
7210
HDUOJ---3743Frosh Week(BIT+离散化)
Frosh Week Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1545    Accepted Submission(s): 497 Problem Description During Frosh Week, students play various fun games to get to know each other and
Gxjun
2018/03/22
7400
HDUOJ-----2838Cow Sorting(组合树状数组)
Cow Sorting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2163    Accepted Submission(s): 671 Problem Description Sherlock's N (1 ≤ N ≤ 100,000) cows are lined up to be milked in the evening. Ea
Gxjun
2018/03/22
6840
HDUOJ---2642Stars(二维树状数组)
Stars Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/65536 K (Java/Others) Total Submission(s): 975    Accepted Submission(s): 420 Problem Description Yifenfei is a romantic guy and he likes to count the stars in the sky. To make the prob
Gxjun
2018/03/22
4800
HDUOJ---2642Stars(二维树状数组)
HDU 1556-差分数组和线段树的对比分析-Color the ball
差分数组 数据结构详解戳这里! 线段树 数据结构详解戳这里! 这两个数据结构的操作主要有两个:更新和查询。 假设数据结构总长度为n。 差分数组: 更新时间复杂度 O(1) 查询时间复杂度 O(n) 线段树 : 更新时间复杂度 O(logn) 查询时间复杂度 O(logn) 因此,差分数组适用于多次更新,常量次查询,数据范围在1e7以内的情况;线段树适用于多次更新,多次查询,数据范围在1e5以内的情况。 下面例题的要求比较低,两种数据结构都可以用。如果改动一下要求: 1、数据范围不是1e5而是1e7,只能用差分数组。 2、不是一次查询而是多次查询,只能用线段树。
全栈程序员站长
2022/08/23
2500
HDOJ/HDU 1556 Color the ball(树状数组)
Problem Description N个气球排成一排,从左到右依次编号为1,2,3….N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽”牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
谙忆
2021/01/21
4280
HDU 4247 Pinball Game 3D(cdq 分治+树状数组+动态规划)
Pinball Game 3D Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1137    Accepted Submission(s): 477 Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pi
ShenduCC
2018/04/27
6350
Color the ball(前缀和)- HDU 1556
N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色。但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气球被涂过几次颜色吗?
ACM算法日常
2018/08/07
5840
Color the ball(前缀和)- HDU 1556
HUDOJ-----1394Minimum Inversion Number
Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9163    Accepted Submission(s): 5642 Problem Description The inversion number of a given number sequence a1, a2, ..., an is
Gxjun
2018/03/22
7480
HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 640    Accepted Submission(s): 210 Problem Description Jam like to solve the problem which on the 3D-axis,given  points  If
ShenduCC
2018/04/27
1.1K0
nyoj----522 Interval (简单树状数组)
Interval 时间限制:2000 ms  |  内存限制:65535 KB 难度:4 描述 There are n(1 <= n <= 100000) intervals [ai, bi] and m(1 <= m <= 100000) queries, -100000 <= ai <= bi <= 100000 are integers. Each query contains an integer xi(-100000 <= x <= 100000). For each query, you sho
Gxjun
2018/03/22
5450
poj----2155 Matrix(二维树状数组第二类)
Matrix Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 16950 Accepted: 6369 Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we have A[i, j] =
Gxjun
2018/03/22
6410
相关推荐
HDUOJ-----2852 KiKi's K-Number(树状数组+二分)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验