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

思路:

树状数组还能做这个orz

题意大概就是给你每头牛代表的区间[a,b],要求给出:比第i头牛区间大的牛的头数。

首先我们有每头牛的区间[ai,bi],我们先按照bi降序排列,这样,我们能保证每头牛前面的牛的b都大于等于他,我们再按照ai升序排列,这样我们就能得通过比较a来得到答案。

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<queue>
#include<cmath>
#include<string>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<iostream>
#include<algorithm>
#include<sstream>
#define ll long long
const int N=1e5+10;
const int INF=1e9;
using namespace std;
int n,cnt;
struct node{
int s,e;
int id;
}cow[N];
int a[N],ans[N]; int lowbit(int x){
return x&(-x);
}
void update(int x){
for(int i=x;i<N;i+=lowbit(i)){
a[i]++;
}
}
int sum(int x){
int count=0;
for(int i=x;i>=1;i-=lowbit(i)){
count+=a[i];
}
return count;
} int cmp(node a,node b){
if(a.e>b.e) return 1;
else if(a.e==b.e && a.s<b.s) return 1;
return 0;
} int main(){
while(scanf("%d",&n) && n){    //加~就WA
memset(a,0,sizeof(a));
for(int i=1;i<=n;i++){
scanf("%d%d",&cow[i].s,&cow[i].e);
cow[i].s++;
cow[i].e++;
cow[i].id=i;
}
sort(cow+1,cow+n+1,cmp);
for(int i=1;i<=n;i++){
if(cow[i].e==cow[i-1].e && cow[i].s==cow[i-1].s){
ans[cow[i].id]=ans[cow[i-1].id];
}
else{
ans[cow[i].id]=sum(cow[i].s);
}
update(cow[i].s);
}
for(int i=1;i<=n;i++){
printf("%d ",ans[i]);
}
printf("\n");
}
return 0;
}

poj 2481 Cows(树状数组)题解的更多相关文章

  1. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  2. Cows POJ - 2481 (树状数组 + 单点更新 + 区间查询)

    Cows 思路:我们可以按照每个范围的S从小到大排序,相同的S按E从大到小排序,这样的好处是当前范围的S一定大于等于之前范围的S(即当前的范围可能被之前范围的包围),那么我们只需要统计之前的范围E比当 ...

  3. POJ 2481:Cows 树状数组

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14906   Accepted: 4941 Description ...

  4. POJ 2182 Lost Cows (树状数组 && 二分查找)

    题意:给出数n, 代表有多少头牛, 这些牛的编号为1~n, 再给出含有n-1个数的序列, 每个序列的数 ai 代表前面还有多少头比 ai 编号要小的牛, 叫你根据上述信息还原出原始的牛的编号序列 分析 ...

  5. poj2481 Cows 树状数组

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

  6. POJ2481:Cows(树状数组)

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

  7. POJ 3468(树状数组的威力)

    之前说过这是线段树的裸题,但是当看了http://kenby.iteye.com/blog/962159 这篇题解后我简直震惊了,竟然能如此巧妙地转化为用树状数组来处理,附上部分截图(最好还是进入原网 ...

  8. poj 2229 Ultra-QuickSort(树状数组求逆序数)

    题目链接:http://poj.org/problem?id=2299 题目大意:给定n个数,要求这些数构成的逆序对的个数. 可以采用归并排序,也可以使用树状数组 可以把数一个个插入到树状数组中, 每 ...

  9. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  10. poj 2155 Matrix (树状数组)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 16797   Accepted: 6312 Descripti ...

随机推荐

  1. Redis添加历史浏览记录

    参考资料 http://redisdoc.com/index.html http://redis-py.readthedocs.io/en/latest/#indices-and-tables 1.什 ...

  2. oracle(三) SQL语句

    1.聚集函数遇到空值时,除count(*)外,都会跳过空值. 2.group by 细化聚集函数的作用对象 3.group by有个原则,就是select后面出面的列,除聚集函数外必须出现在group ...

  3. xpath教程 2 - lxml库

    xpath教程 2 - lxml库 这些就是XPath的语法内容,在运用到Python抓取时要先转换为xml. lxml库 lxml 是 一个HTML/XML的解析器,主要的功能是如何解析和提取 HT ...

  4. XMind思维导图主题操作要点

  5. Json和List的表示形式

    JsonObject和List的表示形式 package payItem.test; import java.util.ArrayList; import java.util.List; import ...

  6. BGD-py实现学习【1】[转载]

    转自:https://github.com/icrtiou/Coursera-ML-AndrewNg 1.源码-对数据读取 import numpy as np import pandas as pd ...

  7. spark shuffle原理

    1.spark中窄依赖的时候不需要shuffle,只有宽依赖的时候需要shuffle,mapreduce中map到reduce必须经过shuffle 2.spark中的shuffle fetch的时候 ...

  8. webdriver模拟鼠标悬浮

    未经作者允许,禁止转载! 有时候会遇到这样的情况,鼠标停留在某一区域,不需要点击,悬浮在这一区域的上方就会显示该区域的下拉框,如下图 下面将鼠标停留在“日历”和“星座”这两个部分之间来回悬浮,下面是代 ...

  9. 怎么在jquery里清空文本框的内容

    $("input[name='test']").val("").focus(); // 将name=test的文本框清空并获得焦点,以便重新输入

  10. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...