POJ2481:Cows(树状数组)
Description
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: cow i and cow j, their favourite clover range is [Si, Ei] and [Sj, Ej]. If Si <= Sj and Ej <= Ei and Ei - Si > Ej - Sj, we say that cow i is stronger than cow j.
For each cow, how many cows are stronger than her? Farmer John needs your help!
Input
For each test case, the first line is an integer N (1 <= N <= 10 5), which is the number of cows. Then come N lines, the i-th of which contains two integers: S and E(0 <= S < E <= 10 5) 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
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
Hint
Source
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <list>
#include <algorithm>
#include <climits>
using namespace std; #define lson 2*i
#define rson 2*i+1
#define LS l,mid,lson
#define RS mid+1,r,rson
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define gcd(a,b) __gcd(a,b)
#define LL long long
#define N 200005
#define INF 0x3f3f3f3f
#define EXP 1e-8
#define lowbit(x) (x&-x)
const int mod = 1e9+7; struct node
{
int l,r,id;
} a[N];
int n,maxn,c[N]; int cmp(node a,node b)
{
if(a.r!=b.r)
return a.r>b.r;
return a.l<b.l;
} int sum(int x)
{
int ret = 0;
while(x>0)
{
ret+=c[x];
x-=lowbit(x);
}
return ret;
} void add(int x,int d)
{
while(x<=maxn+1)
{
c[x]+=d;
x+=lowbit(x);
}
} int ans[N];
int main()
{
int i,j,k,l,r;
while(~scanf("%d",&n),n)
{
MEM(ans,0);
MEM(c,0);
maxn = -1;
for(i = 1; i<=n; i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].id = i;
maxn = max(maxn,a[i].r);
}
sort(a+1,a+1+n,cmp);
for(i = 1; i<=n; i++)
{
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+1);
}
add(a[i].l+1,1);
}
printf("%d",ans[1]);
for(i = 2; i<=n; i++)
printf(" %d",ans[i]);
printf("\n");
} return 0;
}
POJ2481:Cows(树状数组)的更多相关文章
- poj2481 Cows 树状数组
题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...
- POJ 2481:Cows 树状数组
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14906 Accepted: 4941 Description ...
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- 【POJ2182】Lost Cows 树状数组+二分
题中给出了第 i 头牛前面有多少比它矮,如果正着分析比较难找到规律.因此,采用倒着分析的方法(最后一头牛的rank可以直接得出),对于第 i 头牛来说,它的rank值为没有被占用的rank集合中的第A ...
- poj2182Lost Cows——树状数组快速查找
题目:http://poj.org/problem?id=2182 从后往前确定,自己位置之前没有被确定的且比自己编号小的个数+1即为自己的编号: 利用树状数组快速查找,可另外开一个b数组,角标为编号 ...
- POJ2182 Lost Cows 树状数组
题意:有编号1~n乱序排列的奶牛,给出了每一个奶牛前小于自己编号的奶牛数目 维护一个树状数组,下标是编号,值为$0/1$标识是否存在,很显然最后一个牛的编号是知道的,我们在树状数组上二分出前缀和为小于 ...
- POJ 2182 Lost Cows (树状数组 && 二分查找)
题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...
- poj_2481,Cows,树状数组
将e按从大到小排序,统计前i-1个中比 #include<iostream> #include<cstdio> #include<cstring> #include ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
随机推荐
- NodeJs学习记录(五)初学阶段关于ejs和路由
1.因为只是用了一点皮毛,所以使用起来感觉基本和jsp无异, 逻辑代码块使用 <% if() {} else %> , 输出参数值使用 <%=title %>, 有一个 ...
- .Net实战之反射相关类之间的人体经络关系
--1.类的部分组成成员 --2.巧记成员之间的关系 [MyTable("T_UserInfo")] public class UserInfo : Person, UserSer ...
- Python学习日记之练习代码
# -*- coding:utf-8 -*- number = 23 test=True while test: guess=int(raw_input('输入数字')) if guess==numb ...
- Microsoft SQL Server学习(六)--查询语句
联合查询 use student --建表 create table class_A( id int primary key, name varchar(50), sex char(50), cour ...
- NVIDIA各个领域芯片现阶段的性能和适应范围
NVIDIA作为老牌显卡厂商,在AI领域深耕多年.功夫不负有心人,一朝AI火,NVIDIA大爆发,NVIDIA每年送给科研院所和高校的大量显卡,大力推广Physix和CUDA,终于钓了产业的大鱼. 由 ...
- oracle数据库跨库查询
create public database link mylink connect to orclname identified by orclpasswd using 'ORCL'; drop p ...
- Android(java)学习笔记205:JNI之编写jni程序适配所有处理器型号
1. 还是以"02_两个数相加"为例,你会发现这个jni程序只能在ARM处理器下运行,如下: 如果我们让上面的程序运行在x86模拟器上,处理平台不对应,报如下错误: 03-29 ...
- MFC_1.3 控件子类化 消息反射
控件子类化 如果想要在默认的控件类中添加一些功能,就需要子类化一个控件类 在类内可以响应控件所有的消息,并且可以添加自己的函数和数据 通过类向导子类化控件的步骤 打开类向导,创建一个 MFC 类,不要 ...
- 网站卡测试用 PageSpeed Insights
这个是google测试网页的;https://developers.google.com/speed/pagespeed/insights/ PageSpeed Insights 简介 PageSpe ...
- 09Microsoft SQL Server 表数据插入,更新,删除
Microsoft SQL Server 表数据插入,更新,删除 向表中插入数据 INSERT INTO insert into tb1 values(0004,'张凤凤') insert into ...