您的位置:首页 > 其它

poj 2481 Cows 树状数组

2015-08-08 21:22 288 查看
Cows

Time Limit: 3000MSMemory Limit: 65536K
Total Submissions: 14430Accepted: 4791
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!
Input

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.
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 cowi.

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.
Source
POJ Contest,Author:Mathematica@ZSU
给出n个非空区间,求每个区间是多少个区间的真子集。
既然是求每个区间被多少区间包含,那么可以先把大区间放上去,然后放小的区间,每次考虑到一个区间只用考虑有多少个在他前面的区间包含了它。
一个区间[le,ri],首先给区间排序,ri大的在前面,(这样后的区间一定不能包含前面的区间),如果ri相等,le小的在前面。
树状数组的c[i]表示 =i的区间有多少个,每增加一个值x,c[x]++;查询的时候查询sum[x],即c[1],c[2],c[3]...c[x]的和,即1,2,3,,,x中的值一共达到了多少次(即有多少区间满足)。

<span style="font-size:18px;">#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<climits>
#include<queue>
#include<vector>
#include<map>
#include<sstream>
#include<set>
#include<stack>
#include<utility>
//#pragma comment(linker, "/STACK:102400000,102400000")
#define PI 3.1415926535897932384626
#define eps 1e-10
#define sqr(x) ((x)*(x))
#define FOR0(i,n)  for(int i=0 ;i<(n) ;i++)
#define FOR1(i,n)  for(int i=1 ;i<=(n) ;i++)
#define FORD(i,n)  for(int i=(n) ;i>=0 ;i--)
#define  lson   num<<1,le,mid
#define rson    num<<1|1,mid+1,ri
#define MID   int mid=(le+ri)>>1
#define zero(x)((x>0? x:-x)<1e-15)

using namespace std;
const int INF =0x3f3f3f3f;
//const int maxn=    ;
//const int maxm=    ;
//const int INF=    ;
//typedef long long ll;
//ifstream fin("input.txt");
//ofstream fout("output.txt");
//fin.close();
//fout.close();
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
const int maxn=100000+20;
int c[maxn];
int n;
struct kk
{
    int le,ri,index;
} a[maxn] ;
int ans[maxn];
bool cmp(kk a,kk b)
{
    if(a.ri!=b.ri)
        return a.ri>b.ri;
    return a.le<b.le;
}
inline int lowerbit(int x)
{
    return x&(-x);
}

void add(int x,int val)
{
    while(x<=n)
    {
        c[x]+=val;
        x+=lowerbit(x);
    }
}

int sum(int x)
{
    int s=0;
    while(x>0)
    {
        s+=c[x];
        x-=lowerbit(x);

    }
    return s;
}
int main()
{
    int T;
    while(~scanf("%d",& n )&&n)
    {
        FOR0(i,n)
        {
              scanf("%d%d",&a[i].le,&a[i].ri );
              a[i].index=i;
               a[i].le++,a[i].ri++;
        }

        memset(c,0,sizeof c);
        sort(a,a+n,cmp);
        ans[a[0].index]=0;
        add(a[0].le ,1);

        for(int i=1;i<n;i++)
        {
            if(a[i].le==a[i-1].le&&a[i].ri==a[i-1].ri)
            {
               ans[ a[i].index ]=ans[ a[i-1].index       ];
            }
            else
            ans[a[i].index]=sum(a[i].le);

            add(a[i].le,1);
        }
        for(int i=0;i<n;i++)
        {
            if(i==0)  printf("%d",ans[i]);
            else printf(" %d",ans[i]);
        }
        putchar('\n');

    }

    return 0;
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: