zoukankan      html  css  js  c++  java
  • poj 2481 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

    思路:

    树状数组还能做这个orz

    题意大概就是给你每头牛代表的区间[a,b],要求给出:比第i头牛区间大的牛的头数。

    首先我们有每头牛的区间[ai,bi],我们先按照bi降序排列,这样,我们能保证每头牛前面的牛的b都大于等于他,我们再按照ai升序排列,这样我们就能得通过比较a来得到答案。

    代码:

    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<queue>
    #include<cmath>
    #include<string>
    #include<stack> 
    #include<set>
    #include<map>
    #include<vector>
    #include<iostream>
    #include<algorithm>
    #include<sstream>
    #define ll long long 
    const int N=1e5+10;
    const int INF=1e9;
    using namespace std;
    int n,cnt;
    struct node{
    	int s,e;
    	int id;
    }cow[N];
    int a[N],ans[N];
    
    int lowbit(int x){
    	return x&(-x);
    }
    void update(int x){
    	for(int i=x;i<N;i+=lowbit(i)){
    		a[i]++;
    	}
    }
    int sum(int x){
    	int count=0;
    	for(int i=x;i>=1;i-=lowbit(i)){
    		count+=a[i];
    	}
    	return count;
    }
    
    int cmp(node a,node b){
    	if(a.e>b.e) return 1;
    	else if(a.e==b.e && a.s<b.s) return 1;
    	return 0;
    }
    
    int main(){
    	while(scanf("%d",&n) && n){    //加~就WA
    		memset(a,0,sizeof(a));
    		for(int i=1;i<=n;i++){
    			scanf("%d%d",&cow[i].s,&cow[i].e);
    			cow[i].s++;
    			cow[i].e++;
    			cow[i].id=i;
    		}
    		sort(cow+1,cow+n+1,cmp);
    		for(int i=1;i<=n;i++){
    			if(cow[i].e==cow[i-1].e && cow[i].s==cow[i-1].s){
    				ans[cow[i].id]=ans[cow[i-1].id];
    			}
    			else{
    				ans[cow[i].id]=sum(cow[i].s);
    			}
    			update(cow[i].s);
    		}
    		for(int i=1;i<=n;i++){
    			printf("%d ",ans[i]);
    		}
    		printf("
    ");
    	}
        return 0;  
    }  

  • 相关阅读:
    HDU 3565 Bi-peak Number(数位DP)题解
    FJNU Fang G and his Friends(状压DP)题解
    newcoder 小A的柱状图(单调栈)题解
    CodeForces 518E Arthur and Questions(贪心 + 思维)题解
    装饰器来激活生成器
    迭代器(Iterator)和生成器(generator)浅析
    简单的获取网页样式元素(装饰器实现)
    多层装饰器的调用及执行顺序
    三角形的输出
    简单的用户登录(文件处理)
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9409100.html
Copyright © 2011-2022 走看看