码迷,mamicode.com
首页 > 编程语言 > 详细

Cows(树状数组)

时间:2019-08-27 22:52:22      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:hide   sort   limit   algorithm   script   fir   hint   bool   oid   

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("\n");
31     }
32     return 0;
33 }
View Code

 

Cows(树状数组)

标签:hide   sort   limit   algorithm   script   fir   hint   bool   oid   

原文地址:https://www.cnblogs.com/qq-1585047819/p/11420948.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!