HDU 1541 star tree-like array

Source: Internet
Author: User

Stars Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 2250 Accepted Submission (s): 858


Problem Description Astronomers often examine star maps where stars is represented by points on a plane and All star has Cartesian coordinates. The level of a star is a amount of the stars that is not higher and is not 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 above. Level of the star number 5 are 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 is 1. At this map there is only one star of the level 0, and the level 1, one star of the level 2, and one star of the Level 3.

You is to write a program, that would 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 (both integers X and Y per line separated by a space, 0<=x,y<=320 ). There can be is only one star at a point of the the plane. Stars is listed in ascending order of Y coordinate. Stars with equal Y coordinates is listed in ascending order of X coordinate.
Output the output should contain N lines, one number per line. The first line contains amount's 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



The difficulty of this question is how to use a tree array to store the Stars ' levels in each coordinate.

Test instructions has explained that the input is sorted by the y-coordinate from small to large, and then you can use an array to record the level of the star in this point based on the size of the horizontal axis.

First each time you read into the position of a star, the number of levels that it belongs to is +1, and then you want to update the level of the horizontal axis after it.

 #include <cstdio> #include <iostream> #include <string.h> #include <vector> #
Include <stdlib.h> #include <algorithm> #include <math.h> using namespace std;
int lev[32005];

int sum[32005];    
int lowbit (int x) {return x& (-X);
    } int num (int x) {int s = 0;
           while (x > 0) {s + = sum[x];       
    X-= Lowbit (x);
} return s;
            } void Update (int x) {while (X < 32005) {sum[x]++;       
     x + = Lowbit (x);
    }} int main () {int n,x,y;
           while (~SCANF ("%d", &n)) {memset (lev, 0, sizeof (LEV));
           memset (sum, 0, sizeof (sum));
                for (int i=0; i<n; i++) {scanf ("%d%d", &x,&y);
                ++lev[num (x+1)];
           Update (X+1);
    } for (int i=0; i<n; i++) printf ("%d\n", Lev[i]);
} return 0; } 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.