POJ 2352 Stars (树状数组)

题意:给定一组点,问二维空间中各个点的等级是多少,点的等级定义如下:其左下方点的个数。

思路:昨天看了树状数组,然后怀着无比激动的心情开始了,一阵子思考--不会,在一阵子思考--还是不会,只能硬着头皮翻报告......终于明白树状数组是怎么应用了....汗了....到现在才开始学树状数组....不管怎么说,树状数组的确是一个强大的在线数据结构!

  
    
#include < iostream >
#include
< cstdio >
#include
< algorithm >
#include
< memory.h >
#include
< cmath >
#include
< ctime >
#include
< bitset >
#include
< queue >
#include
< vector >
using namespace std;

const int BORDER = ( 1 << 20 ) - 1 ;
const int MAXSIZE = 37 ;
const int MAXN = 40000 ;
const int INF = 1000000000 ;
#define CLR(x,y) memset(x,y,sizeof(x))
#define ADD(x) x=((x+1)&BORDER)
#define IN(x) scanf("%d",&x)
#define OUT(x) printf("%d\n",x)
#define MIN(m,v) (m)<(v)?(m):(v)
#define MAX(m,v) (m)>(v)?(m):(v)
#define ABS(x) ((x)>0?(x):-(x))

int tre[MAXN],ans[MAXN],arr[MAXN];
int n_tre,n;

int init()
{
CLR(tre,
0 );
CLR(ans,
0 );
CLR(arr,
0 );
n_tre
= MAXN;
return 0 ;
}
int lowbit( int x)
{
return x & ( - x);
}
void modify( int ind, int delta)
{
while ( ind <= n_tre)
{
tre[ind]
+= delta;
ind
+= lowbit(ind);
}
}
int get_sum( int ind)
{
int sum = 0 ;
while (ind != 0 )
{
sum
+= tre[ind];
ind
-= lowbit(ind);
}
return sum;
}
int work()
{
int i,b,a,j,tmp;
for (i = 0 ; i < n; ++ i)
{
scanf(
" %d%d " , & a, & b);
++ a;
++ ans[get_sum(a)];
modify(a,
1 );
}
for (i = 0 ; i < n; ++ i)
OUT(ans[i]);
return 0 ;
}
int main()
{
while (IN(n) != EOF)
{
init();
work();
}
return 0 ;
}

 

你可能感兴趣的:(树状数组)