设为首页 加入收藏

TOP

poj 2481 Cows 树状数组解法,详细解析。
2015-07-20 17:20:08 来源: 作者: 【 】 浏览:3
Tags:poj 2481 Cows 解法 详细 解析
Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 13445 Accepted: 4448

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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.

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 <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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 cow i.

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
莫名的感觉自己越来越笨了,哎,可能是做题少了吧,,以后要疯狂做题,这题的本意就是求一个区间的真子集。首先你得自己画几条长短不一的线,子集推一下 : 1 ------------------ 2 ----------------- 3 --------------------------- 4 --------- 5 ----------- 6 ------------------ 自己琢磨琢磨这就会发现,当这样排列时,问题就变的简单了: 3 --------------------------- 1 ------------------
6 ------------------
5 -----------
2 -----------------
4 ---------

这是按什么样的规则排列的呢? 1,首先按E做降序排列 2,如果E相同,S按升序排列。
为什么这样排列就使问题简单了呢? 因为此时只需考虑S值,如果当前牛的测验值为[s, e],那么比它强壮的牛的个数,就等于排列在它前面的,sum值在[0,s]区间的牛数量(E相等的话为[0,s-1])。怎么求S,就是树状数组的事情了。


#include 
  
   
#include 
   
     #include 
    
      #define MAX 100100 using namespace std ; struct Interval{ int s , e , id; }in[MAX]; int t[MAX] , ans[MAX]; bool cmp(Interval a , Interval b) { if(a.e!=b.e) { return a.e>b.e ; } else { return a.s
     
      0) { count += t[pos] ; pos -= lowbit(pos) ; } return count ; } int main() { int n ; while(scanf("%d",&n) && n) { memset(t,0,sizeof(t)) ; for(int i = 1 ; i <= n ; ++i) { in[i].id = i ; scanf("%d%d",&in[i].s,&in[i].e) ; ++in[i].s,++in[i].e; //这步是考虑到树状数组的 0 陷阱 } sort(in+1,in+n+1,cmp) ; for(int i = 1 ; i <= n ; ++i) { int num ; if(in[i].e == in[i-1].e && in[i].s == in[i-1].s) //注意这步,当区间相同时,比他们强壮的牛应该相等。 num = ans[in[i-1].id] ; else num = query(in[i].s) ; update(in[i].s) ; ans[in[i].id] = num; } for(int i = 1 ; i <= n ; ++i) { printf("%d ",ans[i]) ; } printf("\n") ; } return 0 ; } 
     
    
   
  



】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇poj 2299 Ultra-QuickSort 求逆序.. 下一篇YT14-HDU-盒子与瓷砖

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·【C语言】动态内存管 (2025-12-27 06:23:20)
·C语言中的内存管理 - (2025-12-27 06:23:16)
·C语言指南:C语言内 (2025-12-27 06:23:14)
·Redis on AWS:Elast (2025-12-27 04:19:30)
·在 Spring Boot 项目 (2025-12-27 04:19:27)