• Cows(树状数组)


    Cows
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 24787   Accepted: 8296

    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
     
    解题思路 : 先按照E从大到小排序(满足一个条件) 如果相等再按照S从小到大排序 特别注意相等的情况 题目中这句话Ei - Si > Ej - Sj(代表完全相等的情况下不算比他强的!)
     1 #include <iostream>
     2 #include <algorithm>
     3 #include <cstring>
     4 #include <cstdio>
     5 using namespace std;
     6 int n,x,y;
     7 const int maxn=1e5+5;
     8 int cnt[maxn],res[maxn];
     9 struct Node{ int x,y,id; }A[maxn];
    10 bool cmp(Node a,Node b){ if(a.y!=b.y) return a.y>b.y; return a.x<b.x; }
    11 int lowbits(int x) { return x&-x; }
    12 void add(int x){ while(x<=maxn) cnt[x]++,x+=lowbits(x); }
    13 int query(int x){ int sum=0;while(x>0) sum+=cnt[x],x-=lowbits(x); return sum; }
    14 
    15 int main(){
    16     while(scanf("%d",&n),n){
    17         memset(cnt,0,sizeof(cnt)),memset(res,0,sizeof(res));
    18         for(int i=1;i<=n;i++) scanf("%d%d",&A[i].x,&A[i].y),A[i].id=i;
    19         sort(A+1,A+1+n,cmp);
    20         res[A[1].id]=0;  add(A[1].x+1);
    21         for(int i=2;i<=n;i++){
    22             if(A[i].x==A[i-1].x&&A[i].y==A[i-1].y) res[A[i].id]=res[A[i-1].id];
    23             else res[A[i].id]=query(A[i].x+1);
    24             add(A[i].x+1);
    25         }
    26         for(int i=1;i<=n;i++){
    27             if(i!=1) printf(" ");
    28             printf("%d",res[i]);
    29         }
    30         printf("
    ");
    31     }
    32     return 0;
    33 }
    View Code
  • 相关阅读:
    EasyFlash 的初始化配置
    不能靠眼睛之 KEIL 中失效代码灰暗特性
    C 头文件、宏、编译问题
    C++ 中 const、volatile、mutable的用法
    【转】C++ const 关键字总结
    你想要的成都全攻略,好耍不重样——成都胖娃呕心巨作
    【转】RO段、RW段和ZI段 --Image$$??$$Limit 含义(zz)
    深有体会内存对系统性能的重要性
    毕业论文编写笔记
    (二)基于商品属性的相似商品推荐算法——Flink SQL实时计算实现商品的隐式评分
  • 原文地址:https://www.cnblogs.com/qq-1585047819/p/11420948.html
Copyright © 2020-2023  润新知