poj 2481 Cows[求逆序数]

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!

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

Analyse:

s从小到大排,e从大到小。然后依次modify,求>=c[i].e的sum;

注意几个坑点:

3

1 3

1 3

1 3

1 2

0 0 0 1

3

1 2

1 2

1 2

0 0 0

注意!!!!坑死!

CODE:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<string>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#define INF 0x7fffffff
#define SUP 0x80000000
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;

typedef long long LL;
const int N=100007;

struct Tree{
    int le,ri;
    int sum;
}t[N<<2];

struct Cow{
    int s,e,id;
    bool operator <(Cow tt) const{
        if(s==tt.s) return e>tt.e;
        return s<tt.s;
    }
}c[N];

int ans[N];

void build(int ro,int le,int ri)
{
    t[ro].le=le;
    t[ro].ri=ri;
    t[ro].sum=0;
    if(le==ri) return;
    int mid=le+(ri-le)/2;

    build(ro<<1,le,mid);
    build(ro<<1|1,mid+1,ri);
}

void modify(int ro,int le,int ri,int x)
{
    if(le==ri){
        t[ro].sum+=1;
        return;
    }
    int mid=le+(ri-le)/2;
    if(x<=mid) modify(ro<<1,le,mid,x);
    else modify(ro<<1|1,mid+1,ri,x);

    t[ro].sum=t[ro<<1].sum+t[ro<<1|1].sum;
}

int query(int ro,int le,int ri,int L,int R)
{
    if(L<=le&&ri<=R){
        return t[ro].sum;
    }
    int mid=le+(ri-le)/2;
    int ret=0;
    if(L<=mid) ret+=query(ro<<1,le,mid,L,R);
    if(R>mid) ret+=query(ro<<1|1,mid+1,ri,L,R);
    return ret;
}

int main()
{
    int n;
    while(scanf("%d",&n)==1,n){
        int maxx=0;
        for(int i=0;i<n;i++){
            scanf("%d%d",&c[i].s,&c[i].e);
            c[i].id=i;
            maxx=max(c[i].e,maxx);
        }
        sort(c,c+n);
        build(1,0,maxx+10);
        modify(1,0,maxx+10,c[0].e);
        ans[c[0].id]=0;
        for(int i=1;i<n;i++)
        {
            if(c[i].s==c[i-1].s&&c[i].e==c[i-1].e){
                ans[c[i].id]=ans[c[i-1].id];
                modify(1,0,maxx+10,c[i].e);
            }
            else{
                ans[c[i].id]=query(1,0,maxx+10,c[i].e,maxx+10);
                modify(1,0,maxx+10,c[i].e);
            }
        }
        for(int i=0;i<n;i++)
            printf("%d%c",ans[i],i<n-1?' ':'\n');
    }
    return 0;
}
时间: 2024-08-24 10:33:24

poj 2481 Cows[求逆序数]的相关文章

POJ 2299 Ultra-QuickSort (求逆序数:离散化+树状数组或者归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 55048   Accepted: 20256 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 2299 Ultra-QuickSort 求逆序数,树状数组解法,详细解析

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 44554   Accepted: 16195 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 2299 Ultra-QuickSort 归并排序求逆序数对

题目链接: http://poj.org/problem?id=2299 题目描述: 给一个有n(n<=500000)个数的杂乱序列,问:如果用冒泡排序,把这n个数排成升序,需要交换几次? 解题思路: 根据冒泡排序的特点,我们可知,本题只需要统计每一个数的逆序数(如果有i<j,存在a[i] > a[j],则称a[i]与 a[j]为逆序数对),输出所有的数的逆序数的和用普通排序一定会超时,但是比较快的排序,像快排又无法统计 交换次数,这里就很好地体现了归并排序的优点.典型的利用归并排序求逆

POJ训练计划2299_Ultra-QuickSort(归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 39279   Accepted: 14163 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 2299 树状数组求逆序数+离散化

http://poj.org/problem?id=2299 最初做离散化的时候没太确定但是写完发现对的---因为后缀数组学的时候,,这种思维习惯了吧 1.初始化as[i]=i:对as数组按照num[]的大小间接排序 2.bs[as[i]]=i:现在bs数组就是num[]数组的离散化后的结果 3.注意,树状数组中lowbit(i)  i是不可以为0的,0&(-0)=0,死循环... #include <cstdio> #include <cstring> #include

POJ 2299 Ultra-QuickSort (归并排序求逆序数)

Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 47235   Accepted: 17258 Description In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swappin

poj 1007 DNA Sorting (求逆序数)

DNA Sorting Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 83069   Accepted: 33428 Description One measure of ``unsortedness'' in a sequence is the number of pairs of entries that are out of order with respect to each other. For instanc

POJ 2299 Ultra-QuickSort (树状数组+离散化 求逆序数)

In this problem, you have to analyze a particular sorting algorithm. The algorithm processes a sequence of n distinct integers by swapping two adjacent sequence elements until the sequence is sorted in ascending order. For the input sequence 9 1 0 5

POJ 2481 Cows &amp;&amp; POJ 2352 Stars(树状数组妙用)

题目链接:POJ 2481 Cows POJ 2352 Stars 发现这两个题目都跟求逆序数有着异曲同工之妙,通过向树状数组中插入点的位置,赋值为1,或者++,然后通过求和来判断比当前 点 "小" 的有多少点. Cows需要自己排序, Stars题目已经给排好序. POJ 2352 Stars 题目大意为在二维坐标上给出一些星星的坐标,求某一个星星左方,下方,左下方的星星个数.题目已经把星星按照Y坐标从小到大,X从小到大排序.因此,在每次对一个星星进行统计时,之前出现过的星星,只要X