树状数组 POJ 2481 Cows
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std; const int MAX_N = + ;
int cnt[MAX_N];
int ans[MAX_N];
int maxn = -;
struct node
{
int s, e;
int id;
}cow[MAX_N]; inline int read(void)
{
int x = , f = ; char ch = getchar ();
while (ch < '' || ch > '') {if (ch == '-') f = -; ch = getchar ();}
while (ch >= '' && ch <= '') {x = x * + ch - ''; ch = getchar ();}
return x * f;
} inline void print(int x)
{
if (x < ) {putchar ('-'); x = -x;}
if (x > ) print (x / );
putchar (x % + '');
} bool cmp(node x, node y) //先对e从大到小排序
{
if (x.e == y.e)
return x.s < y.s; //如果如果相等,再按s从小到大排序
else
return x.e > y.e;
} int lowbit(int t) //求 2^t
{
//return t & (t ^ (t - 1));
return t & (-t);
} void plus(int x)
{
while (x <= maxn)
{
ans[x]++; //记录强壮的牛数
x += lowbit (x);
}
} int sum(int x) //统计前x项强壮的牛数
{
int sum = ;
while (x > )
{
sum += ans[x];
x -= lowbit(x);
}
return sum;
} int main(void) //POJ 2481 Cows
{
//freopen ("inH.txt", "r", stdin);
int n; while (scanf ("%d", &n) && n)
{
for (int i=; i<=n; ++i)
{
cow[i].s = read (); cow[i].e = read (); //读入外挂优化
//scanf ("%d%d", &cow[i].s, &cow[i].e);
cow[i].id = i;
maxn = max (maxn, cow[i].e);
}
memset (ans, , sizeof (ans));
sort (cow+, cow+n+, cmp);
for (int i=; i<=n; ++i)
{
if (cow[i].e == cow[i-].e && cow[i].s == cow[i-].s) //如果值相等,不加入统计
cnt[cow[i].id] = cnt[cow[i-].id];
else
{
cnt[cow[i].id] = sum (cow[i].s + ); //第id的牛有多少比它强壮
}
plus (cow[i].s + );
}
int j;
for (j=; j<n; ++j)
{
print (cnt[j]); putchar (' '); //输出外挂优化
//printf ("%d ", cnt[j]);
}
printf ("%d\n", cnt[j]);
} return ;
}
树状数组 POJ 2481 Cows的更多相关文章
- 线段树/树状数组 POJ 2182 Lost Cows
题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...
- 树状数组 || POJ 2352 Stars
Astronomers often examine star maps where stars are represented by points on a plane and each star h ...
- 树状数组 || POJ 3321 Apple Tree
一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...
- LCA+树状数组 POJ 2763 Housewife Wind
题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...
- POJ 2481:Cows 树状数组
Cows Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 14906 Accepted: 4941 Description ...
- POJ 2481 Cows(树状数组)
Cows Time Limit: 3000MS Memory L ...
- poj 2481 Cows(树状数组)题解
Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...
- poj 2481 - Cows(树状数组)
看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...
- POJ 2481 Cows 【树状数组】
<题目链接> 题目大意: 就是给出N个区间,问这个区间是多少个区间的真子集. 解题分析: 本题与stars类似,只要巧妙的将线段的起点和终点分别看成 二维坐标系中的x,y坐标,就会发现,其 ...
随机推荐
- Arcgis Engine(ae)接口详解(4):featureClass的feature插入
//由于测试数据不完善,featureClass在此要只设null值,真实功能要设实际的值 IFeatureClass featureClass = null; //获取某个字段的索引,后面取字段值用 ...
- MapReduce算法形式二:去重(HashSet)
案例二:去重(shuffle/HashSet等方法)shuffle主要针对的是key去重HashSet主要针对values去重
- Oracle数据库案例整理-Oracle系统执行时故障-断电导致数据文件状态变为RECOVER
1.1 现象描写叙述异常断电.数据库数据文件的状态由ONLINE变为RECOVER. 系统显演示样例如以下信息:SQL>selectfile_name,tablespace_name, ...
- (23) java web的struts2框架的使用-struts动态调用和通配符
一,动态查找 1,配置允许动态调用 <!-- 允许动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocat ...
- ubuntu 本地和服务器scp文件传输
安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH 远程登入 Ubuntu 机 ssh username@192.168.0.1 ...
- HDU3338 Kakuro Extension —— 最大流、方格填数类似数独
题目链接:https://vjudge.net/problem/HDU-3338 Kakuro Extension Time Limit: 2000/1000 MS (Java/Others) ...
- POJ3281 Dining —— 最大流 + 拆点
题目链接:https://vjudge.net/problem/POJ-3281 Dining Time Limit: 2000MS Memory Limit: 65536K Total Subm ...
- 包、修饰符、内部类、匿名内部类(java基础知识十)
1.package关键字的概述及作用 * A:为什么要有包 * 将字节码(.class)进行分类存放 * B:包的概述 * * C:包的作用 * 包名要定义在第一行, ...
- 奶牛排序——RMQ
[问题描述]奶牛在熊大妈的带领下排成了一条直队.显然,不同的奶牛身高不一定相同……现在,奶牛们想知道,如果找出一些连续的奶牛,要求最左边的奶牛 A 是最矮的,最右边的 B 是最高的,且 B 高于 A ...
- css3 实现png图片改变背景颜色
实际上是用的是就是css的filter的drop-shadow属性 drop-shadow: 1 不支持内阴影 2 不支持多阴影 3 兼容性 ie13+ 谷歌 火狐 android4.4+ i ...