Cows
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 17484 | Accepted: 5875 |
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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.
For each cow, how many cows are stronger than her? Farmer John needs your help!
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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.
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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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.
For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow
i.
Sample Input
3 1 2 0 3 3 4 0
Sample Output
1 0 0
题目大意:给你很多线段的头S和尾E,问每一条线段中包含了多少个线段,(S和E相同不计在内)。这题先一看,完全不知道什么方法,感觉非常的难办。但是!树状数组可以轻松解决这个问题!!!首先,将她们线段的s和e当做是(s,e)一个点,这样子把所有点画出来,你就会发现一个很神奇的现象,题目要求就会变成:问每一个点的左上角有多少个点?
!!!这样不就和那题最简单的stars一样吗???!!!
stars那题是问左下角有多少个点,而这题是问左上角,而且点不是有序排好的,所以有些不同,特殊处理一下就可以。
如果正常做,那个y是递增的,所以sum和update那个方向就会相反了,这个其实没什么所谓,一样的,排序的时候先y由大到小排,y相同时x由小到大排,这样小小的处理,就变成stars那题了!!!
还有一点忘了,这题也是需要离散化的,离散化很重要很强大!!!
#include <iostream> #include <stdio.h> #include <memory.h> #include <algorithm> using namespace std; struct node { int x,y,id; }a[100005]; int n,b[100005],val[100005]; bool cmp1(node a,node b) { if(a.y!=b.y) return a.y>b.y; return a.x<b.x; } int lowbit(int i) { return i&-i; } void add(int pos, int num) { while(pos <= n) { b[pos] += num; pos += lowbit(pos); } } int sum(int pos) { int res = 0; while(pos > 0) { res += b[pos]; pos -= lowbit(pos); } return res; } int main() { int i; while(scanf("%d",&n),n) { memset(b,0,sizeof(b)); memset(val,0,sizeof(val)); for(int i=0;i<n;i++) { scanf("%d %d",&a[i].x,&a[i].y); a[i].id=i; a[i].x++;//防止为零,否则会超时 a[i].y++; } sort(a,a+n,cmp1); val[a[0].id]=sum(a[0].x); add(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) { val[a[i].id]=val[a[i-1].id]; } else val[a[i].id]=sum(a[i].x); add(a[i].x,1); } printf("%d",val[0]); for(int i=1;i<n;i++) { printf(" %d",val[i]); } printf("\n"); } }