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 4772 Zhuge Liang's Password (矩阵旋转)
    POJ 1141 Brackets Sequence(区间DP)
    POJ 2531 Network Saboteur (DFS)
    HDU 2680 Choose the best route (最短路)
    HDU 1285 确定比赛名次 (预处理+拓扑排序)
    HDU 4540 威威猫系列故事——打地鼠 (DP)
    HDU 2899 Strange fuction (二分)
    HDU 3485 Count 101(DP)
    codeforces 510c (拓扑排序)
    codeforces 510B Fox And Two Dots(dfs)
  • 原文地址:https://www.cnblogs.com/KirinSB/p/9409100.html
Copyright © 2011-2022 走看看