poj2481之线段树单点更新
来源:互联网 发布:云计算 季新华 编辑:程序博客网 时间:2024/05/03 03:31
Cows
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9813 Accepted: 3214
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
Time Limit: 3000MS Memory Limit: 65536K
Total Submissions: 9813 Accepted: 3214
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
/*思路:对所有牛按先左端点s从小到大排序,如果s相等则按e从大到小排序每次更新e点,更新的目的是使得包含e点的区间含有点的个数+1对于比牛[s,e]强壮的牛的个数则只要查询区间[e,MAX]内的点的个数(由于前面更新的牛s<=当前的牛的s,所以只要前面的牛的e>=当前的牛的e即可,即求[e,MAX]含有的点的个数) */#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<string>#include<queue>#include<algorithm>#include<map>#include<iomanip>#define INF 99999999using namespace std;const int MAX=100000+10;int sum[MAX<<2];//区间内含有点的个数int num[MAX];//记录比第i只牛壮的数量 struct node{int s,e,ID;node(){}node(int S,int E,int pos):s(S),e(E),ID(pos){}bool operator <(const node &a)const {//比较函数 if(s == a.s)return e>a.e;return s<a.s; }}s[MAX];void Update(int pos,int n,int left,int right){++sum[n];if(left == right)return;int mid=left+right>>1;if(pos<=mid)Update(pos,n<<1,left,mid);else Update(pos,n<<1|1,mid+1,right);}int Query(int L,int R,int n,int left,int right){if(L<=left && right<=R)return sum[n];int mid=left+right>>1,ans=0;if(L<=mid)ans+=Query(L,R,n<<1,left,mid);if(R>mid)ans+=Query(L,R,n<<1|1,mid+1,right);return ans;}int main(){int n,a,b;while(cin>>n,n){for(int i=1;i<=n;++i){scanf("%d%d",&a,&b);s[i]=node(a,b,i);}sort(s+1,s+n+1);memset(sum,0,sizeof sum);for(int i=1;i<=n;++i){if(s[i].s == s[i-1].s && s[i].e == s[i-1].e){num[s[i].ID]=num[s[i-1].ID];}else num[s[i].ID]=Query(s[i].e,MAX,1,1,MAX);Update(s[i].e,1,1,MAX);}for(int i=1;i<=n;++i)printf("%d%c",num[i],i == n?'\n':' ');}return 0;}
- poj2481之线段树单点更新
- poj2481(线段树单点更新)
- POJ2481:Cows(线段树单点更新)
- poj2481 cows(线段树单点更新)
- poj2481(单点更新)
- Billboard----HDU_2795----线段树之单点更新
- hdu1541/poj2352 线段树之单点更新
- 数据结构之线段树 单点更新
- poj2828之线段树单点更新
- hdu2886之线段树单点更新
- hdu4288之线段树单点更新
- hdu4521之线段树单点更新
- 线段树之单点更新,区域求和
- 线段树之单点更新求和hdoj1166
- HDU2795线段树之单点更新
- 线段树 单点更新
- 线段树单点更新
- 线段树 单点更新
- 关于Spring3.2+Hibernate4.1整合问题:No Session found for current thread
- Linux下多线程基础编程实例(1)
- git上两种工作模式
- 谈谈Google Polymer以及Web UI框架的未来
- 第31章、删除一条记录
- poj2481之线段树单点更新
- 第32章、更新一条记录
- speed tree技术分析
- Android开发基础Android之handler
- 新式骗子,利用提供网站服务进行招摇撞骗
- 第33章、填充三种常见列表
- hql Criteria.add 添加条件 order by
- java反射详解
- 第34章、数据控件GridView