zoukankan      html  css  js  c++  java
  • poj2481

    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 10785   Accepted: 3572

    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
    
     1 #include <stdio.h>
     2 #include <string.h>
     3 #include <math.h>
     4 #include <algorithm>
     5 #include <iomanip>
     6 #include <set>
     7 #include <map>
     8 #include <vector>
     9 #include <queue>
    10 #define llt long long int
    11 #define ml(x, y) ((x + y) >> 1)
    12 #define mr(x, y) (((x + y) >> 1) + 1)
    13 #define pl(p) (p << 1)
    14 #define pr(p) ((p << 1) | 1)
    15 #define N 100005
    16 using namespace std;
    17 struct node
    18 {
    19     int s, e, pos;
    20     bool operator < (const node& a)const//按照先左边小,再右边大进行排序
    21     {
    22         return s == a.s ? e > a.e : s < a.s;
    23     }
    24 }a[N];
    25 int segtree[N << 2], ans[N];
    26 void build(int l, int r, int p)
    27 {
    28     segtree[p] = 0;
    29     if (l < r)
    30     {
    31         build(l, ml(l, r), pl(p));
    32         build(mr(l, r), r, pr(p));
    33     }
    34 }
    35 void update(int l, int r, int p, int v)
    36 {
    37     segtree[p]++;
    38     if (l != r)
    39         if (ml(l, r) >= v)
    40             update(l, ml(l, r), pl(p), v);
    41         else
    42             update(mr(l, r), r, pr(p), v);
    43 }
    44 int query(int l, int r, int p, int v)//查询区间[l, imax]的个数
    45 {
    46     if (v <= l)
    47         return segtree[p];
    48     int ans = 0;
    49     if (ml(l, r) >= v)//等于[l,mid]+[mid+1,imax] 
    50         ans = segtree[pr(p)] + query(l, ml(l, r), pl(p), v);
    51     else//[mid+1,imax] 
    52         ans = query(mr(l, r), r, pr(p), v);
    53     return ans;
    54 }
    55 int main()
    56 {
    57     int n, i, maxe;//maxe记录一个最大值
    58     while (scanf("%d", &n), n)
    59     {
    60         maxe = 0;
    61         for (i = 0; i < n; i++)
    62         {
    63             scanf("%d%d", &a[i].s, &a[i].e);
    64             a[i].pos = i;
    65             if (maxe < a[i].e)
    66                 maxe = a[i].e;
    67         }
    68         sort(a, a + n);//排序 
    69         build(0, maxe, 1);
    70         for (i = 0; i < n; i++)
    71         {
    72             if (i != 0 && a[i].e == a[i - 1].e && a[i].s == a[i - 1].s)//处理重复的
    73                 ans[a[i].pos] = ans[a[i - 1].pos];//
    74             else
    75                 ans[a[i].pos] = query(0, maxe, 1, a[i].e);//查询只需要得要a[i].e到imax有多少个数就行了
    76             update(0, maxe, 1, a[i].e);//因为s小的先处理,所以只需要根据e来更新,记录该区间e的个数
    77         }
    78         printf("%d", ans[0]);
    79         for (i = 1; i < n; i++)
    80             printf(" %d", ans[i]);
    81         putchar('
    ');
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    C++中的虚函数、重写与多态
    STL中_Rb_tree的探索
    C++11 新用法
    FJUTOJ-3682 LRU算法的实现2 (链表+哈希)
    Windows系统对拍程序
    FOJ-2013 A Short Problem (前缀和)
    HDU-2036 改革春风吹满地 (数学)
    Ubuntu下PostgreSQL的安装和简单操作
    Ubuntu,Linux下goldendict词典安装及配置
    搜狗输入法输入汉字时候选栏乱码
  • 原文地址:https://www.cnblogs.com/jecyhw/p/3454837.html
Copyright © 2011-2022 走看看