当前位置: 代码迷 >> 综合 >> POJ 2352 树状数组
  详细解决方案

POJ 2352 树状数组

热度:99   发布时间:2024-01-20 20:26:48.0

这是一道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;
}