POJ 2481 Cows
来源:互联网 发布:mac mp4 srt 编辑:程序博客网 时间:2024/06/03 18:57
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
- poj 2481 Cows
- poj 2481 Cows
- POJ 2481 cows
- Poj 2481 Cows
- poj 2481 cows
- POJ 2481 Cows
- poj 2481 Cows
- POJ 2481 Cows
- POJ 2481 Cows
- POJ 2481 Cows
- POJ 2481 Cows
- POJ:2481 Cows
- poj 2481 Cows
- POJ 2481 Cows
- POJ 2481 Cows
- poj 2481 Cows
- POJ 2481 Cows
- poj 2481 Cows
- ubuntu虚拟机上网设置(打开浏览器出现serve not found)
- Palindrome Linked List
- 二叉树中序遍历(迭代)
- css学习笔记之 inline-block
- APIDemo动画合集
- POJ 2481 Cows
- 暴力拆解hupu球员信息
- Python 3.4 install lxml
- 【LeetCode】Linked List Cycle I&II
- android回调函数
- ubuntu10.10,安装flash插件
- 【代码片段】两列布局
- 加性高斯白噪声信道(AWGN)下的digital调制格式识别分类
- JavaScript Lexical Scope