POJ2352 Stars 树状数组

emm,ssy说可以直接CDQ分治...%%%但是注意到y是有序的,所以可以直接求一下前缀和就行了.

题干:

    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. 

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(register int i = a;i <= n;i++)
#define lv(i,a,n) for(register int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
const int INF = 1 << 30;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
    char c;
    bool op = 0;
    while(c = getchar(), c < '0' || c > '9')
        if(c == '-') op = 1;
    x = c - '0';
    while(c = getchar(), c >= '0' && c <= '9')
        x = x * 10 + c - '0';
    if(op) x = -x;
}
template <class T>
void write(T x)
{
    if(x < 0) putchar('-'), x = -x;
    if(x >= 10) write(x / 10);
    putchar('0' + x % 10);
}
const int maxn = 32005;
int tree[1000005];
int ans[100005];
int n;
int lowbit(int x)
{
    return x & -x;
}
void change(int x)
{
    while(x <= maxn)
    {
        tree[x]++;
        x += lowbit(x);
    }
}
int query(int x)
{
    int tot = 0;
    while(x > 0)
    {
        tot += tree[x];
        x -= lowbit(x);
    }
    return tot;
}
int main()
{
    read(n);
    duke(i,1,n)
    {
        int x,y;
        read(x);read(y);
        ans[query(x + 1)]++;
        /*duke(j,1,n)
        {
            printf("%d ",query(j));
        }
        puts("");*/
        change(x + 1);
    }
    duke(i,0,n - 1)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}

 

posted @ 2018-11-14 15:39  DukeLv  阅读(197)  评论(0编辑  收藏  举报