HDU - 1556 Color the ball(线段树和树状数组)

题意:n个气球,n个操作(每次给区间[a,b]的气球涂颜色),求最后每个气球被涂颜色的次数。

n可以取到100000,所以如果想for,for的话,就 爆!爆!爆!

1.线段树做法:

一道简单的线段树,做了好久都没做出来,果然自己还是太菜了。。。

创建一棵线段树,把里面的涂色次数对应的值都初始化为0。

更新的时候,找到那个要涂色的区间,涂色次数+1。

询问的时候从上往下找到那个区间,每次把值都递归出来。

 1 #define LC(a) ((a<<1)+1)
 2 #define RC(a) ((a<<1)+2)
 3 #define MID(a,b) ((a+b)>>1)
 4 #include <stdio.h>
 5
 6 struct node{
 7     int l,r;
 8     int mid;
 9     int val;
10 };
11
12 const int maxn=111111;
13 node T[maxn*4];
14
15 void create(int p,int l,int r){
16     T[p].l=l,T[p].r=r,T[p].mid=MID(l,r),T[p].val=0;//初始化所有的val都为0
17     if(l==r) return ;//如果找到最下面就退出
18     create(LC(p),l,T[p].mid);
19     create(RC(p),T[p].mid+1,r);
20 }
21
22 void update(int p,int l,int r){
23     if(T[p].l==l&&T[p].r==r){//找到这个节点,+1,递归出口
24         T[p].val++;
25         return ;
26     }
27     if(T[p].l==T[p].r) return ;//没找到这个节点,递归出口,特殊情况的出口
28     if(T[p].mid>=r) update(LC(p),l,r);//往左子树找
29     else if(T[p].mid<l) update(RC(p),l,r);//右子树找
30     else{                                //如果是分开的区间,两边都要找
31         update(LC(p),l,T[p].mid);
32         update(RC(p),T[p].mid+1,r);
33     }
34 }
35
36 int query(int p,int l,int r){
37     if(T[p].l==l&&T[p].r==r) return T[p].val;//如果找到这个区间,就返回。递归,之前的值也都加上来了
38     if(T[p].l==T[p].r) return 0;//特殊情况的出口
39     if(r<=T[p].mid) return query(LC(p),l,r)+T[p].val;
40     else if(l>T[p].mid) return query(RC(p),l,r)+T[p].val;
41     else return query(LC(p),l,T[p].mid)+query(RC(p),T[p].mid+1,r)+T[p].val;
42 }
43
44 int main(){
45     int n,a,b;
46     while(scanf("%d",&n)!=EOF&&n!=0){
47         create(0,1,n);
48         for(int i=1;i<=n;i++){
49             scanf("%d %d",&a,&b);
50             update(0,a,b);
51         }
52         for(int i=1;i<=n;i++){
53             int sum=query(0,i,i);
54             printf("%d",sum);
55             if(i!=n) printf(" ");
56         }
57         printf("\n");
58     }
59     return 0;
60 }

2.树状数组做法:

时间: 2024-12-17 19:58:40

HDU - 1556 Color the ball(线段树和树状数组)的相关文章

HDU 1556 Color the ball 线段树

HDU 1556 Color the ball 线段树模版题,存个模板 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #include <deque> 7 #include <vector> 8 #include <queue> 9 #inclu

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): 9120    Accepted Submission(s): 4665 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽

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): 8984    Accepted Submission(s): 4594 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球

hdu 1556 Color the ball(线段树区间维护+单点求值)

传送门:Color the ball Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 25511    Accepted Submission(s): 12393 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele

HDU 1556 Color the Ball 线段树 题解

本题使用线段树自然能够,由于区间的问题. 这里比較难想的就是: 1 最后更新须要查询全部叶子节点的值,故此须要使用O(nlgn)时间效率更新全部点. 2 截取区间不能有半点差错.否则答案错误. 这两点卡了我下.看来我的线段树还是不够熟,须要多多练习. 线段树是二分法的高级应用,可是却不是简单应用,要锻炼好并应用好线段树思维还是比二分法难非常多的. static const int SIZE = 100005; static const int TREESIZE = SIZE<<2; int a

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): 7941    Accepted Submission(s): 4070 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电

【线段树】hdu 1556 Color the ball

[线段树]hdu 1556 Color the ball 题目链接:hdu 1556 Color the ball 题目大意 给你N个气球,不断刷新指定区间的颜色,刷新N次,最后输出每一个气球的刷新次数. 上一篇文章是线段树的点修改.区间查询: 这篇文章是线段树的区间修改.点查询: 说一下思路 线段树的区间修改:利用线段树的区间查询,查询到叶节点segTree[root].sum++,而如果对区间进行多次点修改的话,注定超时 线段树的点查询:以为用之前的区间查询就可以了,实际上还是有改变,因为原

HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)

HDU - 1556 Color the ball Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气

hdu 1556 Color the ball(树状数组)

转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气球已经涂过几次颜色了,你能帮他算出每个气

hdu 1556 Color the ball (扫描线+树状数组)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556 Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14237    Accepted Submission(s): 7120 Problem Description N个气球排成一排,从左到右依次编号为1,