Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments
题目连接:
http://www.codeforces.com/contest/652/problem/D
Description
You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.
Input
The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.
Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.
Output
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
Sample Input
4
1 8
2 3
4 7
5 6
Sample Output
3
0
1
0
Hint
题意
给你n个线段,让你输出每个线段完全包含多少个其他的线段
保证任意两个线段的端点不重合
题解:
先离散化一波
然后离线树状数组维护一波就好了
for循环暴力扫左端点,然后树状数组去查询小于右端点的线段有多少个,就完了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 4e5+7;
pair<pair<int,int>,int>p[maxn];
int ans[maxn];
vector<int> Q;
map<int,int> H;
int a[maxn];
int lowbit(int x){return x&(-x);}
void update(int x,int v)
{
for(int i=x;i<maxn;i+=lowbit(i))
a[i]+=v;
}
int get(int x)
{
int tot = 0;
for(int i=x;i;i-=lowbit(i))
tot+=a[i];
return tot;
}
int main()
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d%d",&p[i].first.first,&p[i].first.second);
Q.push_back(p[i].first.first),Q.push_back(p[i].first.second);
p[i].second=i;
}
sort(Q.begin(),Q.end());
Q.erase(unique(Q.begin(),Q.end()),Q.end());
for(int i=0;i<Q.size();i++)
H[Q[i]]=i+1;
for(int i=1;i<=n;i++)
{
p[i].first.first=H[p[i].first.first];
p[i].first.second=H[p[i].first.second];
update(p[i].first.second,1);
}
sort(p+1,p+1+n);
for(int i=1,j=1;i<maxn;i++)
{
while(j<=n&&p[j].first.first==i)
{
ans[p[j].second]=get(p[j].first.second);
update(p[j].first.second,-1);
j++;
}
if(j==n+1)break;
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]-1);
}
Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化的更多相关文章
- Educational Codeforces Round 10 D. Nested Segments 【树状数组区间更新 + 离散化 + stl】
任意门:http://codeforces.com/contest/652/problem/D D. Nested Segments time limit per test 2 seconds mem ...
- Educational Codeforces Round 10 D. Nested Segments
D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- CodeForces-652D:Nested Segments(树状数组+离散化)
You are given n segments on a line. There are no ends of some segments that coincide. For each segme ...
- Educational Codeforces Round 8 E. Zbazi in Zeydabad 树状数组
E. Zbazi in Zeydabad 题目连接: http://www.codeforces.com/contest/628/problem/D Description A tourist wan ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- Codeforces 220B - Little Elephant and Array 离线树状数组
This problem can be solve in simpler O(NsqrtN) solution, but I will describe O(NlogN) one. We will s ...
- Codeforces Round #401 (Div. 1) C(set+树状数组)
题意: 给出一个序列,给出一个k,要求给出一个划分方案,使得连续区间内不同的数不超过k个,问划分的最少区间个数,输出时将k=1~n的答案都输出 比赛的时候想的有点偏,然后写了个nlog^2n的做法,T ...
随机推荐
- 使用maven打包项目遇到错误: http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
今天在使用maven打包项目时遇到一个错误: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin ...
- nc-使用方法
nc-远程克隆硬盘 A 接收端: nc -lp 333 | dd of=/dev/sda #用nc开启333监听端口 将收到的数据 写入到sda的硬盘上(前提是挂一块硬盘) B ...
- Git和Github简单教程【转】
转自:https://www.cnblogs.com/schaepher/p/5561193.html#clone 原文链接:Git和Github简单教程 网络上关于Git和GitHub的教程不少,但 ...
- 64_n2
nodejs-from-0.1.3-4.fc26.noarch.rpm 11-Feb-2017 15:01 9982 nodejs-from2-2.1.0-6.fc26.noarch.rpm 11-F ...
- 008 BlockingQueue理解
原文https://www.cnblogs.com/WangHaiMing/p/8798709.html 本篇将详细介绍BlockingQueue,以下是涉及的主要内容: BlockingQueue的 ...
- 让MySql支持Emoji表情存储
java后台报错,如下. aused by: java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x84' for column ...
- (六)Spring4 整合Hibernate4,Struts2
第一节:S2SH 整合所需Jar 包 Struts2.3.16,Spring4.0.6,Hibernate4.3.5 整合所需jar 包: Struts2.3.16 jar 包 Spring4.0.6 ...
- 获取ios设备系统信息的方法 之 [UIDevice currentDevice]
获取iphone的系统信息使用[UIDevice currentDevice],信息如下: [[UIDevice currentDevice] systemName]:系统名称,如iPhone OS ...
- kali&BT安装好之后无法上网(包括Wifi)或者无法获得内网IP解决方法
大家都知道,要想进行内网渗透攻击,你必须要在那个内网里.但是大家在Vmware里安装kali的时候,大多数用户为了方便,未选择桥接模式,而是选择了使用与本机共享的IP网络当然,这样能上网,但是你的虚拟 ...
- Hive2.x 版本的安装及配置 以及要注意的事项
博主学习Hadoop学习到Hive,一开始跟着资料去安装Hive 1.x一点问题也没有,方便快捷啊,但是看了一下官方文档,上面好像说Hive 2.0修复了很多bug,那么我想,我还是用Hive2.0好 ...