poj 2481 cows 树状数组
2016-07-08 14:38
543 查看
Cows
Description
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a
range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
Hint
Huge input and output,scanf and printf is recommended.
题意:求每个区间是多少个区间的真子集
借用hdu 1541 stars的思想,
把这些区间的左右端点分别看成坐标的x和y值,然后先按y轴从大到小排,其次,y相等再按x从小到大排,最后依次读入每个点,那么这个点左上方的点的个数就是包含它的区间个数.
代码如下:
Time Limit: 3000MS | | Memory Limit: 65536K |
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.
Farmer John has N cows (we number the cows from 1 to N). Each of Farmer John's N cows has a range of clover that she particularly likes (these ranges might overlap). The ranges are defined by a closed interval [S,E].
But some cows are strong and some are weak. Given two cows: cowi and cowj, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cowi is stronger than cowj.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
The input contains multiple test cases.
For each test case, the first line is an integer N (1 <= N <= 105), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 105) specifying the start end location respectively of a
range preferred by some cow. Locations are given as distance from the start of the ridge.
The end of the input contains a single 0.
Output
For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi.
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
Hint
Huge input and output,scanf and printf is recommended.
题意:求每个区间是多少个区间的真子集
借用hdu 1541 stars的思想,
把这些区间的左右端点分别看成坐标的x和y值,然后先按y轴从大到小排,其次,y相等再按x从小到大排,最后依次读入每个点,那么这个点左上方的点的个数就是包含它的区间个数.
代码如下:
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; int maxn=100001; struct node { int x,y; int n; } nod[100001]; int c[100001],ans[100001]; int lowbit(int x) { return x&(-x); } void up(int x,int val) { for(; x<=maxn; x+=lowbit(x)) c[x]+=val; } int sum(int x) { int sum=0; for(; x>0; x-=lowbit(x)) sum+=c[x]; return sum; } inline bool cmp(const node a,const node b) { if(a.y!=b.y) return a.y>b.y;//y不同就按y从大到小排 return a.x<b.x;//y相同就按x从小到大排 } int main() { int x,j,i,n; while(~scanf("%d",&n)&&n) { memset(c,0,sizeof(c)); maxn=n; for(i=1; i<=n; i++) { scanf("%d %d",&nod[i].x,&nod[i].y); nod[i].n=i; } sort(nod+1,nod+n+1,cmp); for(i=1; i<=n; i++) { if (nod[i].x== nod[i - 1].x && nod[i].y== nod[i - 1].y)//如果两点重合,因为是真子集,那么后一个点和前一个点的值应该相同,不能加一 { ans[nod[i].n] = ans[nod[i - 1].n]; } else ans[nod[i].n] = sum(nod[i].x+ 1); up(nod[i].x+ 1,1); } for(i=1; i<n; i++) printf("%d ",ans[i]); printf("%d\n",ans[i]); } return 0; }
相关文章推荐
- C#数据结构之顺序表(SeqList)实例详解
- Lua教程(七):数据结构详解
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构之队列(Quene)实例详解
- C#数据结构揭秘一
- C#数据结构之单链表(LinkList)实例详解
- 数据结构之Treap详解
- 用C语言举例讲解数据结构中的算法复杂度结与顺序表
- C#数据结构之堆栈(Stack)实例详解
- C#数据结构之双向链表(DbLinkList)实例详解
- JavaScript数据结构和算法之图和图算法
- Java数据结构及算法实例:冒泡排序 Bubble Sort
- Java数据结构及算法实例:插入排序 Insertion Sort
- Java数据结构及算法实例:考拉兹猜想 Collatz Conjecture
- java数据结构之java实现栈
- java数据结构之实现双向链表的示例
- Java数据结构及算法实例:选择排序 Selection Sort
- Java数据结构及算法实例:朴素字符匹配 Brute Force
- Java数据结构及算法实例:汉诺塔问题 Hanoi
- Java数据结构及算法实例:快速计算二进制数中1的个数(Fast Bit Counting)