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. 洛谷P3166 数三角形 [CQOI2014] 数论

    正解:数论 解题报告: 传送门! 很久以前做的题了呢,,,回想方法还想了半天QAQ 然后写这题题解主要是因为看到了好像有很新颖的法子,就想着,学习一下趴,那学都学了不写博客多可惜 首先港下最常规的方法 ...

  2. oracle(五)tkprof 使用 transient kernal profile 侧面 轮廓

    1.show parameter sql_trace value是false表示系统当前不会产生trace文件 2.使产生trace文件 alter session set sql_trace = t ...

  3. EasyUI 基本的拖动和放置

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Ba ...

  4. csrf攻击原理及如何防止csrf攻击

    CSRF(Cross-site request forgery)跨站请求伪造,也被称为“One Click Attack”或者Session Riding,是一种对网站的恶意利用,通过伪装来自受信任用 ...

  5. 可以搜索到DedeCms后台文章列表文档id吗?或者快速定位id编辑文章

    我们在建站时有的时候发现之前的文章有错误了,要进行修改,但又不知道文章名,只知道大概的文章id,那么可以搜索到DedeCms后台文章列表文档id吗?或者快速定位文章id方便修改? 第一种方法:复制下面 ...

  6. SVN 取消版本控制并添加至忽略列表

    忽略已经版本控制的文件忽略文件和目录 起因: 由于项目中的IDE Jdeveloper在某些指定目录下面会生成server.xml以及common文件夹,且包含一个名为bc4j.xcfg的文件,每次当 ...

  7. 7.9 Models -- Connection to An HTTP Server

    一.概述 1. 如果你的Ember应用程序需要从一个HTTP服务器加载JSON数据,在你的服务器返回的任何格式中,配置Ember Data的过程将会加载records. 2. store使用一个被称为 ...

  8. #C++初学记录(sort函数)

    sort函数 前言:当进行贪心算法的学习时,需要用到sort函数,因为初学c++汇编语言,sort的具体用法没有深入学习,所以这里进行sort学习记录并只有基础用法并借用贪心算法题目的代码. 百度百科 ...

  9. Verilog篇(四)时序模型

    时序模型:仿真器的时间推进模型,它反映了推进仿真时间和调度事件的方式. 1)门级时序模型:适用于分析所有的连续赋值语句,过程连续赋值语句,门级原语,用户自定义原语. 特点:任意时刻,任意输入变化都将重 ...

  10. discuz注册页修改

    大家好!近来备受发帖机困扰,备受垃圾帖子困扰.一直以来都纯粹在删帖当中,本来网站服务器就是国内服务器,这样一来很多关键字是禁止的,可不管如何设置防灌水还是无法杜绝这一事项,特别是国内空间的网站,一出现 ...