POJ 2481 cows 树状数组
2016-07-28 16:46
459 查看
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 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
Sample Output
就是让你比较John's的牛谁比较牛
先对e按照降序排列,每加入一只牛,当前已经加入树状数组的牛的s如果比这只牛小,那么那些牛就更强壮
跟Stars那个题一个类型 把E值按降序排好 就只用考虑S的值了
#include <iostream>
#include <stdio.h>
#include<algorithm>
#include <string.h>
using namespace std;
const int maxn = 100000+20;
struct nodes{
int id;
int x,y;
}N[maxn];
int n;
int a[maxn],ans[maxn];
bool cmp(nodes a,nodes b)
{
if(a.y!=b.y) return a.y>b.y;
else return a.x<b.x;
}
int lowbit(int x)
{
return x&(-x);
}
int cal(int t)//计算1-t的和
{
int sum=0;
while(t>0)
{
sum+=a[t];
t-=lowbit(t);
}
return sum;
}
void add(int x,int v)//在x的位置加上v
{
while(x<=n)
{
a[x]+=v;
x+=lowbit(x);
}
}
int main()
{
int i;
while(~scanf("%d",&n),n)
{
memset (a,0,sizeof(a));
memset (ans,0,sizeof(ans));
for(i=1;i<=n;i++)
{
scanf("%d%d",&N[i].x,&N[i].y);
N[i].id=i;
}
sort(N+1,N+n+1,cmp);
for( i=1;i<=n;i++)
{
if(N[i].x==N[i-1].x&&N[i].y==N[i-1].y)
{
ans[N[i].id]=ans[N[i-1].id];
}
else
{
ans[N[i].id]=cal(N[i].x+1);
}
add(N[i].x+1,1);
}
for(i=1;i<=n;i++)
{
if(i==n) printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
}
}
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 16489 | Accepted: 5504 |
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
就是让你比较John's的牛谁比较牛
先对e按照降序排列,每加入一只牛,当前已经加入树状数组的牛的s如果比这只牛小,那么那些牛就更强壮
跟Stars那个题一个类型 把E值按降序排好 就只用考虑S的值了
#include <iostream>
#include <stdio.h>
#include<algorithm>
#include <string.h>
using namespace std;
const int maxn = 100000+20;
struct nodes{
int id;
int x,y;
}N[maxn];
int n;
int a[maxn],ans[maxn];
bool cmp(nodes a,nodes b)
{
if(a.y!=b.y) return a.y>b.y;
else return a.x<b.x;
}
int lowbit(int x)
{
return x&(-x);
}
int cal(int t)//计算1-t的和
{
int sum=0;
while(t>0)
{
sum+=a[t];
t-=lowbit(t);
}
return sum;
}
void add(int x,int v)//在x的位置加上v
{
while(x<=n)
{
a[x]+=v;
x+=lowbit(x);
}
}
int main()
{
int i;
while(~scanf("%d",&n),n)
{
memset (a,0,sizeof(a));
memset (ans,0,sizeof(ans));
for(i=1;i<=n;i++)
{
scanf("%d%d",&N[i].x,&N[i].y);
N[i].id=i;
}
sort(N+1,N+n+1,cmp);
for( i=1;i<=n;i++)
{
if(N[i].x==N[i-1].x&&N[i].y==N[i-1].y)
{
ans[N[i].id]=ans[N[i-1].id];
}
else
{
ans[N[i].id]=cal(N[i].x+1);
}
add(N[i].x+1,1);
}
for(i=1;i<=n;i++)
{
if(i==n) printf("%d\n",ans[i]);
else printf("%d ",ans[i]);
}
}
}
相关文章推荐
- 友盟官方文档
- leetcode No53. Maximum Subarray
- CentOS关于quota的总结与实践
- 74. Search a 2D Matrix
- javascript eval和JSON之间的联系
- linux使用grep和find查找内容
- 分布式模式之Broker模式
- 让你的Python代码更加pythonic
- 【HEVC帧间预测论文】P1.4 Motion Vectors Merging: Low Complexity Prediction Unit Decision
- Centos 7 文件和目录管理
- 获取网络状态
- linux如何关闭selinux?
- jquery 取值赋值
- Linux wifi 破解
- php webservice服务端和客户端的实现
- Java基础面试题之Singleton模式
- 字符集
- Windows8.1打开程序报 api-ms-win-crt-heap-l1-1-0.dll 错误的解决办法
- 其它网页可以上网,IE浏览器打不开网页的解决办法
- 数学