codeforces 652D D. Nested Segments(离散化+sort+树状数组)
题目链接:
2 seconds
256 megabytes
standard input
standard output
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.
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.
Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.
4
1 8
2 3
4 7
5 6
3
0
1
0
3
3 4
1 5
2 6
0
1
1 题意:给n个线段,对于每个线段问它覆盖了多少个线段;
思路:由于线段端点是在e9范围内,所以要先离散化到2e5内,只离散化一个端点即可,我离散化的是右端点,然后在对左端点排序(保证l[i]>=l[j]),然后再按顺序query右端点(保证r[i]<r[j])并update,就可以得到结果了,感觉线段树和树状数组真是丧心病狂;还有以前不会离散化,昨天睡觉的时候居然自己就领悟了,哈哈哈哈哈,开心;
AC代码:
/*2014300227 652D - 24 GNU C++11 Accepted 187 ms 5948 KB */
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
int n,sum[N],ans[N];
struct node
{
int l,r,id;
};
node qu[N];
bool cmp1(node x,node y)
{
if(x.r==y.r)return x.l<y.l;
return x.r<y.r;
}
bool cmp2(node x,node y)
{
if(x.l==y.l)return x.r<y.r;//注意此处的大小关系,因为我把右端点相等的离散化成不等的了;
return x.l>y.l;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
}
sort(qu+,qu+n+,cmp1);//按右端点排序以离散化
for(int i=;i<=n;i++)
{
qu[i].r=i;//离散化
}
sort(qu+,qu+n+,cmp2);
for(int i=;i<=n;i++)
{
ans[qu[i].id]=query(qu[i].r);
update(qu[i].r);
}
for(int i=;i<=n;i++)
{
printf("%d\n",ans[i]);
} return ;
}
codeforces 652D D. Nested Segments(离散化+sort+树状数组)的更多相关文章
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)
题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...
- Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)
E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- cf 61 E. Enemy is weak 离散化+树状数组
题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ...
- Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)
[题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...
- POJ 2299 Ultra-QuickSort (离散化)+【树状数组】
<题目链接> 题目大意: 给你一段序列,问你如果每次只交换该序列相邻的两个元素,最少需要交换多少步才能够使该序列变为升序排列. 解题分析: 不难发现,其实本题就是让我们求原始序列的逆序对, ...
- POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对
http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...
- CodeForces 380C Sereja and Brackets(扫描线+树状数组)
[题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...
- HDU 5862 Counting Intersections (离散化+扫描线+树状数组)
题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...
随机推荐
- Xampp 环境问题集合
1.不小心把虚拟机的环境删了,需要重新安装xmapp 安装很简单,但是重启:/opt/lampp/lampp restart 发现 XAMPP:"Another web server dae ...
- vue-router实现页面的整体跳转
直接看效果图: 代码地址:https://github.com/YalongYan/vue-router-jump
- Xcode8:"subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0" 的警告
运行xcode8遇到这个警告: subsystem: com.apple.UIKit, category: HIDEventFiltered, enable_level: 0, persist_lev ...
- static全局变量与普通的全局变量有什么区别?static局部变量和普通局部变量有什么区别?static函数与普通函数有什么区别?
答案:全局变量(外部变量)的说明之前再冠以static 就构成了静态的全局变量.全局变量本身就是静态存储方式,静态全局变量当然也是静态存储方式. 这两者在存储方式上并无不同.这两者的区别虽在于非静态全 ...
- Linux下安装 activemq 并指定jdk 1.8
1. 下载安装包 <apache-activemq-5.15.4-bin.tar.gz> 下载地址:https://pan.baidu.com/s/18xzjBAchjWqsHNA1HuY ...
- cocoapods最新使用
1.首先用淘宝的Ruby镜像来访问CocoaPods,打开终端输入以下命令: (1)gem sources --remove http://ruby.gems.org/ (移除现有Ruby默认源) ...
- Orthogonal Least Squares Learning Algorithm for Radial Basis Function Networks
Orthogonal Least Squares Learning Algorithm for Radial Basis Function Networks S. Chen, C. F. N. Cow ...
- mydql练习答案
.查询“生物”课程比“物理”课程成绩高的所有学生的学号: 思路: 获取所有有生物课程的人(学号,成绩) - 临时表 获取所有有物理课程的人(学号,成绩) - 临时表 根据[学号]连接两个临时表: 学号 ...
- linux c编程:线程创建
前面章节中介绍了进程.从这一章开始介绍线程.进程和线程的差别是什么呢: 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位. 线程是进程的一个实 ...
- linux c编程:系统数据文件和信息
linux系统相关的文件信息包含在/etc/passwd文件和/etc/group中.每次登录linux系统以及每次执行ls -l命令时都要使用口令文件.这些字段都包含在<pwd.h>中定 ...