您的位置:首页 > 其它

POJ 2481 Cows

2015-07-11 00:41 316 查看
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
;

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: