关闭

POJ 2481 Cows

标签: acmHDUTree_Arrayitpoj
160人阅读 评论(0) 收藏 举报

It's a tree_array problem.

The portal:http://poj.org/problem?id=2481

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>

using namespace std;

const int N = 1e5+5;
const int M = 1e5+5;

int Bit[M << 1];

struct Node {
	int x,y,r,value;
}a[N];

void Insert(int x,int value) {
	for(int i = x; i <= M; i += i & (-i)){
		Bit[i] += value;
	}
}

int Query(int x){
	int ret = 0;
	if(x <= 0)return 0;
	for(int i = x;i;i -= i & (-i)){
		ret += Bit[i];
	}
	return ret;
}

int cmp1(const void * a1,const void * a2){
	struct Node p1 = *(struct Node *)a1;
	struct Node p2 = *(struct Node *)a2;
	if(p1.y == p2.y) return p1.x - p2.x;
	return p2.y - p1.y;
}

int cmp2(const void * a1,const void * a2){
	struct Node p1 = *(struct Node *)a1;
	struct Node p2 = *(struct Node *)a2;
	return p1.r - p2.r;
}

void Deal_with(){
	int n;
	while(scanf("%d",&n),n){
		memset(a,0,sizeof(a));
		memset(Bit,0,sizeof(Bit));
		for(int i=0;i<n;i++){
			scanf("%d %d",&a[i].x,&a[i].y);
			a[i].x ++ ; a[i].y ++;
			//Because a[i].x may equal '0';
			a[i].r = i;
		}
		qsort(a,n,sizeof(struct Node),cmp1);
		//Use y to first condition.(From higher to lower);Use x to second condition.(From lower to higher.) 
		Insert(a[0].x,1);
		for(int i=1;i<n;i++){
			if(a[i].x == a[i-1].x && a[i].y == a[i-1].y){
				a[i].value = a[i-1].value;
				Insert(a[i].x,1);
				//Add the line is important; 
				continue;
			}
			a[i].value = Query(a[i].x);
			Insert(a[i].x,1);
		}
		qsort(a,n,sizeof(struct Node),cmp2);
		//Use a[i].r to recmp the array a;
		for(int i=0;i<n;i++){
			printf("%d",a[i].value);
			printf(i == n - 1 ? "\n" : " ");
			// if (i == n - 1) printf("\n"); else printf(" ");
		}
	}
}

int main(void){
	//freopen("a.in","r",stdin);
	Deal_with();
	return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:59380次
    • 积分:3592
    • 等级:
    • 排名:第9127名
    • 原创:315篇
    • 转载:3篇
    • 译文:0篇
    • 评论:4条
    文章分类
    最新评论