Cows
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 15405   Accepted: 5133

Description

Farmer John's cows have discovered that the clover growing along the ridge of the hill (which we can think of as a one-dimensional number line) in his field is particularly good.

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

The input contains multiple test cases. 
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

For each test case, output one line containing n space-separated integers, the i-th of which specifying the number of cows that are stronger than cowi

Sample Input

3
1 2
0 3
3 4
0

Sample Output

1 0 0
题意:统计每个区间是多少个区间的真子集。
#include"cstdio"
#include"cstring"
#include"algorithm"
using namespace std;
const int MAXN=;
struct Node{
int S,E;
int index;
}cows[MAXN];
bool comp(const Node &a,const Node &b)
{
if(a.E==b.E) return a.S < b.S;
else return a.E > b.E;
}
int bit[MAXN];
void add(int i,int x)
{
while(i<MAXN)
{
bit[i]+=x;
i+=i&(-i);
}
}
int sum(int i)
{
int s=;
while(i>)
{
s+=bit[i];
i-=i&(-i);
}
return s;
}
int res[MAXN];
int main()
{
int n;
while(scanf("%d",&n)!=EOF&&n!=)
{
memset(bit,,sizeof(bit));
memset(res,,sizeof(res));
for(int i=;i<n;i++)
{
scanf("%d%d",&cows[i].S,&cows[i].E);
cows[i].S++;// 存在 0
cows[i].E++;
cows[i].index=i;
}
sort(cows,cows+n,comp);
add(cows[].S,);
for(int i=;i<n;i++)
{
if(cows[i].E==cows[i-].E&&cows[i].S==cows[i-].S)
{
res[cows[i].index]=res[cows[i-].index];
}
else
{
res[cows[i].index]=sum(cows[i].S);
}
add(cows[i].S,);
}
for(int i=;i<n-;i++)
{
printf("%d ",res[i]);
}
printf("%d\n",res[n-]);
} return ;
}

POJ2481(树状数组:统计数字 出现个数)的更多相关文章

  1. POJ3067(树状数组:统计数字出现个数)

    Japan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24151   Accepted: 6535 Descriptio ...

  2. POJ3928(树状数组:统计数字出现个数)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2641   Accepted: 978 Descript ...

  3. HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)

    题目链接  rausen loves cakes 题意  给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...

  4. poj Ping pong LA 4329 (树状数组统计数目)

    Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2302   Accepted: 879 Descript ...

  5. poj2481树状数组解二维偏序

    按区间r降序排列,r相同按照l升序排列,两个区间相同时特判一下即可 /* 给定n个闭区间[l,r],如果对区间[li,ri],[lj,rj]来说, 有区间j严格包含(两个边界不能同时重合)在区间i内, ...

  6. [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)

    Dynamic Rankings Time Limit: 10 Seconds      Memory Limit: 32768 KB The Company Dynamic Rankings has ...

  7. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  8. BZOJ 2683: 简单题(CDQ分治 + 树状数组)

    BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...

  9. FZOJ 2245 动态树(离散+离线+ 树状数组)

    Problem 2245 动态树 Accept: 17    Submit: 82Time Limit: 3000 mSec    Memory Limit : 65536 KB  Problem D ...

随机推荐

  1. oauth学习

    https://www.cnblogs.com/blowing00/p/4524132.html

  2. ECMall验证码问题

    一.确认问题: 输入下面这个地址:http://你的网址/index.php?app=captcha&876589486 打开后出现:mt_rand(): max(0) is smaller ...

  3. 1213 - Deadlock found when trying to get lock; try restarting transaction

    1213 - Deadlock found when trying to get lock; try restarting transaction 出现这个原因要记住一点就是:innodb的行锁 和解 ...

  4. linux 改动rootpassword以及忘记rootpassword

    改动rootpassword: $ passwd root 或者sudo passwd root $password:  (要求输入旧的密码) $new  password:(输入两遍新密码) 忘记r ...

  5. 微信小程序页面布局之弹性布局-Flex介绍

    布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + float属性.它对于那些特殊布局非常不方便,比如,垂直居中就不容易实现. 2009年,W3C 提出了一种新 ...

  6. Android Studio .9图片的应用以及制作

    参考http://www.jianshu.com/p/761f4d0e5d8d 先找到图片右键 然后修改图片名称,选择目录,注意必须要放在drawable目录里面,不然引用studio会报错 然后进入 ...

  7. 公网yum 源地址

    1. centos5.*  公网yum 源地址 [root@web ~]# cd /etc/yum.repos.d/[root@web yum.repos.d]# wget -O /etc/yum.r ...

  8. TP框架---thinkphp使用ajax

    thinkphp使用ajax和之前使用ajax的方法一样,不同点在于之前的ajax中的url指向了一个页面,而thinkphp里面的url需要指向一个操作方法. 一.thinkphp使用ajax返回数 ...

  9. live555二次开发经验总结:RTSPClient客户端与RTSPServer服务器

    live555介绍 安防领域的流媒体开发者估计没有谁不知道live555的,可能并不是因为其架构有多牛,代码有多好看,而是因为这玩意存在的年限实在是太长了,从changelog来看,live555从2 ...

  10. Python爬虫--初识爬虫

    Python爬虫 一.爬虫的本质是什么? 模拟浏览器打开网页,获取网页中我们想要的那部分数据 浏览器打开网页的过程:当你在浏览器中输入地址后,经过DNS服务器找到服务器主机,向服务器发送一个请求,服务 ...