题意:
n个牛,每个牛对应一个区间,对于每个牛求n个区间有几个包含该牛的区间。
分析:
先 区间右边界从大到小排序,相同时左边界小到大,统计第i头牛即左边界在前i-1头左边界的正序数。
#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <cstdio>
#include <vector>
#include <string>
#include <cctype>
#include <complex>
#include <cassert>
#include <utility>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int,int> PII;
typedef long long ll;
#define lson l,m,rt<<1
#define pi acos(-1.0)
#define rson m+1,r,rt<<11
#define All 1,N,1
#define read freopen("in.txt", "r", stdin)
#define N 100010
const ll INFll = 0x3f3f3f3f3f3f3f3fLL;
const int INF= 0x7ffffff;
const int mod = ;
int bit[N],tmp[N],n;
struct node{
int s,e,index;
}a[N];
bool cmp(node x,node y){
if(y.e==x.e)
return x.s<y.s;
else
return x.e>y.e;
}
int sum(int x){
int num=;
while(x>){
num+=bit[x];
x-=(x&(-x));
}
return num;
}
void add(int x,int d){
while(x<=N){
bit[x]+=d;
x+=(x&(-x));
}
}
void solve(){
memset(bit,,sizeof(bit));
sort(a,a+n,cmp);
tmp[a[].index]=;
add(a[].s,);
for(int i=;i<n;++i){
if(a[i].s==a[i-].s&&a[i].e==a[i-].e){
tmp[a[i].index]=tmp[a[i-].index];
}
else{
// cout<<i<<" "<<sum(a[i].e)<<endl;
tmp[a[i].index]=sum(a[i].s);
}
add(a[i].s,);
}
for(int i=;i<n;++i){
if(i==n-)
printf("%d\n",tmp[i]);
else
printf("%d ",tmp[i]);
}
}
int main()
{
while(~scanf("%d",&n)){
if(n==)break;
for(int i=;i<n;++i){
scanf("%d%d",&a[i].s,&a[i].e);
a[i].s++;
a[i].e++;
a[i].index=i;
}
solve();
}
return ;
}