POJ2481(树状数组:统计数字 出现个数)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 15405 | Accepted: 5133 |
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
题意:统计每个区间是多少个区间的真子集。
#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(树状数组:统计数字 出现个数)的更多相关文章
- POJ3067(树状数组:统计数字出现个数)
Japan Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 24151 Accepted: 6535 Descriptio ...
- POJ3928(树状数组:统计数字出现个数)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2641 Accepted: 978 Descript ...
- HDU 5997 rausen loves cakes(启发式合并 + 树状数组统计答案)
题目链接 rausen loves cakes 题意 给出一个序列和若干次修改和查询.修改为把序列中所有颜色为$x$的修改为$y$, 查询为询问当前$[x, y]$对应的区间中有多少连续颜色段. ...
- poj Ping pong LA 4329 (树状数组统计数目)
Ping pong Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2302 Accepted: 879 Descript ...
- poj2481树状数组解二维偏序
按区间r降序排列,r相同按照l升序排列,两个区间相同时特判一下即可 /* 给定n个闭区间[l,r],如果对区间[li,ri],[lj,rj]来说, 有区间j严格包含(两个边界不能同时重合)在区间i内, ...
- [bzoj1901][zoj2112][Dynamic Rankings] (整体二分+树状数组 or 动态开点线段树 or 主席树)
Dynamic Rankings Time Limit: 10 Seconds Memory Limit: 32768 KB The Company Dynamic Rankings has ...
- HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)
题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...
- BZOJ 2683: 简单题(CDQ分治 + 树状数组)
BZOJ2683: 简单题(CDQ分治 + 树状数组) 题意: 你有一个\(N*N\)的棋盘,每个格子内有一个整数,初始时的时候全部为\(0\),现在需要维护两种操作: 命令 参数限制 内容 \(1\ ...
- FZOJ 2245 动态树(离散+离线+ 树状数组)
Problem 2245 动态树 Accept: 17 Submit: 82Time Limit: 3000 mSec Memory Limit : 65536 KB Problem D ...
随机推荐
- 通信协议之sdp---sdp会话协议
(1)sdp 描述格式 (2)sdp example (3) sdp (1)sdp 描述格式 m=video 1234 RTP/AVP 96a=rtpmap:96 H264a=framerate:15 ...
- 【BZOJ1336】[Balkan2002]Alien最小圆覆盖 随机增量法
[BZOJ1336][Balkan2002]Alien最小圆覆盖 Description 给出N个点,让你画一个最小的包含所有点的圆. Input 先给出点的个数N,2<=N<=10000 ...
- ubuntu 安装codeblocks
本文转载于:http://blog.csdn.net/i_fuqiang/article/details/9749225 1.安装gcc: sudo apt-get install build-ess ...
- maven snapshot和release版本的区别(转)
在使用maven过程中,我们在开发阶段经常性的会有很多公共库处于不稳定状态,随时需要修改并发布,可能一天就要发布一次,遇到bug时,甚至一天要发布N次.我们知道,maven的依赖管理是基于版本管理的, ...
- 九度OJ 1018:统计同成绩学生人数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8807 解决:4651 题目描述: 读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入: 测试输入包含若干测试用例,每个测试用例的 ...
- Neural Task Programming: Learning to Generalize Across Hierarchical Tasks
Neural Task Programming: Learning to Generalize Across Hierarchical Tasks
- linux install beanstalkd
you can instal it via git and then copy systemd script: Step 0. Install git yum install git Step 1. ...
- Latex 4: WinEdt 10试用时间限制的破解+注册码激活
方法1:我发现这个方法1,现在(2018.06.05)在winedt 10.2上已经不能用了,在低版本(10.1及以下版本)上还可以用,所以如果方法1不行,请看方法2. WinEdt 是目前我发现最好 ...
- SPOJ - PHRASES Relevant Phrases of Annihilation —— 后缀数组 出现于所有字符串中两次且不重叠的最长公共子串
题目链接:https://vjudge.net/problem/SPOJ-PHRASES PHRASES - Relevant Phrases of Annihilation no tags You ...
- Java并发知识概述
1.Java内存模型的抽象结构 Java中,所有的实例.静态域和数组元素都存储在堆内存中,堆内存是线程共享的.局部变量,形参,异常处理参数不会在线程之间共享,所以不存在内存可见性问题,也就不受内存模型 ...