POJ2481 Cows(线段树 & 树状数组)
2015-09-01 17:30
190 查看
Cows
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 cowiis 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
Sample Output
Hint
Huge input and output,scanf and printf is recommended.
问你对每头牛,有多少头牛比他强壮,i牛比j牛强壮条件是Si <= Sj && Ej <= Ei && Ei - Si > Ej - Sj,也就是i牛可以完全覆盖j牛。
排序时先按e从大到小再按s从小到大,若遇到重复的数据直接存到数组中。
AC代码(线段树):
AC代码(树状数组):
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 14777 | Accepted: 4890 |
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 cowiis 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.
问你对每头牛,有多少头牛比他强壮,i牛比j牛强壮条件是Si <= Sj && Ej <= Ei && Ei - Si > Ej - Sj,也就是i牛可以完全覆盖j牛。
排序时先按e从大到小再按s从小到大,若遇到重复的数据直接存到数组中。
AC代码(线段树):
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" #include "cstdlib" using namespace std; const int MAXN = 1e5 + 5; #define lson rt << 1, l, mid #define rson rt << 1 | 1, mid + 1, r int n, sum[MAXN << 2], ans[MAXN]; struct node { /* data */ int s, e, flag; }a[MAXN]; bool cmp(node a, node b) { if(a.e == b.e) return a.s < b.s; return a.e > b.e; } void pushup(int rt) { sum[rt] = sum[rt << 1] + sum[rt << 1 | 1]; } void update(int rt, int l, int r, int x) { if(l == r) { ++sum[rt]; return; } int mid = (l + r) >> 1; if(x <= mid) update(lson, x); else update(rson, x); pushup(rt); } int query(int rt, int l, int r, int L, int R) { if(L <= l && R >= r) return sum[rt]; int mid = (l + r) >> 1, res = 0; if(L <= mid) res += query(lson, L, R); if(R > mid) res += query(rson, L, R); return res; } int main(int argc, char const *argv[]) { while(scanf("%d", &n) != EOF && n) { memset(sum, 0, sizeof(sum)); for(int i = 0; i < n; ++i) { scanf("%d%d", &a[i].s, &a[i].e); ++a[i].s; a[i].flag = i; } sort(a, a + n, cmp); for(int i = 0; i < n; ++i) { int x = a[i].s; if(i && a[i].s == a[i - 1].s && a[i].e == a[i - 1].e) ans[a[i].flag] = ans[a[i - 1].flag]; else ans[a[i].flag] = query(1, 1, 1e5 + 1, 1, x); update(1, 1, 1e5 + 1, x); } for(int i = 0; i < n - 1; ++i) printf("%d ", ans[i]); printf("%d\n", ans[n - 1]); } return 0; }
AC代码(树状数组):
#include "iostream" #include "cstdio" #include "cstring" #include "algorithm" using namespace std; const int MAXN = 1e5 + 5; int n, c[MAXN], ans[MAXN]; struct node { /* data */ int s, e, flag; }a[MAXN]; bool cmp(node a, node b) { if(a.e == b.e) return a.s < b.s; return a.e > b.e; } int lowbit(int x) { return x & (-x); } void update(int x, int v) { for(int i = x; i <= n; i += lowbit(i)) c[i] += v; } int get_sum(int x) { int sum = 0; for(int i = x; i > 0; i -= lowbit(i)) sum += c[i]; return sum; } int main(int argc, char const *argv[]) { while(scanf("%d", &n) != EOF && n) { memset(c, 0, sizeof(c)); memset(ans, 0, sizeof(ans)); for(int i = 0; i < n; ++i) { scanf("%d%d", &a[i].s, &a[i].e); a[i].flag = i; } sort(a, a + n, cmp); c[0] =1; for(int i = 0; i < n; ++i) { if(i && a[i].s == a[i - 1].s && a[i].e == a[i - 1].e) ans[a[i].flag] = ans[a[i - 1].flag]; else ans[a[i].flag] = get_sum(a[i].s + 1); update(a[i].s + 1, 1); } for(int i = 0; i < n - 1; ++i) printf("%d ", ans[i]); printf("%d\n", ans[n - 1]); } return 0; }
相关文章推荐
- eclipse无法创建server
- EXP-00091 EXP导出错误问题困惑
- 时间
- Linux 初学杂记
- keepalived+lvs FAQ
- 软工之面型对象和面型过程技术
- sobel图像预处理
- 37.百度地图经纬度转换为指定view的屏幕坐标(像素点)
- 38.微信二维码扫描效果--设定扫描区域,周边是半透明--iOS原生二维码扫描
- js性能优化
- BZOJ 3771 Triple FFT+容斥原理
- 在IE中调试Javascript
- fp oo
- source code of MES Data
- IOS开发之 ---- 振动强度和时间(私有api)
- 36.在iPhone4S上,UIButton-setEnabled属性为NO时,无法改变button的状态,如上面的文字
- 40.iOS循环播放雷达扫描动画--Life365中的开启定位
- 基础知识(七)opencv、python、人脸框检测
- 自己写的Dapper通用数据访问层
- hdu 4756 MST+树形dp ****