题目传送门

 #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. 【Android开发-8】生命周期,Activity中打开另外一个Activity

    前言:生命中有很多人陪伴自己走过一生中的某段旅程,仅仅是有些人仅仅是某阶段出现,有些人却陪伴自己非常久.就像小学.中学.高中.大学,那些以前以为会长久拥有的,当经历过天涯各地地忙碌于生活,或如意.或失 ...

  2. 利用easyUI的combobox打造自己主动提示组件

    自己主动提示是时下一个非常流行的功能,比方说百度.谷歌的搜索输入框都使用到了这么一个功能. 因为easyUI的combobox设计师已经考虑到了这个功能.所以仅仅需简单几步我们能够轻松打造自己的自己主 ...

  3. A nonrecursive list compacting algorithm

    A nonrecursive list compacting algorithm Each Erlang process has its own stack and heap which are al ...

  4. org.eclipse.swt.SWTError: No more handles的解决办法

    今天装了JBoss Tools 3.1 插件后,eclipse 打开jsp文件老是报错,或者要我关闭: org.eclipse.swt.SWTError: No more handles 网上找了两个 ...

  5. jQuery常用插件大全(9)ResponsiveSlides插件

    ResponsiveSlides.js是一个展示同一容器内图片的轻量级响应式jQuery幻灯片插件(tiny responsive slideshow jQuery plugin).它支持包括IE6在 ...

  6. 【Java】DateUtil(2)

    import java.sql.Timestamp; import java.text.ParseException; import java.text.SimpleDateFormat; impor ...

  7. maven实战(1)-- maven仓库

    maven仓库配置 转自:http://blog.csdn.net/dongnan591172113/article/details/7685164 1.两类仓库 1.1本地仓库(local repo ...

  8. UISwitch用法:

    代码: #import "ViewController.h" @interface ViewController () @end @implementation ViewContr ...

  9. [SHOI 2015] 脑洞治疗仪

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=4592 [算法] 对于操作1 , 我们首先查询区间[l0 , r0]中有多少个1 , ...

  10. GYM 100741A Queries

    传送门 题目大意: 一个长度为n的序列,q次三种操作 +p  r:下标为p的数+r -p r:下标为p的数-r s l r mod [L,R]中有多少数%m=mod,m已经给出 题解: 开十个树状数组 ...