Cows
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
Hint
Huge input and output,scanf and printf is recommended.
题意:求没一个区间真子集的个数;
题解:我们把所有数据按左区间从大到小,右区间从小到大排序,这样我们查找任意一个区间的真子集个数时只需要考虑有区间即可。若该区间的右区间==上一个区间的右区间,则该区间的真子集的个数等于上一个区间真子集的个数,否则该区间真子集的个数等于横坐标表该区间横坐标小的总和。
1 #include <cstdio> 2 #include <cstring> 3 #include<algorithm> 4 #include<map> 5 #include<stack> 6 #include<cmath> 7 typedef long long ll; 8 using namespace std; 9 const int MAXN=1e6+10; 10 int m,n; 11 struct node 12 { 13 int left; 14 int right; 15 int num; 16 } str[MAXN]; 17 int ans[MAXN]={0}; 18 bool cmp(node a,node b)//排序预处理 19 { 20 if(a.right==b.right) 21 return a.left<b.left; 22 return a.right>b.right; 23 } 24 int Lowbit(int x) 25 { 26 return x&(-x); 27 } 28 void update(int i, int x,int c[]) 29 { 30 while(i <=n) 31 { 32 c[i] += x; 33 i += Lowbit(i); 34 } 35 } 36 int Getsum(int x,int c[]) 37 { 38 int sum=0; 39 while(x>0) 40 { 41 sum+=c[x]; 42 x-=Lowbit(x); 43 } 44 return sum; 45 } 46 int main() 47 { 48 int T,k,flag=0; 49 while(scanf("%d",&n)!=-1&&n) 50 { 51 flag++; 52 int c[MAXN]= {0}; 53 int a[MAXN]={0}; 54 ll kk=0; 55 for(int i=1; i<=n; i++) 56 { 57 scanf("%d%d",&str[i].left,&str[i].right); 58 str[i].num=i; 59 } 60 sort(str+1,str+n+1,cmp); 61 ans[str[1].num]=0; 62 update(str[1].left+1,1,c); 63 for(int i=2; i<=n; i++) 64 { 65 if(str[i].left==str[i-1].left&&str[i].right==str[i-1].right)//判断该区间的右端点与上一个区间的右端点的大小 66 { 67 ans[str[i].num]=ans[str[i-1].num]; 68 } 69 else 70 { 71 ans[str[i].num]=Getsum(str[i].left+1,c); 72 } 73 update(str[i].left+1,1,c); 74 } 75 for(int i=1;i<=n;i++) 76 { 77 printf("%d%c",ans[i],i==n?'\n':' '); 78 } 79 80 } 81 82 return 0; 83 }
本文来自博客园,作者:左手边五十米,转载请注明原文链接:https://www.cnblogs.com/moomcake/p/9409870.html