题目传送门

 #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的更多相关文章

  1. 线段树/树状数组 POJ 2182 Lost Cows

    题目传送门 题意:n头牛,1~n的id给它们乱序编号,已知每头牛前面有多少头牛的编号是比它小的,求原来乱序的编号 分析:从后往前考虑,最后一头牛a[i] = 0,那么它的编号为第a[i] + 1编号: ...

  2. 树状数组 || POJ 2352 Stars

    Astronomers often examine star maps where stars are represented by points on a plane and each star h ...

  3. 树状数组 || POJ 3321 Apple Tree

    一道dfs序+树状数组的题 因为并没有get到dfs序以及对树状数组也不熟练卡了很久orz dfs序: in和out是时间戳 dfs序可以将树转化成为一个序列,满足区间 -> 子树 然后就可以用 ...

  4. LCA+树状数组 POJ 2763 Housewife Wind

    题目传送门 题意:两种操作,问u到v的距离,并且u走到了v:把第i条边距离改成w 分析:根据DFS访问顺序,将树处理成链状的,那么回边处理成负权值,那么LCA加上BIT能够知道u到v的距离,BIT存储 ...

  5. POJ 2481:Cows 树状数组

    Cows Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14906   Accepted: 4941 Description ...

  6. POJ 2481 Cows(树状数组)

                                                                      Cows Time Limit: 3000MS   Memory L ...

  7. poj 2481 Cows(树状数组)题解

    Description Farmer John's cows have discovered that the clover growing along the ridge of the hill ( ...

  8. poj 2481 - Cows(树状数组)

    看的人家的思路,没有理解清楚,,, 结果一直改一直交,,wa了4次才交上,,, 注意: 为了使用树状数组,我们要按照e从大到小排序.但s要从小到大.(我开始的时候错在这里了) 代码如下: #inclu ...

  9. POJ 2481 Cows 【树状数组】

    <题目链接> 题目大意: 就是给出N个区间,问这个区间是多少个区间的真子集. 解题分析: 本题与stars类似,只要巧妙的将线段的起点和终点分别看成 二维坐标系中的x,y坐标,就会发现,其 ...

随机推荐

  1. Testng 运行报错:"Total tests run: 0, Failures: 0, Skips: 0"以及找不到class文件的问题

    "Total tests run: 0, Failures: 0, Skips: 0" This means that there were no tests executed a ...

  2. scala快速学习笔记(二):控制结构,类和对象

    IV.控制结构 1.if/else 除基本用法外,if/else语句能用来赋值,进而代替?:运算符.这得益于在Scala中,每个语句块都有值,就是该语句块最后一个语句的值.请看下面的代码. def a ...

  3. NettyIO

  4. 在Qt Creator中为Qt工程添加资源

    1.右键单击工程 -> Add New ... -> Qt -> Qt Resource File -> Choose... -> Name: -> Next -& ...

  5. 使用NetBeans生成jar包,并在jar包中添加资源

    在NetBeans中,执行Clean and Build便可得到jar文件 若要在jar中添加资源,先用压缩软件打开jar,然后将资源拖进当前归档文件即可 使用Class.getResource(St ...

  6. jquery.validate ajax验证

    $("#form1").validate({ rules: { ctl00$ContentPlaceHolder1$txtShopName: { remote: { type: & ...

  7. locate和grep命令

    这任然是一篇读书笔记,以下内容来源<linux命令速查手册>这本书. 1.locate命令 用文件名或者文件名的部分来搜索文件在系统中的位置.locate不搜索具体目录,而是搜索一个数据库 ...

  8. spring-jar包详解整理(大合集)

    转:https://blog.csdn.net/weisong530624687/article/details/50888094 spring.jar 是包含有完整发布模块的单个jar 包.但是不包 ...

  9. html5--6-3 CSS语法2

    html5--6-3 CSS语法2 实例 div包含p和h标签的时候可以,但是p标签包含h标签的时候不可以  学习要点 掌握引入外部样式表方法 插入样式的三种方法 内联样式表(行内) 内部样式表(st ...

  10. [Selenium] WebDriver 操作文件系统

    1)屏幕截图 接口函数是 TakesScreenshot 示例: import java.io.File; import org.apache.commons.io.FileUtils; public ...