CodeForces-652D:Nested Segments(树状数组+离散化)
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.
Example
4
1 8
2 3
4 7
5 6
3
0
1
0
3
3 4
1 5
2 6
0
1
1
题意:现在X轴上,有N个线段,给出每条线段是左端点和右端点,求每条线段覆盖了多少其他的线段。
思路:按右端点排序,然后一条一条的纳入考虑。每考虑一条新的,查询之前考虑过的左端点大于当前左端点的,就是当前线段的答案。然后把当前线段的左端点插入树状数组。(因为右端点是有序的,只需要考虑左端点即可,而左端点只需要树状数组来维护区间和及查询)。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
int sum[maxn],a[maxn],ans[maxn],N,tot;
struct in
{
int x; int y; int id;
friend bool operator <(in a,in b){
if(a.y==b.y) return a.x>b.x; return a.y<b.y;
}
}s[maxn];
void add(int x){
while(x<=tot) {
sum[x]++;
x+=(-x)&x;
}
}
int query(int x){
int res=;
while(x){
res+=sum[x];
x-=(-x)&x;
} return res;
}
int main()
{
scanf("%d",&N);
for(int i=;i<=N;i++) {
scanf("%d%d",&s[i].x,&s[i].y);
s[i].id=i;
a[++tot]=s[i].x; a[++tot]=s[i].y;
}
sort(a+,a+tot+);
tot=unique(a+,a+tot+)-(a+);
sort(s+,s+N+);
for(int i=;i<=N;i++){
int tx=lower_bound(a+,a+tot+,s[i].x)-a;
int ty=lower_bound(a+,a+tot+,s[i].y)-a;
ans[s[i].id]=query(ty)-query(tx-);
add(tx);
}
for(int i=;i<=N;i++) printf("%d\n",ans[i]);
return ;
}
CodeForces-652D:Nested Segments(树状数组+离散化)的更多相关文章
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...
- hdu4605 树状数组+离散化+dfs
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- BZOJ_5055_膜法师_树状数组+离散化
BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...
- POJ 2299 【树状数组 离散化】
题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...
- [Codeforces 1208D]Restore Permutation (树状数组)
[Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...
- [离散化+树状数组]CodeForces - 652D Nested Segments
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...
- codeforces 652D Nested Segments 离散化+树状数组
题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- codeforces 1042D - Petya and Array【树状数组+离散化】
题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...
随机推荐
- C#排序1(冒泡排序、直接排序、快速排序)
冒泡排序:就是两个两个的这个比较好理解,代码也比较好写出来. 它的原理就是相邻的两个两个的比较,如果前面的数比后面的大,那么交换,它这个在比较完一次的时候可以得到最大的一个数,然后接着循环,每次外循环 ...
- 关于系统中使用多个PropertyPlaceholderConfigurer的配置
多数的鲜为人知方法都是因为有着罕见的应用,就比如说Spring中PropertyPlaceholderConfigurer这个类,它是用来解析Java Properties属性文件值,并提供在spri ...
- 【linux】ls与ll区别
ll是一个事先被定义好的别名(alias).别名就是赋予一条命令或者一列命令的名称.可以将别名作为缩写的同义词.在我的Ubuntu系统上,~/.bashrc文件中有这么一条语句alias ll='ls ...
- Android操作系统架构
Android操作系统架构 Android操作系统整体应用架构 Android系统架构和一些普遍的操作系统差不多,都是采用了分层的架构,从他们之间的架构图看,Android系统架构分为四个层,从高 ...
- Python()- 面向对象的组合用法
面向对象的组合用法 一个类中以另一个类的对象作为数据属性(一个类中引用另一个类的对象)一种 "有" 的关系: 比如:定义 1个人类 & 1个武器类 然后 张三 有 枪 李四 ...
- strcpy c标准库函数
C语言标准库函数strcpy,把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间. 已知strcpy函数的原型是: char *strcpy(char *dst, const ...
- navicat 无法连接到腾讯云Mysql
远程连接进入服务器 远程连接进入服务器之后,输入命令mysql -u root -p 之后输入mysql密码,进入mysql 命令环境 设置开启远程登录 GRANT ALL PRIVILEGES ON ...
- git服务端安装
Windows平台下Git服务器搭建 第一步:下载Java,下载地址:http://www.java.com/zh_CN/ 注意要下载是完整的JDK(其中有jre) 第二步:安装Java.安装步骤不 ...
- C++设计模式之适配器模式(二)
3.Socket网络通信的设计与实现------类适配器 除了对象适配器模式之外.适配器模式另一种形式.那就是类适配器模式,类适配器模式和对象适配器模式最大的差别在于适配器和适配者之间的关系不同,对象 ...
- Ulua_toLua_基本案例(一)
Ulua_toLua_基本案例 在Untiy中用Lua.必需要LuaInterface.LuaInterface的介绍请看:点击打开链接 能够先光写Lua,生成.lua的纯文件.再Unity中通过,l ...