线段树和树状数组都可以用
#include <stdio.h> #include <stdlib.h>
const int LEN = 15005;
//坐标处理 struct NODE { int x; //X坐标 int y; //Y坐标 int addr; //第几个点 }node[LEN];
int cmp ( const void *a, const void *b ) {
int ans = ( (NODE *)a )->x-( (NODE *)b )->x; if ( !ans ) { ans = ( (NODE *)a )->y-( (NODE *)b )->y; } return ans; }
//记录答案的数组 int answer[LEN];
//线段树 struct { int num; //区间中点的个数 int b, e; //左右区间 int lson, rson; //左右孩子 }tree[LEN*25]; int next_tree;
void creat ( int b, int e ) //建树 {
int p = next_tree; next_tree ++; tree[p].b = b; tree[p].e = e; tree[p].lson = -1; tree[p].rson = -1; tree[p].num = 0;
if ( b!=e ) { tree[p].lson = next_tree; creat ( b, (b+e)/2 );
tree[p].rson = next_tree; creat ( (b+e)/2+1, e ); } }
void ins ( int p, int key ) //插入一个结点 {
if ( tree[p].b==tree[p].e ) { tree[p].num ++; } else { int mid = ( tree[p].b+tree[p].e )/2; if ( key>mid ) { ins ( tree[p].rson, key ); } else { ins ( tree[p].lson, key ); } tree[p].num = tree[ tree[p].lson ].num+tree[ tree[p].rson ].num; } }
int ser ( int p, int key ) //查找函数 {
if ( tree[p].b==tree[p].e ) { return tree[p].num; } else { int mid = ( tree[p].b+tree[p].e )/2; if ( key>mid ) { return tree[ tree[p].lson ].num + ser ( tree[p].rson, key ); } else { return ser ( tree[p].lson, key ); } } }
void work ( int n, int max ) //主要的工作函数 {
next_tree = 0; for ( int i=0; i<n; i++ ) { answer[i] = 0; }
qsort ( node, n, sizeof ( NODE ), cmp ); creat ( 0, max ); for ( int i=0; i<n; i++ ) { int addr = ser ( 0, node[i].y ); answer[addr] ++ ; ins ( 0, node[i].y ); } }
int main () {
int n; int max = -1;; scanf ( "%d", &n ); for ( int i=0; i<n; i++ ) { scanf ( "%d%d", &node[i].x, &node[i].y ); node[i].addr = i; if ( node[i].y>max ) { max = node[i].y; } }
work ( n, max ); for ( int i=0; i<n; i++ ) { printf ( "%d\n", answer[i] ); } }
|
|
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
24 | 25 | 26 | 27 | 28 | 29 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 |
|
公告
决定从线程开始!!
常用链接
留言簿(6)
随笔档案
搜索
最新评论
阅读排行榜
评论排行榜
|
|