POJ

来源:互联网 发布:淘宝网儿童女式凉鞋 编辑:程序博客网 时间:2024/06/05 08:38

注意输入有0出现,BIT不好处理,所以在输入的时候加1

// 树状数组// http://blog.csdn.net/niushuai666/article/details/7389273#include <cstdio>#include <string.h>#define MAX 32000 + 10using namespace std;typedef long long LL;LL c[MAX];LL ans[MAX];LL lowbit( LL i ) {    return i & ( -i );}void update( LL i, LL val, LL n ) {    while( i <= n ) {        c[i] = c[i] + val;        i = i + lowbit( i );    }}LL getSum( LL i ) {    LL ans = 0;    while( i > 0 ) {        ans = ans + c[i];        i = i - lowbit( i );    }    return ans;}int main() {    LL n;    while( scanf( "%lld", &n ) != EOF ) {        memset( c, 0, sizeof( c ) );        memset( ans, 0, sizeof( ans ) );        LL x, y;        for( LL i = 1; i <= n; i++ ) {            scanf( "%lld%lld", &x, &y );            x++; //有0出现,树状数组无法处理。故+1            ans[getSum( x )]++; //先统计,不包括本身            update( x, 1, MAX ); //加入        }        for( LL i = 0; i < n; i++ ) {            printf( "%lld\n", ans[i] );        }    }    return 0;}


原创粉丝点击