poj 2481 Cows(树状数组)

Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 14380   Accepted: 4769

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

[Submit]   [Go Back]   [Status]   [Discuss]

 和 star那道题差不多。

需要注意的是star那道题读入的时候已经排好了序,而这道题没有排序

由于答案是按照下表输出,排序的时候下标会被打乱,所以要存一下下表到结构体里。

另一个区别是,star那道题星星不会重复,就是说一个位置不会有多颗星星。

而这道题却可能,而且题目中说,如果区间的长度差为0(就是后面的那个不等式),那么不计数。

因为区间下标可能为0,但是树状数组的下表必须从1开始,所以下表要+1,但是由于下表可能有所重复,所以 不能用++,而是用+1

不然会增加多次(我就是这么wa了两次。。。)

 

/*************************************************************************
    > File Name: code/poj/2481.cpp
    > Author: 111qqz
    > Email: rkz2013@126.com 
    > Created Time: 2015年08月04日 星期二 02时06分05秒
 ************************************************************************/

#include<iostream>
#include<iomanip>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<vector>
#include<stack>
#define y0 abc111qqz
#define y1 hust111qqz
#define yn hez111qqz
#define j1 cute111qqz
#define tm crazy111qqz
#define lr dying111qqz
using namespace std;
#define REP(i, n) for (int i=0;i<int(n);++i)  
typedef long long LL;
typedef unsigned long long ULL;
const int inf = 0x7fffffff;
const int N=1E5+7;
int n;
int a[N],t[N];
struct Q
{
    int s,e,id;
}q[N];
bool cmp (Q a,Q b)
{
    if (a.e>b.e) return true;
    if (a.e==b.e&&a.s<b.s) return true;
    return false;
}

int lowbit(int x)
{
    return x&(-x);
}
void update ( int x,int c)
{
    for ( int i = x ; i <  N ; i = i + lowbit(i))
    {
    t[i] = t[i] + c;
    }
}
int Sum ( int x)
{
    int res = 0;
    for (  int i = x ; i >= 1 ; i = i - lowbit(i))
    {
    res = res + t[i];
    }
    return res;
}
int main()
{
    while (~scanf("%d",&n)&&n)
    {
    memset(t,0,sizeof(t));
    memset(a,0,sizeof(a));

    for ( int i = 0 ; i  < n; i ++ )
    {
        scanf("%d %d",&q[i].s,&q[i].e);
        q[i].id = i;
    }
    sort(q,q+n,cmp);
    a[q[0].id] = 0; //0是最强的...
    update(q[0].s+1,1);
    for ( int i = 1 ; i < n ;  i++)
    {
        if (q[i].e==q[i-1].e&&q[i].s==q[i-1].s)
        {
        a[q[i].id]=a[q[i-1].id];  // 完全一样,区间长度为0,不计数。
        }
        else
        a[q[i].id] = Sum (q[i].s+1);  //一开始这里写成了 Sum(++q[i].s),然后下面update 写成(q[i].s,1)
                        //结果wa了好多次。因为有重复的,
                       // 就会++多次,导致相同的区间下标不同。差评!
        update(q[i].s+1,1);
    }
    for ( int i = 0 ; i < n-1 ; i++ )
        cout<<a[i]<<" ";
        cout<<a[n-1]<<endl;
    }
      return 0;
}

 

posted @ 2015-08-04 03:32  111qqz  阅读(202)  评论(0编辑  收藏  举报