POJ 2481Cows(树状数组 + 好题)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 15222 | Accepted: 5070 |
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
Hint
#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(树状数组 + 好题)的更多相关文章
- st表树状数组入门题单
预备知识 st表(Sparse Table) 主要用来解决区间最值问题(RMQ)以及维护区间的各种性质(比如维护一段区间的最大公约数). 树状数组 单点更新 数组前缀和的查询 拓展:原数组是差分数组时 ...
- HDU 1166 敌兵布阵(线段树/树状数组模板题)
敌兵布阵 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submi ...
- POJ 2299 Ultra-QuickSort 求逆序数 (归并或者数状数组)此题为树状数组入门题!!!
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 70674 Accepted: 26538 ...
- POJ 3928 Ping pong 树状数组模板题
開始用瓜神说的方法撸了一发线段树.早上没事闲的看了一下树状数组的方法,于是又写了一发树状数组 树状数组: #include <cstdio> #include <cstring> ...
- POJ 3321 Apple Tree 树状数组 第一题
第一次做树状数组,这个东西还是蛮神奇的,通过一个简单的C数组就可以表示出整个序列的值,并且可以用logN的复杂度进行改值与求和. 这道题目我根本不知道怎么和树状数组扯上的关系,刚开始我想直接按图来遍历 ...
- bzoj1103树状数组水题
(卧槽,居然规定了修改的两点直接相连,亏我想半天) 非常水的题,用dfs序(而且不用重复,应该是直接规模为n的dfs序)+树状数组可以轻松水 收获:树状数组一遍A(没啥好骄傲的,那么简单的东西) #i ...
- POJ 3321 树状数组(+dfs+重新建树)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 27092 Accepted: 8033 Descr ...
- POJ 2352Stars 树状数组
Stars Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42898 Accepted: 18664 Descripti ...
- poj 2182 树状数组
这题对于O(n^2)的算法有很多,我这随便贴一个烂的,跑了375ms. #include<iostream> #include<algorithm> using namespa ...
- poj 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
随机推荐
- .Net core Linux环境安装
10月份的时候在自己电脑上搭了开发环境,当时跑完Welcome后就搁置了一段时间,最近有一个小项目要正式采用core来开发并部署在linux环境下,于是在Linux上也搭建一个运行环境. 虽然不经常使 ...
- 阻塞与非阻塞IO step by step
谈到IO,阻塞.非阻塞,异步.同步是绕不开的话题.说实话,我也没搞清楚,网上查了许多资料,大家众说纷纭,一种比较靠谱的说法是:”在处理 IO 的时候,阻塞和非阻塞都是同步 IO,使用使用了特殊的API ...
- jQuery调用WCF服务传递JSON对象
下面这个示例使用了WCF去创建一个服务端口从而能够被ASP.Net页面通过jQuery的AJAX方法访问,我们将在客户端使用Ajax技术来 与WCF服务进行通信.这里我们仅使用jQuery去连接Web ...
- Linux操作系统里查看所有用户
Xwindows界面的就不说了. 1.Linux里查看所有用户 linux里,并没有像windows的net user,net localgroup这些方便的命令来管理用户. (1)在终端里.其实只需 ...
- 浪潮之巅IT那点事之三——神奇的规律
“道可道,非常道”是老子在<道德经>中的开篇第一句话,这句话的意思是:万事万物其真理是可以探索并道说得出来的,但这些真理并非是永恒的,天道轮转,没有永恒不变的真理(来自百度百科).在IT行 ...
- 浅析java类加载器ClassLoader
作为一枚java猿,了解类加载器是有必要的,无论是针对面试还是自我学习. 本文从JDK提供的ClassLoader.委托模型以及如何编写自定义的ClassLoader三方面对ClassLoader做一 ...
- 每个Android开发者都应该了解的资源列表
前言 这是一篇译文,原文地址Resources every Android developer must know,在译文开头,推荐两篇同样适合于Android开发者阅读的资源列表Android开 ...
- 我给出的一份Java服务器端面试题-适合电话面试
这部分内容,参考了<面试官的七种武器>一文. 电面与face2face的面试还是有区别的,很多考察方式无法使用. 从简历聊起,逐渐进入正题. Java语法 重载与重写的区别? java如何 ...
- C# 使用XML序列化对象(二)
在C# 使用XML序列化对象(一)中描述了使用XML序列化对象的最简单的实现. 现在我们来看看稍微复杂一点的情况: 现有两个类:A和B,B是A的派生类,如下所示: public class A { p ...
- Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG]
Sublime Text 3 Build 3065 All System CracKed By Hmily[LCG] <ignore_js_op> 程序员文本编辑器 Sublime Tex ...