2724: 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!

Entry

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.

Export

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

The meaning of problems

Each cow has a grazing area [S, E]; If the condition Si <= Sj and Ej <= Ei and Ei - Si> Ej - Sj, described stronger than the i-j cow cow. Seek stronger than their number of cows on each cow.

Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+5;
struct node
{
    int S,E,id;
}e[N];
int n,maxn;
int cmp(node x,node y)
{
    return x.S<y.S||x.S==y.S&&x.E>y.E;
}
int f[N],num[N];
void update(int x,int data)
{
      for(int i=x;i<N;i+=i&-i)
      {
          f[i]+=data;
      }
}
int Getsum(int x)
{
    int result=0;
    for(int i=x;i>=1;i-=i&-i)
    {
       result+=f[i];
    }
    return result;
}
int main()
{
     while(~scanf("%d",&n),n)
     {
          for(int i=1;i<=n;i++)
          {
              scanf("%d%d",&e[i].S,&e[i].E);
              e[i].id=i;
          }
          sort(e+1,e+n+1,cmp);
          memset(f,0,sizeof(f));
          num[e[1].id]=0;
          update(e[1].E,1);
          maxn=e[1].E;
          for(int i=2;i<=n;i++)
          {
              if(e[i].S==e[i-1].S&&e[i].E==e[i-1].E)
              num[i]=num[i-1];
              num[e[i].id]=Getsum(maxn)-Getsum(e[i].E-1);
              update(e[i].E,1);
              maxn=max(maxn,e[i].E);
          }
          for(int i=1;i<=n;i++)
          {
              if(i!=1)printf(" ");
              printf("%d",num[i]);
          }
          printf("\n");
     }
}
View Code

 

Guess you like

Origin www.cnblogs.com/llhsbg/p/11229264.html