ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1028. Stars

I counldnt solve this problem easy and got AC only using AVL-tree... Can you explain me the easiest solution?
Posted by Petrenuk (NNSTU) 4 Feb 2011 05:13
int main()
{
    int N = 0;
    scanf("%d",&N);
    rangs = new int[N];
    Star *stars = new Star[N];

    for(int i = 0; i < N; i++) {
        scanf("%d %d", &stars[i].x, &stars[i].y);
    }

    qsort(stars, N, sizeof(Star), StarCompare);

    AVLTreeNode *root = new AVLTreeNode();

    for(int i = 0; i < N; i++) {
        rangs[i] = 0;
    }

    for(int i = 0; i < N; i++) {
        AVLTreeNode::Insert(root, stars[i].y, 0);
    }

    for(int i = 0; i < N; i++) {
        printf("%d\n", rangs[i]);
    }

    return 0;
}

Edited by author 05.02.2011 00:23
Re: I counldnt solve this problem easy and got AC only using AVL-tree... Can you explain me the easiest solution?
Posted by Artem Khizha [DNU] 6 Feb 2011 16:41
Two advices:
1) There's no need in Y-coordinates since points are sorted in input. Ignore them.
2) You should find a way to answer quickly on queries "how many numbers are lesser than given one".

P.S. I used Binary Indexed Tree (Fenwick's Tree) for this problem and got a code of the same length as your pseudocode. ;-)