# HDU-1556 Color the ball 【差分数组】

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

Input

Output

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

sum[i]=d[1]+d[2]+d[3]+...+d[i]=a[1]+a[2]-a[1]+a[3]-a[2]+...+a[i]-a[i-1]=a[i],即前缀和sum[i]=a[i]；

（代码为了体现思想，这题a和sum数组都可以省略）

 1 #include<bits/stdc++.h>
2 using namespace std;
3 #define INF 0x3f3f3f3f
4 #define M(a, b) memset(a, b, sizeof(a))
5 const int N = 100000 + 10;
6 int a[N], d[N], sum[N];
7
8 int main() {
9     int n;
10     while (scanf("%d", &n), n) {
11         M(a, 0);
12         d[1] = a[1];
13         for (int i = 2; i <= n; ++i) d[i] = a[i] - a[i-1];
14         int l, r;
15         for (int i = 1; i <= n; ++i) {
16             scanf("%d%d", &l, &r);
17             d[l] += 1;
18             d[r+1] -= 1;
19         }
20         M(sum, 0);
21         for (int i = 1; i <= n; ++i) sum[i] = sum[i-1] + d[i];
22         for (int i = 1; i < n; ++i) printf("%d ", sum[i]);
23         printf("%d\n", sum[n]);
24     }
25
26     return 0;
27 }

posted @ 2017-05-16 20:03  Robin!  阅读(564)  评论(0编辑  收藏