poj2481 树状数组
来源:互联网 发布:java分割字符串 编辑:程序博客网 时间:2024/06/05 15:12
http://poj.org/problem?id=2481
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 <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#define MAX 100005using namespace std;int c[MAX],ans[MAX],n,imax;struct cow{ int l,r,id;} a[MAX];bool cmp(cow a,cow b){ if(a.r==b.r) return a.l<b.l; return a.r>b.r;}int lowbit(int t){ return t&(-t);}int sum(int t){ int total=0; while(t>0) { total+=c[t]; t-=lowbit(t); } return total;}void modify(int posi,int key){ while(posi<=imax) { c[posi]+=key; posi+=lowbit(posi); }}int main(){ int i,j,k,n; while(scanf("%d",&n),n) { memset(c,0,sizeof(c)); imax=0; for(i=1; i<=n; i++) { scanf("%d%d",&a[i].l,&a[i].r); a[i].id=i; ++a[i].l;//树状数组是从1开始的,这样做能把所有的数都增大1,排除了零的干扰 ++a[i].r; if(imax<a[i].l) imax=a[i].l; } sort(a+1,a+n+1,cmp); for(i=1; i<=n; i++) { if(i==1) { ans[a[i].id]=sum(a[i].l); modify(a[i].l,1); } else { if(a[i].l==a[i-1].l&&a[i].r==a[i-1].r) ans[a[i].id]=ans[a[i-1].id]; else ans[a[i].id]=sum(a[i].l); modify(a[i].l,1); } } for(i=1; i<n; i++) printf("%d ",ans[i]); printf("%d\n",ans[i]); } return 0;}
0 0
- poj2481-树状数组
- POJ2481树状数组
- poj2481 树状数组
- poj2481~Cows~(树状数组)
- POJ2481:Cows(树状数组)
- poj2481 树状数组
- poj2481 树状数组
- POJ2481 Cows 树状数组
- poj2481 Cows (树状数组)
- POJ2481->树状数组
- poj2481树状数组
- [树状数组]poj2481 Cows
- 树状数组poj2481
- [POJ2481] cows - 树状数组
- POJ2481 Cows 树状数组
- POJ2481 树状数组+sort
- poj2481-树状数组的应用
- poj2481之排序+树状数组
- 我是新人,好紧张啊
- poj 1159 DP
- 关于dll的一些事
- 当oracle有多个实例时,用pl/sql查看session
- 渣硕笔记——偶读《TCP/IP详解:卷1 协议》 关于IP路由选项和时间戳选项
- poj2481 树状数组
- CentOS-min 操作记录(3)
- STL map常用操作简介
- 进制转换算法
- pjsip 搭建 sip服务器
- Android开源项目SlidingMenu深入剖析
- CListCtrl控件失去焦点时仍保持高亮
- Java中的==和equals区别
- Google Guice使用入门