Cows(poj 2481 树状数组)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 15301 | Accepted: 5095 |
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
思路:初看好像挺复杂的。其实可以把区间[S, E]看成点(S, E),这样题目就转化为hdu 1541 Stars。只是这里是求该点左上方的点的个数。
虽然如此,我还是WA了不少,有一些细节没注意到。给点排序时是先按y由大到小排序,再按x由小到大排序。而不能先按x排序。比如n=3, [1,5], [1,4], [3,5]的例子。另外还要注意对相同点的处理。
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std; const int MAX = ; struct Node{
int x, y, id, ans;
}seq[MAX];
int sum[MAX], n; int cmp1(Node a,Node b){
if(a.y==b.y) return a.x<b.x;
return a.y>b.y;
}
int cmp2(Node a,Node b){
return a.id<b.id;
} int lowbit(int x){
return x & (-x);
}
void add(int pos, int val){
while(pos < MAX){
sum[pos]+=val;
pos+=lowbit(pos);
}
}
int getsum(int pos){
int res = ;
while(pos>){
res+=sum[pos];
pos-=lowbit(pos);
}
return res;
} int main()
{
freopen("in.txt","r",stdin);
int i,j;
while(scanf("%d", &n) && n){
for(i=;i<=n;i++){
scanf("%d%d", &seq[i].x, &seq[i].y);
seq[i].x++, seq[i].y++;
seq[i].id = i;
}
sort(seq+,seq+n+,cmp1);
memset(sum, , sizeof(sum));
seq[].ans = ;
add(seq[].x, );
int fa = ;
for(i=;i<=n;i++){
if(seq[i].x == seq[fa].x && seq[i].y == seq[fa].y){
seq[i].ans = seq[fa].ans;
}else{
fa = i;
seq[i].ans = getsum(seq[i].x);
} add(seq[i].x, );
}
sort(seq+,seq+n+,cmp2);
printf("%d", seq[].ans);
for(i=;i<=n;i++) printf(" %d", seq[i].ans);
printf("\n");
}
return ;
}
Cows(poj 2481 树状数组)的更多相关文章
- Cows POJ - 2481 树状数组
Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can ...
- 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 2299 树状数组求逆序数+离散化
http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...
- MooFest POJ - 1990 (树状数组)
Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gather ...
- poj 3928 树状数组
题目中只n个人,每个人有一个ID和一个技能值,一场比赛需要两个选手和一个裁判,只有当裁判的ID和技能值都在两个选手之间的时候才能进行一场比赛,现在问一共能组织多少场比赛. 由于排完序之后,先插入的一定 ...
- POJ 2299 树状数组+离散化求逆序对
给出一个序列 相邻的两个数可以进行交换 问最少交换多少次可以让他变成递增序列 每个数都是独一无二的 其实就是问冒泡往后 最多多少次 但是按普通冒泡记录次数一定会超时 冒泡记录次数的本质是每个数的逆序数 ...
- poj 2299 树状数组求逆序对数+离散化
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 54883 Accepted: 20184 ...
- poj 2182 树状数组
这题对于O(n^2)的算法有很多,我这随便贴一个烂的,跑了375ms. #include<iostream> #include<algorithm> using namespa ...
随机推荐
- UITableView属性和方法
1.初始化一个UITableView - (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style struct CGRect { C ...
- 163k地方门户网站系统团购定时结束限量控制
#coding=utf8 #!/usr/bin/env python # 网站自动审核系统 import pymssql import re import sys import datetime im ...
- python 反向查找
python 字符串反向查找大部分在正向查找前面加入r eg: str.rfind('str') str.rsplit(',')
- Keil C51怎样将子程序段定位在固定的地址位?
以下2问题均要用C51解决1.怎样将1个子程序段定位在1个固定的地址位置?例如将 INT BCD2HEX(INT XX)定位在1000H2.如何在EEPROM 中固定的位置存放1字符串?如在200H处 ...
- Keil C51内存分配与优化
C51的内存分配不同于一般的PC,内存空间有限,采用覆盖和共享技术.在Keil编译器中,经过编译后,会形成一个M51文件,在其内部可以详细的看到内存的分配情况. C51内存常见的两个误区: A.变量超 ...
- UESTC_Dividing Numbers CDOJ 1156
Dividing Numbers Time Limit: 9000/3000MS (Java/Others) Memory Limit: 262144/262144KB (Java/Other ...
- POJ 1631 Bridging signals DP(最长上升子序列)
最近一直在做<挑战程序设计竞赛>的练习题,感觉好多经典的题,都值得记录. 题意:给你t组数据,每组数组有n个数字,求每组的最长上升子序列的长度. 思路:由于n最大为40000,所以n*n的 ...
- SpringMVC(二)——流程控制
SpringMVC主要就是用来做流程控制的,这篇博客总结一下如何在流程控制添加Interceptor(拦截器),如何将进行流程Mapping映射解析,如何编写Controller(控制器). 一,首先 ...
- [置顶] Application,Session,Cookie之Application对象
概述 Application为全局作用域,且只有一个Application对象,它可以存储和访问任意页面的变量(数据存储类型都是Object,也就是任意类型),同时也被多页面使用(也为引用). App ...
- [Protractor] Locators and Suites in Protractor
HTML: <ul class="list"> <li ng-repeat="item in itmes" ng-click="se ...