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 ...
随机推荐
- Saltstack-初体验
安装 rpm -Uvh http://mirrors.yun-idc.com/epel/6Server/x86_64/epel-release-6-8.noarch.rpm yum install s ...
- html中的src与href的区别
写代码的时候就经常把这两个属性弄混淆,到底是href还是src,href标识超文本引用,用在link和a等元素上,href是引用和页面关联,是在当前元素和引用资源之间建立联系,src表示引用资源,表示 ...
- Gradle的HelloWorld
Gradle的脚本名为 build.gradle task hello{ doLast{ println("Hello World") } } 运行:gradle -q hell ...
- MahApps.Metro怎么调用消息窗口
网上查看了好多资料,没有找到很清楚明了的结果,经过了多天的研究,无意发现了这个方法来进行全局调用 效果展示:
- WP老杨解迷:如何获得更多的应用评价和解读内容刷新
Windows Phone的市场评论功能研究的时间比较长,只是这一功能,估计就能写一篇论文,我曾搞过多款评论数超高的游戏,其中<少林塔防>是重量级的作品,至今稳坐最高评分第一把交椅,如果不 ...
- C++的CreateThread实例
function CreateThread( lpThreadAttributes: Pointer; {安全设置} dwStackSize: DWORD; ...
- 未能加载文件或程序集“XXXXX”或它的某一个依赖项。试图加载格式不正确的程序。
未能加载文件或程序集“FastColoredTextBox, Version=2.10.5.0, Culture=neutral, PublicKeyToken=null”或它的某一个依赖项.试图加载 ...
- python字符串str和字节数组相互转化
b = b"Hello, world!" # bytes object s = "Hello, world!" # str object print('str ...
- python数字图像处理(7):图像的形变与缩放
图像的形变与缩放,使用的是skimage的transform模块,函数比较多,功能齐全. 1.改变图片尺寸resize 函数格式为: skimage.transform.resize(image, o ...
- 5.HBase In Action 第一章-HBase简介(1.1.3 HBase的兴起)
Pretend that you're working on an open source project for searching the web by crawling websites and ...