您的位置:首页 > 其它

POJ 2352 树状数组

2012-04-16 17:02 357 查看
这是一道YY题= =...

确实.. 做这题我就是YY出来的。一交没想到1Y了~ 哈哈哈~ 笑死我了~

树状数组再练一道就罢手!

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

int c[15555],n,lev[15555];
struct point
{
int x,y,pos;
}p[15555];

bool cmp1( point a,point b )
{
if( a.x==b.x )
return a.y<b.y;
return a.x<b.x;
}
bool cmp2( point a,point b )
{
if( a.y==b.y )
return a.x<b.x;
return a.y<b.y;
}

int lowbit( int x ){ return x&-x; }
int sum( int x )
{
int rec=0;
while( x )
{
rec+=c[x];
x-=lowbit(x);
}
return rec;
}
void add( int x,int v )
{
while( x<=n )
{
c[x]+=v;
x+=lowbit(x);
}
}

int main()
{
while( scanf("%d",&n)!=EOF )
{
memset( c,0,sizeof(c) );
memset(lev,0,sizeof(lev));
for( int i=0;i<n;i++ )
scanf( "%d%d",&p[i].x,&p[i].y );
sort( p,p+n,cmp1 );
for( int i=0;i<n;i++ )
p[i].pos=i+1;
sort( p,p+n,cmp2 );
for( int i=0;i<n;i++ )
{
//printf( "pos:%d\n",p[i].pos );
lev[sum(p[i].pos)]++;
add( p[i].pos,1 );
}
for( int i=0;i<n;i++ )
printf( "%d\n",lev[i] );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ini struct c