poj2352

树状数组,注意树状数组范围

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

#define maxm 32005

int n, ans[maxm];
int ar[maxm];

int lowb( int t)
{
return t & ( - t);
}

void add( int i, int v)
{
for (; i < maxm; ar[i] += v, i += lowb(i))
;
}

int sum( int i)
{
int s = 0 ;
for (; i > 0 ; s += ar[i], i -= lowb(i))
;
return s;
}
int main()
{
// freopen("t.txt", "r", stdin);
memset(ans, 0 , sizeof (ans));
memset(ar,
0 , sizeof (ar));
scanf(
" %d " , & n);
for ( int i = 0 ; i < n; i ++ )
{
int x, y;
scanf(
" %d%d " , & x, & y);
x
++ ;
ans[sum(x)]
++ ;
add(x,
1 );
}
for ( int i = 0 ; i < n; i ++ )
printf(
" %d\n " , ans[i]);

return 0 ;
}

你可能感兴趣的:(poj)