zoukankan      html  css  js  c++  java
  • POJ 2418 Cows (树状数组)

    Cows
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 9618   Accepted: 3153

    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
     
     

    题目大意:给你很多线段的头S和尾E,问每一条线段中包含了多少个线段,(S和E相同不计在内)。这题先一看,完全不知道什么方法,感觉非常的难办。

    但是!树状数组可以轻松解决这个问题!!!首先,将她们线段的s和e当做是(s,e)一个点,这样子把所有点画出来,你就会发现一个很神奇的现象,题目要求就会变成:问每一个点的左上角有多少个点?

          !!!这样不就和那题最简单的stars一样吗???!!!

              stars那题是问左下角有多少个点,而这题是问左上角,而且点不是有序排好的,所以有些不同,特殊处理一下就可以。

           如果正常做,那个y是递增的,所以sum和update那个方向就会相反了,这个其实没什么所谓,一样的,排序的时候先y由大到小排,y相同时x由小到大排,这样小小的处理,就变成stars那题了!!!

           还有一点忘了,这题也是需要离散化的,离散化很重要很强大!!!

     
     
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    
    using namespace std;
    
    const int N=100010;
    
    struct node{
        int s,e;
        int id;
    }a[N];
    
    int n,level[N],arr[N];
    
    int cmp(node a,node b){
        if(a.e==b.e)
            return a.s<b.s;
        return a.e>b.e;
    }
    
    int lowbit(int x){
        return x&(-x);
    }
    
    void update(int i,int val){
        while(i<=n){
            arr[i]+=val;
            i+=lowbit(i);
        }
    }
    
    int Sum(int i){
        int ans=0;
        while(i>0){
            ans+=arr[i];
            i-=lowbit(i);
        }
        return ans;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        while(~scanf("%d",&n) && n){
            memset(level,0,sizeof(level));
            memset(arr,0,sizeof(arr));
            for(int i=1;i<=n;i++){
                scanf("%d%d",&a[i].s,&a[i].e);
                a[i].s++;   a[i].e++;
                a[i].id=i;
            }
            sort(a+1,a+n+1,cmp);
            level[a[1].id]=Sum(a[1].s);
            update(a[1].s,1);
            for(int i=2;i<=n;i++){
                if(a[i].s==a[i-1].s && a[i].e==a[i-1].e)
                    level[a[i].id]=level[a[i-1].id];
                else
                    level[a[i].id]=Sum(a[i].s);
                update(a[i].s,1);
            }
            for(int i=1;i<n;i++)
                printf("%d ",level[i]);
            printf("%d\n",level[n]);
        }
        return 0;
    }
  • 相关阅读:
    GitHub上受欢迎的Android UI Library
    推荐的优秀博客--多关注,多看看
    Android SDK在线更新镜像服务器大全
    SqlServer查看各个表所占空间大小的sql
    演化理解 Android 异步加载图片
    tsp、rtmp测试地址
    eclipse离线安装Properties Editor插件查看配置文件中Unicode内容
    亲测成功关闭谷歌浏览器自动更新方法分享 取消chrome自动更新
    SQL Server 中用While循环替代游标(Cursor的解决方案
    【转】service和serviceImpl的选择
  • 原文地址:https://www.cnblogs.com/jackge/p/3039829.html
Copyright © 2011-2022 走看看