首页 > 代码库 > POJ2352(树状数组)

POJ2352(树状数组)

Stars
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 33786 Accepted: 14741
题目链接:http://poj.org/problem?id=2352

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

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed.





解题思路:
题目大意就是看描述中给的那个图,每次找当前星星(不包括本身)左下方区域(包括左面水平线)有多少颗星星,则它的Level就是几。如对于1号星星,左下方没有包含其他星星,所以它的Level就是0;对于2号和4号星星,他们的左下方都包含了一颗星星,所以他们的Level是1;对于5号星星,它的左下方区域包含了1号、2号、4号,所以它的Level是3;对于3号星星,它的左下方区域包含了1号和2号,所以他的Level是2。
最后输出的要求是从Level0~Leveln-1所对应的星星数量。
最近在熟悉模板(检验模板的正确性),同时加深对以前算法的理解,学习模板上新的算法。就敲了这道树状数组模板题。下面简单谈谈树状数组:
树状数组组可以做到O(logn)时间的查询和添加,实现起来比线段树相对容易,并且可以扩展到多维。

如上图示,(1)如果下标为奇数,那么c[ i ] = a[ i ] ; (2)如果下标为偶数,那么看 i 的因子里最多有多少2的幂数,如 i = 6 ,最多有2^1,所以c[ 6 ] = a[ 6 ] + a[ 5 ] ; 如 i = 4 , 最多有2^2,所以c[4] = a[1] + a[2] + a[3] + a[4]。即对于i 为偶数来说,i 的因子里最多有2^k,那么就向前找k个数(包括本身)。
回到本题,这道题算是一道模板题。因为他的Y在输入时按照升序输入,所以我们只需要关注x的变化即可。这里需要注意的是,树状数组的下标从1开始,而题目里的x可能取值0,所以我们在计算使每次都把x向右移动一个单位。这里可以这么理解,看上面树状数组的形成图,没有0吧,所以下标不能从0开始。



完整代码:
#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <climits>
#include <cassert>
#include <complex>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")

typedef long long LL;
typedef double DB;
typedef unsigned uint;
typedef unsigned long long uLL;

/** Constant List .. **/ //{

const int MOD = int(1e9)+7;
const int INF = 0x3f3f3f3f;
const LL INFF = 0x3f3f3f3f3f3f3f3fLL;
const DB EPS = 1e-9;
const DB OO = 1e20;
const DB PI = acos(-1.0); //M_PI;
const int maxn = 100001;
int tree[maxn];
int level[maxn];
int x , y;
inline int lowbit(int x)
{
    return (x & -x);
}

void add(int x , int value)
{
    for(int i = x ; i <= maxn ; i += lowbit(i))
    {
        tree[i] += value;
    }
}

int get(int x)
{
    int sum = 0;
    for(int i = x ; i ; i -= lowbit(i))
    {
        sum += tree[i];
    }
    return sum;
}

int main()
{
    #ifdef DoubleQ
    freopen("in.txt","r",stdin);
    #endif
    int n;
    while(~scanf("%d",&n))
    {
        memset(level , 0 , sizeof(level));
        for(int i = 0 ; i < n ; i ++)
        {
            scanf("%d%d",&x,&y);
            x ++;
            level[get(x)] ++;
            add(x , 1);
        }
        for(int i = 0 ; i < n ; i ++)
            printf("%d\n" , level[i]);
    }
}


POJ2352(树状数组)