POJ 2481 Cows
2015-07-11 00:41
375 查看
It's a tree_array problem.
The portal:http://poj.org/problem?id=2481
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; }
相关文章推荐
- tomcat 配置
- css学习笔记之 inline-block
- 二叉树中序遍历(迭代)
- 为InfiniBand而哭泣
- Palindrome Linked List
- 为InfiniBand而哭泣
- c语言学习之数组2
- 游戏杂谈
- OOAD之创建型模式之工厂模式
- 测试RegExp对象的属性
- HTTP协议研究
- ubuntu 手动安装flashplayer
- QtLog
- 蛇形抖动
- Activity篇章参考
- Several Python Tools/Utilities
- c语言学习之数组2
- javah、javap的用法
- ASP.NET-DataList控件-DataList嵌套
- [CodeEdit--Sublime]Sublime使用相关