首页 > 代码库 > POJ2481 Cows

POJ2481 Cows

题解:

给出n个区间,求每个区间是多少个区间的真子集

按照x从小到大排序,若x相同,则按照y从大到小排序。

然后每次需要判断一下x和y相同的个数,减去就行了

代码:

#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>#include<map>using namespace std;#define pb push_back#define mp make_pair#define se second#define fs first#define LL long long#define CLR(x) memset(x,0,sizeof x)#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1 typedef pair<int,int> P;const double eps=1e-9;const int maxn=100100;const int mod=1e9+7;const int INF=1e9;struct node{    int s,e,id;}p[maxn];int c[maxn],a[maxn];bool cmp(node a,node b){    if(a.s==b.s) return a.e>b.e;    return a.s<b.s;}int lowbit(int x){return x&-x;}void Add(int x,int v){   while(x<=maxn){        c[x]+=v;        x+=lowbit(x);   }}int Sum(int x){    int sum=0;    while(x){        sum+=c[x];        x-=lowbit(x);    }    return sum;}int main(){    int n;    while(~scanf("%d",&n)&&n){    CLR(c);    for(int i=1;i<=n;i++){        scanf("%d%d",&p[i].s,&p[i].e);        p[i].s++;        p[i].e++;        p[i].id=i;    }    sort(p+1,p+n+1,cmp);    /*    cout<<endl;    cout<<endl;    for(int i=1;i<=n;i++) printf("%d %d %d\n",p[i].s,p[i].e,p[i].id);    */    for(int i=1;i<=n;i++){        int tmp=0,k=i;        while(k>1&&p[k].e==p[k-1].e&&p[k].s==p[k-1].s){            tmp++;            k--;        }         a[p[i].id]=Sum(maxn)-Sum(p[i].e-1)-tmp;        Add(p[i].e,1);    }    for(int i=1;i<=n;i++) printf("%d%c",a[i],(i==n?\n: ));    }}

 

POJ2481 Cows