[HDOJ1556]Color the ball

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1556

树状数组的第二类应用:区间更新。需要做的是将update函数修改为由a更新到n+权值即可,然后更新b到n-权值就可以实现固定区间的更新。

代码如下:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 #include <iostream>
 6 #include <cmath>
 7 #include <queue>
 8 #include <map>
 9 #include <stack>
10 #include <list>
11 #include <vector>
12
13 using namespace std;
14
15 const int maxn = 100010;
16 int d[maxn<<1];
17 int n, a, b;
18
19 //求某点管辖范围
20 int lowbit(int x) { //求x末尾最低位1的位置(末尾0的个数+1)
21     // return x & (x ^ (x - 1));
22     return x & (-x);
23 }
24
25 //更新树状数组(i到x)
26 void update(int i, int x, int num) {
27     while(i <= x) {
28         d[i] += num;
29         i += lowbit(i);
30     }
31 }
32
33 //获取前x项和
34 int getsum(int x) {
35     int sum = 0;
36     while(x > 0) {
37         sum += d[x];
38         x -= lowbit(x);
39     }
40     return sum;
41 }
42
43 int main() {
44     // freopen("in", "r", stdin);
45     while(~scanf("%d", &n) && n) {
46         memset(d, 0, sizeof(d));
47         for(int i = 1; i <= n; i++) {
48             scanf("%d %d", &a, &b);
49             update(a, n, 1);
50             update(b+1, n, -1);
51         }
52         for(int i = 1; i <= n; i++) {
53             int ans;
54             if(i == 1) {
55                 printf("%d", d[i]);
56             }
57             else {
58                 printf("%d", getsum(i));
59             }
60             if(i != n) {
61                 printf(" ");
62             }
63             else {
64                 printf("\n");
65             }
66         }
67     }
68 }
时间: 2025-01-05 14:44:43

[HDOJ1556]Color the ball的相关文章

HDOJ1556 Color the ball 【线段树】+【树状数组】+【标记法】

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 8272    Accepted Submission(s): 4239 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌

Color the ball(树状数组+线段树)

Color the ball Time Limit : 9000/3000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 3   Accepted Submission(s) : 1 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b

Color the ball(hdu1556)(hash)或(线段树,区间更新)

Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 9502 Accepted Submission(s): 4872 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球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): 22122    Accepted Submission(s): 10715 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"

HDU 1199 Color the Ball

Color the Ball Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 119964-bit integer IO format: %I64d      Java class name: Main There are infinite balls in a line (numbered 1 2 3 ....), and initially all of the

Color the ball 树状数组的区间修改

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9712    Accepted Submission(s): 4998 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌

Color the ball 前缀和+思维

Color the ball 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

HDU 1556 Color the ball【算法的优化】

/* 解题思路:每次仅仅求解一開始的第一个数字,让第一个数字加一,最后的一个数字的后面一个数减一.我们能够想想,最后加的时候,就是加上前面一个数出现的次数和自己本身出现的次数. 解题人:lingnichong 解题时间:2014-10-25 10:30:46 解题体会:因为測试区间非常大,所以此题非常easy超时 */ Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav

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