POJ

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

考查树状数组 + 排序

// 树状数组 + 排序#include <cstdio>#include <string.h>#include <algorithm>#define MAX 100010using namespace std;typedef long long LL;typedef struct {    LL x;    LL y;    LL id;} Node;Node node[MAX];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;}bool cmp( Node a, Node b ) {    //if( a.y == b.y ) return a.x < b.x ? 1 : 0;    //return a.y > b.y ? 1 : 0;    if( a.y != b.y ) return a.y > b.y ? 1 : 0;    return a.x < b.x ? 1 : 0;}int main() {    LL n;    while( scanf( "%lld", &n ) != EOF ) {        if( n == 0 ) break;        memset( c, 0, sizeof( c ) );        memset( ans, 0, sizeof( ans ) );        for( LL i = 1; i <= n; i++ ) {            scanf( "%lld%lld", &node[i].x, &node[i].y );            node[i].id = i;            node[i].x++;            node[i].y++;        }        sort( node + 1, node + 1 + n, cmp );        ans[node[1].id] = getSum( node[1].x );        update( node[1].x, 1, MAX );        for( LL i = 2; i <= n; i++ ) {            if( node[i].x == node[i - 1].x && node[i].y == node[i - 1].y ) {                ans[node[i].id] = ans[node[i - 1].id];                update( node[i].x, 1, MAX );            }            else {                ans[node[i].id] = getSum( node[i].x );                update( node[i].x, 1, MAX );            }        }        for( LL i = 1; i <= n; i++ ) {            if( i == 1 ) printf( "%lld", ans[i] );            else printf( " %lld", ans[i] );        }        printf( "\n" );    }    return 0;}


原创粉丝点击