poj2481Cows【树状数组】
来源:互联网 发布:最优化理论 推荐教材 编辑:程序博客网 时间:2024/05/17 08:05
Cows
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 15446 Accepted: 5151
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!
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.
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
31 20 33 40
Sample Output
1 0 0
Hint
Huge input and output,scanf and printf is recommended.
#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>#include<list>#include<queue>#include<vector>using namespace std;const int maxn=100010;struct Node{int s,e,id;}A[maxn];int c[maxn];int low(int x){return x&(-x);}bool cmp(Node a,Node b){//按照e降序排列相等按照s的升序排列 if(a.e==b.e)return a.s<b.s;return a.e>b.e;}int sum(int n){int value=0;while(n>0){value+=c[n];n-=low(n);}return value;}void add(int pos){while(pos<maxn){c[pos]+=1;pos+=low(pos);}}int ans[maxn];int main(){int n,i,j,k;while(scanf("%d",&n),n){for(i=0;i<n;++i){scanf("%d%d",&A[i].s,&A[i].e);A[i].s+=1;A[i].e+=1;A[i].id=i;}sort(A,A+n,cmp);memset(c,0,sizeof(c));for(i=0;i<n;++i){if(i>0&&A[i].s==A[i-1].s&&A[i].e==A[i-1].e){ans[A[i].id]=ans[A[i-1].id];}else {ans[A[i].id]=sum(A[i].s);}add(A[i].s);}for(i=0;i<n;++i){printf(i==n-1?"%d\n":"%d ",ans[i]);}}return 0;}
0 0
- 树状数组poj2481cows
- poj2481Cows【树状数组】
- POJ2481Cows(树状数组)
- poj2481Cows
- poj2481Cows
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- 树状数组
- HDU 5620 KK's Steel(斐波那契数列的巧妙应用)
- 【运动传感器】Madgwick算法(上)
- 设计模式之建造者模式
- MySQLi基于面向过程的编程
- UVA 1593
- poj2481Cows【树状数组】
- CodeForces 294A Shaass and Oskols
- MySQLi基于面向对象的编程
- UVA 1594
- MySQLi的高级应用
- no system images installed for target 问题的解决办法
- 函数指针 虚函数遐思
- hdoj5489Removed Interval【lis】
- UVA 10935