树状数组怒刷sum!!!(前缀和应用)

我们知道我们利用树状数组维护的是存到其中的a[ ]数组,但是我们做题需要的是sum[ ]数组,这才是我们真正需要的有用的信息,写这篇博客的目的便是整理一下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.
 
题目意思:有N个星星,给出N个星星的笛卡尔坐标,但是坐标给出的顺序有一定的规律,按照y坐标从小到大给出,当y坐标相同的时候按照x坐标的大小顺序给出;之后输出处在各个等级的星星的个数,等级就是看y坐标和x坐标同时小于等于这个星星的个数之和。
 
解题思路:我们直达星星坐标的y是按照从小到大给出的,那么我们算一个星星的等级只要判断有多少个星星的x是不是大于等于现在输入的x不就好了吗?
或许还可以这样理解,星星的等级是它左下角的个数,而按照y从小到大的排序,y是从小的位置依次攀升到大的位置,纵使后面的星星的x坐标可能会小于前面星星的x坐标,但将这些星星压缩到一个高度,左边包含自身位置星星的个数就是所求的等级。
在这里得到sum数组是lev数组(等级数组)的下标。
 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
每个测试实例第一行为一个整数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

解题思路;这道题我们在前面学习线段树的时候已经做过了,当时我们的思路是每涂一段色就相当区域更新(该区域每一点都+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);
41             add(x,1);
42             add(y+1,-1);
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个城市,现在修高速公路连接东西海岸的城市,求交点个数。

解题思路:

记每条告诉公路为(x,y), 即东岸的第x个城市与西岸的第y个城市修一条路。当两条路有交点时,满足(x1-x2)*(y1-y2) < 0。所以,将每条路按x从小到达排序,若x相同,按y从小到大排序。 然后按排序后的公路用树状数组在线更新,求y的逆序数之 和 即为交点个数。

由于x是从小到大排序的,假设当前我们在处理第k条边,那么第1~k-1条边的x必然是小于(等于时候暂且不讨论)第k条边的 x 的,那么前k-1条边中,与第k条边相交的边的y值必然大于yk的,所以此时我们只需要求出在前k-1条边中有多少条边的y值在区间[yk, M]即可,也就是求yk的逆序数,M为西岸城市个数,即y的最大值。 所以就将问题转化成区间求和的问题,树状数组解决。当两条边的x相同时,我们记这两条边的y值分别为ya,yb(ya<yb),我们先处理(x,ya),再处理(x,yb),原因很明显,因为当x相同时,这两条边是认为没有交点的,若先处理(x,yb),那么下次处理(x,ya)时,(x,ya)就会给(x,yb)增加一个逆序,也就是将这两条边做相交处理了,这是不正确的。


 

 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         {
60             add(a[i].r,1,y);
61             ans+=Getsum(y)-Getsum(a[i].r);
62         }
63         printf("Test case %d: %lld\n",cas++,ans);
64     }
65     return 0;
66 }
67  

 

 
posted @ 2018-08-08 21:08  王陸  阅读(867)  评论(0编辑  收藏  举报