# 树状数组怒刷sum!!!（前缀和应用）

## 1. Stars

Astronomers often examine star maps where stars are represented by points on a plane and each star has Cartesian coordinates. Let the level of a star be an amount of the stars that are not higher and not to the right of the given star. Astronomers want to know the distribution of the levels of the stars.

For example, look at the map shown on the figure above. Level of the star number 5 is equal to 3 (it's formed by three stars with a numbers 1, 2 and 4). And the levels of the stars numbered by 2 and 4 are 1. At this map there are only one star of the level 0, two stars of the level 1, one star of the level 2, and one star of the level 3.
You are to write a program that will count the amounts of the stars of each level on a given map.
Input
The first line of the input file contains a number of stars N (1<=N<=15000). The following N lines describe coordinates of stars (two integers X and Y per line separated by a space, 0<=X,Y<=32000). There can be only one star at one point of the plane. Stars are listed in ascending order of Y coordinate. Stars with equal Y coordinates are listed in ascending order of X coordinate.
Output
The output should contain N lines, one number per line. The first line contains amount of stars of the level 0, the second does amount of stars of the level 1 and so on, the last line contains amount of stars of the level N-1.
Sample Input
5
1 1
5 1
7 1
3 3
5 5
Sample Output
1
2
1
1
0
Hint
This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.

 1 #include<cstdio>
2 #include<cstring>
3 #include<algorithm>
4 using namespace std;
5 int lev[32010];
6 int c[32010];///树状数组
7 int lowbit(int x)
8 {
9     return x&(-x);
10 }
11 int Getsum(int x)
12 {
13     int s=0;
14     while(x>0)
15     {
16         s+=c[x];
17         x-=lowbit(x);
18     }
19     return s;
20 }
21 void update(int x,int n)
22 {
23     while(x<32010)
24     {
25         c[x]++;///将星星x坐标加1
26         x+=lowbit(x);
27     }
28 }
29 int main()
30 {
31     int x,y,i,n;
32     while(scanf("%d",&n)!=EOF)
33     {
34         memset(lev,0,sizeof(lev));
35         memset(c,0,sizeof(c));
36         for(i=0; i<n; i++)
37         {
38             scanf("%d%d",&x,&y);
39             x++;///要注意坐标有可能为0，为0时会死循环，所以在读入坐标时应加1。
40             lev[Getsum(x)]++;///每次更新进一个星星，都要统计这个星星之前的星星的个数
41             update(x,1);
42         }
43         for(i=0; i<n; i++)
44         {
45             printf("%d\n",lev[i]);
46         }
47     }
48     return 0;
49 }

## 2.Color the ball

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

Input

Output

3
1 1
2 2
3 3
3
1 1
1 2
1 3
0
Sample Output
1 1 1
3 2 1解题思路；这道题我们在前面学习线段树的时候已经做过了，当时我们的思路是每涂一段色就相当区域更新（该区域每一点都+1），然后单点查询最后每一个点的状态。也就是线段数的区段更新，单点查找现在我们用树状数组来思考一下，这里我们使用一种技巧，比如我们要将[3,6]区段的数据+1,维护数据的过程我们不必去理会，使用树状数组，我们只需要将3那个点更新+1，和6之后的一个点也就是7更新为-7就可以了。为什么要这样呢？因为我们最后需要的是那个sum数组，也就是要维护的前缀和！比如原先【3,6】区间全是0，那么此番操作后sum[1]=0;sum[2]=0;sum[3]=1;sum[4]=1;sum[5]=1;sum[6]=1;sum[7]=0;sum[8]=0;发现了吗？现在的前缀和sum数组竟然可以来表示更新后单点的数据！其实这也是树状数组的区段更新单点查找！
 1 #include <cstdio>
2 #include <cstring>
3 #define MAXN 100010
4 int c[MAXN];
5 int n;
6 int lowbit( int x )
7 {
8     return x&(-x);
9 }
10 void add( int i,int val) ///更新
11 {
12     while(i<=n)
13     {
14         c[i]+=val;
15         i+=lowbit(i);
16     }
17 }
18 int Getsum(int i)
19 {
20     int sum=0;
21     while(i>0)
22     {
23         sum+=c[i];
24         i-=lowbit(i);
25     }
26     return sum;
27 }
28 int main()
29 {
30     int i,x,y;
31     while(scanf("%d",&n)!=EOF)
32     {
33         if(n==0)
34         {
35             break;
36         }
37         memset(c,0,sizeof(c));
38         for(i=0;i<n;i++)
39         {
40             scanf("%d%d",&x,&y);
43         }
44         for(i=1;i<=n;i++)
45         {
46             if(i==n)
47             {
48                 printf("%d\n",Getsum(n));
49             }
50             else
51             {
52                 printf("%d ",Getsum(i));
53             }
54         }
55     }
56     return 0;
57 }

## 3.Japan


Japan plans to welcome the ACM ICPC World Finals and a lot of roads must be built for the venue. Japan is tall island with N cities on the East coast and M cities on the West coast (M <= 1000, N <= 1000). K superhighways will be build. Cities on each coast are numbered 1, 2, ... from North to South. Each superhighway is straight line and connects city on the East coast with city of the West coast. The funding for the construction is guaranteed by ACM. A major portion of the sum is determined by the number of crossings between superhighways. At most two superhighways cross at one location. Write a program that calculates the number of the crossings between superhighways.
Input
The input file starts with T - the number of test cases. Each test case starts with three numbers – N, M, K. Each of the next K lines contains two numbers – the numbers of cities connected by the superhighway. The first one is the number of the city on the East coast and second one is the number of the city of the West coast.
Output
For each test case write one line on the standard output:
Test case (case number): (number of crossings)

Sample Input
1
3 4 4
1 4
2 3
3 2
3 1
Sample Output
Test case 1: 5题目意思：

日本岛东海岸与西海岸分别有N和M个城市，现在修高速公路连接东西海岸的城市，求交点个数。

解题思路：

 1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <algorithm>
5 using namespace std;
6 #define LL long long
7 #define N 1005*1005
8 LL ans;
9 struct node
10 {
11     int l;
12     int r;
13 } a[N];
14 int n,c[N];
15 int my_cmp(node a,node b)
16 {
17     if(a.l!=b.l)
18         return a.l<b.l;
19     return a.r<b.r;
20 }
21 int lowbit(int x)
22 {
23     return x&-x;
24 }
25 int Getsum(int x)
26 {
27     int ret = 0;
28     while(x>0)
29     {
30         ret+=c[x];
31         x-=lowbit(x);
32     }
33     return ret;
34 }
35
36 void add(int x,int d,int y)
37 {
38     while(x<=y)
39     {
40         c[x]+=d;
41         x+=lowbit(x);
42     }
43 }
44 int main()
45 {
46     int i,j,k,l,r,t,cas = 1,x,y;
47     scanf("%d",&t);
48     while(t--)
49     {
50         scanf("%d%d%d",&x,&y,&n);
51         memset(c,0,sizeof(c));
52         for(i = 1; i<=n; i++)
53         {
54             scanf("%d%d",&a[i].l,&a[i].r);
55         }
56         sort(a+1,a+1+n,my_cmp);
57         ans = 0;
58         for(i = 1; i<=n; i++)
59         {
67