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 ...
随机推荐
- 浏览器报错 SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 2 of the JSON data
ajax调用是200,结果返回的不是json字符串(字符串格式不是json应该有的格式),进入了ajax的error回调函数,改为返回json字符串,问题解决了.
- 拖拽功能-jquery
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 01Ping程序的设计
1.Ping程序设计具体设计任务 1.1 实验目的 PING程序是我们使用的比较多的用于测试网络连通性的程序.PING程序基于ICMP,使用ICMP的回送请求和回送应答来工作.由计算机网络课程知道,I ...
- 【EL&JSTL】学习笔记
一.EL表达式(形式:${ }) 1. 1 EL运算符 算术运算符: +.-.*./.% 示例 结果 ${1+1} 2 ${1-1} 0 ${1*3} 3 ${3/2} 1.5 ${5%3} 2 关系 ...
- 【UVA 1636】 (条件概率)
题链:https://cn.vjudge.net/problem/UVA-1636 题意 给出一把枪的子弹序列,已知开了一枪且无子弹,问下一步的决策是旋转,还是开下一枪 题解 已知开一枪没子弹,下一枪 ...
- Root of AVL Tree
04-树5 Root of AVL Tree(25 分) An AVL tree is a self-balancing binary search tree. In an AVL tree, the ...
- JavaScript中上传文件为图片如何读取+JS中如何使用clip()剪切指定区域(UI组件之图片剪裁器)
File读取和FileReader() //获取上传的文件/图片 function getFile(){ var files,len; var reader = new FileReader(); v ...
- sqlserver复制表数据到另一个表
SQL Server中,如果目标表存在: insert into 目标表 select * from 原表; SQL Server中,,如果目标表不存在: select * into 目标表 from ...
- zoj 2850 Beautiful Meadow
Beautiful Meadow Time Limit: 2 Seconds Memory Limit: 65536 KB Tom's Meadow Tom has a meadow in ...
- 【ITOO 3】.NET 动态建库建表:实用EF框架提供的codeFirst实现动态建库
导读:在上篇博客中,介绍了使用SQL字符拼接的方式,实现动态建库建表的方法.这样做虽然也能够实现效果,但是,太麻烦,而且,如果改动表结构,字段的话,会对代码修改很多.但是EF给我们提供了一种代码先行的 ...