Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15222   Accepted: 5070

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

Hint

Huge input and output,scanf and printf is recommended.
 
题意:线段起点s,终点e,问每一个线段包含在几个线段里面,样例中线段[1,2]包含在[0,3]里面所以第一个输出是1,
思路:按照e按照从大到小排序,e相同按照s从小到大排序。因为只有e从大到小排序后,s就可以从小到大更新了,
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <algorithm>
#include <string.h>
using namespace std;
const int MAX = + ;
struct node
{
int s,e;
int index;
};
node a[MAX];
int c[MAX],cnt[MAX];
int cmp(node x,node y)
{
if(x.e == y.e)
return x.s < y.s;
return x.e > y.e;
}
int lowbit(int k)
{
return k & (-k);
}
void add (int k,int num)
{
for(int i = k; i < MAX; i += lowbit(i))
c[i] += num;
}
int sum(int k)
{
int s = ;
for(int i = k; i > ; i -= lowbit(i))
s += c[i];
return s;
}
int main()
{
int n;
while(scanf("%d", &n) != EOF && n)
{
for(int i = ; i < n; i++)
{
scanf("%d%d", &a[i].s, &a[i].e);
a[i].index = i;
}
sort(a,a + n,cmp);
memset(c,,sizeof(c));
memset(cnt,,sizeof(cnt));
cnt[a[].index] = ;
add (a[].s + , );
for(int i = ; i < n; i++)
{
if(a[i].s == a[i - ].s && a[i].e == a[i - ].e)
cnt[a[i].index] = cnt[a[i - ].index];
else
cnt[a[i].index] = sum(a[i].s + );
add(a[i].s + ,);
}
printf("%d",cnt[]);
for(int i = ; i < n; i++)
printf(" %d",cnt[i]);
printf("\n");
} return ;
}

POJ 2481Cows(树状数组 + 好题)的更多相关文章

  1. st表树状数组入门题单

    预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...

  2. HDU 1166 敌兵布阵(线段树/树状数组模板题)

    敌兵布阵 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  3. POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!

    Ultra-QuickSort Time Limit: 7000MS   Memory Limit: 65536K Total Submissions: 70674   Accepted: 26538 ...

  4. POJ 3928 Ping pong 树状数组模板题

    開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...

  5. POJ 3321 Apple Tree 树状数组 第一题

    第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...

  6. bzoj1103树状数组水题

    (卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) #i ...

  7. POJ 3321 树状数组(+dfs+重新建树)

    Apple Tree Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27092   Accepted: 8033 Descr ...

  8. POJ 2352Stars 树状数组

    Stars Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42898   Accepted: 18664 Descripti ...

  9. poj 2182 树状数组

    这题对于O(n^2)的算法有很多,我这随便贴一个烂的,跑了375ms. #include<iostream> #include<algorithm> using namespa ...

  10. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

随机推荐

  1. Python-操作Memcache、Redis、RabbitMQ、

    Memcache 简述: Memcache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要 ...

  2. Windows下安装Redmine

    参考链接:http://www.cnblogs.com/afarmer/archive/2011/08/06/2129126.html 最新教程:http://www.myexception.cn/w ...

  3. 【转】【WPF】WPF样式(Style)—触发器

    样式(Styles)由三部分构成:设置器(Setter).触发器(Triggers).资源(Resources). (1)触发器,让样式的使用更加准确.灵活和高效. (2)触发器(Triggers)主 ...

  4. 我的WCF摸爬滚打之路(1)

    等了好久终于等到今天!盼了好久终于把梦实现……哈哈,仅以此歌词来庆祝我为期3天的wcf学习之路圆满结束. 今天写这个文章的目的在于记录一下我自己在学习WCF的时候碰到的一些问题,俗话说,好记心不如烂笔 ...

  5. Silverlight OOB程序签名问题

    浏览器外部署Silverlight时,为了让部署到本地的Silverlight应用程序保持最新,通常需要在应用程序中添加更新检查的功能.具体实现可参见这儿. 除了文中提到的“应用程序中使用了用户尚未安 ...

  6. PHP基础01:环境搭建

    1.只会前端的只是有时候让我感到很苦恼,所以决定从今天开始学习后端,看了一些关于后端语言的比较帖子,决定选择php作为我的第一门后端语言.这个是我自己的学习笔记.方便自己复习,不写下来会太无聊了. 第 ...

  7. P2P NAT检测和穿越方式

    一.      NAT类型 本文转自:http://www.cnblogs.com/hummersofdie/archive/2013/05/21/3090163.html  1.基本的NAT类型:只 ...

  8. 基于RESTful标准的Web Api

    微软的web api是在vs2012上的mvc4项目绑定发行的,它提出的web api是完全基于RESTful标准的,完全不同于之前的(同是SOAP协议的)wcf和webService,它是简单,代码 ...

  9. [CareerCup] 14.2 Try-catch-finally Java中的异常处理

    14.2 In Java, does the finally block get executed if we insert a return statement inside the try blo ...

  10. Opencv step by step - 图像变换

    这里举出三个案例: #include <cv.h> #include <highgui.h> void image_smooth(IplImage * image) { cvN ...