Stars(BIT树状数组)

Stars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6993    Accepted Submission(s): 2754


Problem Description
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
给你一堆星星的坐标,一个星星的level等于它左下边的星星个数,不包括自己本身,可同x,同y;
而且题目给的星星的顺序是按照y,x坐标来排序的,BIT搞。
 1 /*******************************
 2 
 3 Date    : 2015-12-09 23:16:34
 4 Author  : WQJ (1225234825@qq.com)
 5 Link    : http://www.cnblogs.com/a1225234/
 6 Name     : 
 7 
 8 ********************************/
 9 #include <iostream>
10 #include <cstdio>
11 #include <algorithm>
12 #include <cmath>
13 #include <cstring>
14 #include <string>
15 #include <set>
16 #include <vector>
17 #include <queue>
18 #include <stack>
19 using namespace std;
20 int bit[32000+5];
21 int num[32000+5];
22 int k=32000+5;
23 int lowbit(int i)
24 {
25     return i&-i;
26 }
27 void add(int i,int a)
28 {
29     while(i<=32005)
30     {
31         bit[i]+=a;
32         i=i+lowbit(i);
33     }
34 }
35 int sum(int i)
36 {
37     int s=0;
38     while(i>0)
39     {
40         s+=bit[i];
41         i=i-lowbit(i);
42     }
43     return s;
44 }
45 int main()
46 {    
47     freopen("in.txt","r",stdin);
48     int i,j;
49     int n,x,y;
50     while(scanf("%d",&n)!=EOF)
51     {
52         memset(num,0,sizeof(num));
53         memset(bit,0,sizeof(bit));
54         for(i=0;i<n;i++)
55         {
56             scanf("%d%d",&x,&y);
57             num[sum(x+1)]++;
58             add(x+1,1);
59         }
60         for(i=0;i<n;i++)
61             printf("%d\n",num[i]);
62     }    
63     return 0;
64 }

据说暴力也能过:

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 int a[33000],i,j,k,l,m,n,x[33000],y[33000];
 6 int main()
 7 {
 8     while(scanf("%d",&k)!=EOF)
 9     {
10         memset(a,0,sizeof(a));
11         for(j=0;j<k;j++)
12         {
13         scanf("%d%d",&x[j],&y[j]);
14         int cnt=0;
15         for(l=0;l<j;l++)
16         if(x[l]<=x[j])
17         cnt++;
18         a[cnt]++;
19         }
20         for(i=0;i<k;i++)
21         printf("%d\n",a[i]);
22         
23     }
24     return 0;
25 }

 

 
posted @ 2015-12-09 11:01  御心飞行  阅读(185)  评论(0编辑  收藏  举报