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

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的更多相关文章

  1. POJ-2481 Cows (线段树单点更新)

    题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...

  2. poj2481 Cows 树状数组

    题目链接:http://poj.org/problem?id=2481 解题思路: 这道题对每组数据进行查询,是树状数组的应用.对于二维的树状数组, 首先想到排序.现在对输入的数据按右值从大到小排序, ...

  3. 线段树总结 (转载 里面有扫描线类 还有NotOnlySuccess线段树大神的地址)

    转载自:http://blog.csdn.net/shiqi_614/article/details/8228102 之前做了些线段树相关的题目,开学一段时间后,想着把它整理下,完成了大牛NotOnl ...

  4. POJ2481:Cows(树状数组)

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  5. [LeetCode] Bulls and Cows 公母牛游戏

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

  6. POJ 2186 Popular Cows(Targin缩点)

    传送门 Popular Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31808   Accepted: 1292 ...

  7. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  8. LeetCode 299 Bulls and Cows

    Problem: You are playing the following Bulls and Cows game with your friend: You write down a number ...

  9. [Leetcode] Bulls and Cows

    You are playing the following Bulls and Cows game with your friend: You write a 4-digit secret numbe ...

随机推荐

  1. 安装 Zend Studio 报错:0x80070666

    出现 0x80070666 报错时 查看日志文件,发现调用VC14(即:Microsoft Visual C++ 2015 Redistributable)时,出错返回0x666 先卸载原有的VC14 ...

  2. Python打包成exe,pyc

    D:\mypython\path\ C:\Python27\Scripts\pyinstaller.exe -w mypython.py # Python打包成exe D:\mypython\path ...

  3. scanf_s读取键盘输入字符串失败

    #include<stdio.h> int main() { ]; ]; printf("Input string:\n"); scanf_s("%s&quo ...

  4. Spoj8093 Sevenk Love Oimaster

    题目描述 题解: 对于所有n串建广义后缀自动机. (广义后缀自动机唯一区别就是每次将las附成1,并不需要在插入时特判) 建完后再建出parent树,然后用dfs序+树状数组搞区间不同种类. 其实就是 ...

  5. 25. TABLESPACES , 26. TABLE_CONSTRAINTS , 27. TABLE_PRIVILEGES

    25. TABLESPACES TABLESPACES表提供有关活动MySQL Cluster表空间的信息. TABLESPACES表有以下列: TABLESPACE_NAME :表空间名称 ENGI ...

  6. 夯实Java:从面向对象说起

    作者:伯特出处:github.com/ruicbAndroid/LoulanPlan声明:本文出自伯特的<LoulanPlan>,转载务必注明作者及出处. 刚学习 Java 那会就接触了& ...

  7. 04--activiti demo

    核心API1:ProcessEngine说明:1) 在Activiti中最核心的类,其他的类都是由他而来.2) 产生方式: ProcessEngine processEngine = ProcessE ...

  8. LeetCode(237)Delete Node in a Linked List

    题目 Write a function to delete a node (except the tail) in a singly linked list, given only access to ...

  9. 基于 WPF + Modern UI 的 公司OA小助手 开发总结

    前言: 距离上一篇博客,整整一个月的时间了.人不能懒下来,必须有个阶段性的总结,算是对我这个阶段的一个反思.人只有在总结的过程中才会发现自己的不足. 公司每天都要在OA系统上上班点击签到,下班点击签退 ...

  10. 在mysql的操作界面中,如何清屏幕

    1.快捷键:Ctrl+L2.通过执行SHELL命令: \! clear实际上 \! 用来执行操作系统的shell命令,不仅是clear,其他命令也可以.shell命令执行完成后,会返回mysql Us ...