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: 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
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
Sample Input
3
1 2
0 3
3 4
0
Sample Output
1 0 0
这题的做法和star那道题差点儿相同,先按x坐标进行升序排列,然后x同样的取对y进行降序排列,然后每次循环推断当前线段和上一条线段是不是x。y都一样,假设一样就直接等于上一条算出的值。不等于就计算。
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define maxn 100006
struct node{
int x,y,id,num;
}a[maxn];
int b[maxn];
bool cmp(node a,node b){
if(a.x==b.x)return a.y>b.y;
return a.x<b.x;
}
bool cmp1(node a,node b){
return a.id<b.id;
} int lowbit(int x){
return x&(-x);
}
void update(int pos,int num){
while(pos<=maxn){
b[pos]+=num;pos+=lowbit(pos);
}
}
int getsum(int pos)
{
int num=0;
while(pos>0){
num+=b[pos];pos-=lowbit(pos);
}
return num;
} int main()
{
int n,m,i,j,t;
while(scanf("%d",&n)!=EOF && n!=0)
{
memset(a,0,sizeof(a));
for(i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].x++;a[i].y++;
a[i].id=i;
}
memset(b,0,sizeof(b));
sort(a+1,a+1+n,cmp);
for(i=1;i<=n;i++){
if(a[i].x==a[i-1].x && a[i].y==a[i-1].y){
a[i].num=a[i-1].num;
}
else{
a[i].num=getsum(maxn)-getsum(a[i].y-1);
}
update(a[i].y,1);
}
sort(a+1,a+1+n,cmp1);
for(i=1;i<=n;i++){
if(i==n)printf("%d\n",a[i].num);
else printf("%d ",a[i].num);
}
}
return 0;
}
poj2481 Cows的更多相关文章
- POJ-2481 Cows (线段树单点更新)
题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...
- poj2481 Cows 树状数组
题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...
- 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)
转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...
- POJ2481:Cows(树状数组)
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- [LeetCode] Bulls and Cows 公母牛游戏
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
- POJ 2186 Popular Cows(Targin缩点)
传送门 Popular Cows Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 31808 Accepted: 1292 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- LeetCode 299 Bulls and Cows
Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...
- [Leetcode] Bulls and Cows
You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...
随机推荐
- Open Cascade:使用鼠标画线
Open Cascade:使用鼠标画线 在View类文件中创建以下代码: 1.创建鼠标消息: afx_msg void OnLButtonDown(UINT nFlags, CPoint point) ...
- 896. Monotonic Array@python
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- ubuntu下操作Hadoop、hdfs、hbase、zookeeper时产生的一些问题及解决办法
2019/05/29 1.在终端输入jps时,没有显示Hdfs的DataNode 在文件夹中分别找到DataNode 和Namenode的version,将Datanode的version改为与nam ...
- 高逼格关闭Win10防火墙
作为一个开发人员,你还需要进入这个界面来关闭防火墙么? 如果是,那么现在,我将为大家介绍一种高逼格的方式: 第一步: 打开Windows PowerShell(管理员) 第二步:查看当前防火墙状态:n ...
- mysql 数据库 show命令
MySQL中有很多的基本命令,show命令也是其中之一,在很多使用者中对show命令的使用还容易产生混淆,本文汇集了show命令的众多用法. 1. show tables或show tables fr ...
- 第一章:systemverilog简介
1.为何要学systemverilog ..... 2.systemverilog起源 ..... 3.systemverilog标准历程 systemverilog3.0 for 综合 system ...
- 由Java实现Valid Parentheses
一.题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- Python爬虫-Scrapy-CrawlSpider与ItemLoader
一.CrawlSpider 根据官方文档可以了解到, 虽然对于特定的网页来说不一定是最好的选择, 但是 CrwalSpider 是爬取规整的网页时最常用的 spider, 而且有很好的可塑性. 除了继 ...
- Solr5.0.0定时更新索引
由于通过配置的方式定时更新不生效,故通过代码执行定时任务更新 package com.thinkgem.jeesite.modules.meeting.task; import java.io.IOE ...
- linux find的用法
①.一般格式: ·find path -option [ -print ] [ -exec -ok command ] {} \; 说明: #-print 将查找到的文 ...