poj 2481 Cows (树状数组)
来源:互联网 发布:星期四数码靠谱吗淘宝 编辑:程序博客网 时间:2024/06/03 21:25
Cows
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 19677 Accepted: 6702
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.
Source
POJ Contest,Author:Mathematica@ZSU
思路:将数据先按照E的降序再按S的升序排,然后用树状数组查询。注意处理0的数据以及重点。
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#define MAX_N 100005using namespace std;int n,tree[MAX_N],sum[MAX_N];struct node{ int s,e,id,cnt,les;}cows[MAX_N];int lowbit(int i){ return i&(-i);}void update(int i,int x){ while(i<=MAX_N) { tree[i]+=x; i+=lowbit(i); }}int query(int n){ int sum=0; while(n>0) { sum+=tree[n]; n-=lowbit(n); } return sum;}bool cmp(node a,node b){ if(a.e!=b.e) return a.e>b.e; return a.s<b.s;}bool cmp2(node a,node b){ return a.id<b.id;}int main(){ while(~scanf("%d",&n)&&n) { memset(tree,0,sizeof(tree)); memset(sum,0,sizeof(sum)); int x=0,y=0,sum=0; for(int i=1;i<=n;i++) { scanf("%d%d",&cows[i].s,&cows[i].e); cows[i].s++,cows[i].e++,cows[i].id=i; } sort(cows+1,cows+1+n,cmp); for(int i=1;i<=n;i++) { if(x==cows[i].s&&y==cows[i].e)//处理重点的数据 { sum++; cows[i].les=sum; } else { sum=0,x=cows[i].s,y=cows[i].e; cows[i].les=sum; } update(cows[i].s,1); cows[i].cnt=query(cows[i].s)-1-cows[i].les;//les是重点的个数 } sort(cows+1,cows+1+n,cmp2); for(int i=1;i<=n;i++) { if(i==1) printf("%d",cows[i].cnt); else printf(" %d",cows[i].cnt); } printf("\n"); } return 0;}
阅读全文
0 0
- POJ 2481 Cows 树状数组
- POJ 2481 Cows【树状数组】
- POJ 2481Cows 树状数组
- POJ 2481 Cows 树状数组
- POJ 2481 Cows (树状数组)
- POJ 2481 Cows 树状数组
- poj 2481 - Cows(树状数组)
- 树状数组 POJ 2481 Cows
- POJ(2481)Cows 树状数组
- POJ 2481 Cows(树状数组)
- POJ-2481 Cows 树状数组
- Cows - POJ 2481 树状数组
- Poj 2481 Cows 树状数组
- POJ - 2481 Cows(树状数组)
- poj 2481 Cows(树状数组)
- POJ 2481 Cows(树状数组)
- poj 2481 Cows 树状数组
- poj 2481 Cows 树状数组
- 如何進入“強制禁止驅動簽章”模式
- Linux在启动时出现Intel VT-x被禁用的问题
- node-npm安全性插件helmet(防护包含点击劫持、xss、嗅探攻击...)
- bzoj 3680 吊打xxx 模拟退火
- 01背包问题以及网易考题
- poj 2481 Cows (树状数组)
- 3767: A+B Problem加强版
- JDBC基础操作
- 网络的概念
- uboot移植2
- 2016年湖南省第十二届大学生计算机程序设计竞赛
- 【启发】leetcode
- hdu 2147 kiki's game
- [数位DP]Hdu 2089——不要62