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的关系 ...
随机推荐
- CodeForces 20 A+B
A - BerOS file system 水题不解释了,压缩斜杆.要注意最后没有斜杠. char a[105]; ...
- Problem 2121 神庙逃亡(FZU)
Problem 2121 神庙逃亡 Accept: 700 Submit: 1788 Time Limit: 1000 mSec Memory Limit : 32768 KB Prob ...
- [Docker]容器镜像
1.rootfs的基础知识 Mount namespaces 隔离的是文件系统挂接点,它使每个容器能看到不同的文件系统层次结构,即每当创建一个新容器时,希望容器进程看到的文件系统时一个独立的隔离环境 ...
- 生产环境下lnmp的权限说明
https://www.cnblogs.com/zrp2013/p/4183546.html 有关权限说明:-rwxrw-r‐-1 root root 1213 Feb 2 09:39 50.html ...
- BZOJ 1924: [Sdoi2010]所驼门王的宝藏 【tarjan】
Description 在宽广的非洲荒漠中,生活着一群勤劳勇敢的羊驼家族.被族人恭称为“先 知”的Alpaca L. Sotomon 是这个家族的领袖,外人也称其为“所驼门王”.所 驼门王毕生致力于维 ...
- hdu4115:Eliminate the Conflict
n<=10000局剪刀石头布,对面第i局出Ai,m<=10000种对你出什么提出的要求:Xi Yi Wi 表示第Xi局和第Yi局,Wi=1:必须不同:Wi=0:必须相同,问是否存在你一局都 ...
- ES6__变量的解构赋值
/* 变量的解构赋值 */ /* 基本概念 : 本质上就是一种匹配模式,只要等号两边的模式相同,那么左边的变量就可以被赋予对应的值. 结构赋值主要分为: 1. 数组的解构赋值 2. 对象的结构赋值 3 ...
- 毕业bg--hdu1881(01背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1881 01 背包 先按发起人离开的时间从小到大排序 然后再套01背包的模板 #include <iost ...
- 一次使用NodeJS实现网页爬虫记
前言 几个月之前,有同事找我要PHP CI框架写的OA系统.他跟我说,他需要学习PHP CI框架,我建议他学习大牛写的国产优秀框架QeePHP. 我上QeePHP官网,发现官方网站打不开了,GOOGL ...
- iOS中MRC和ARC混编
1. 在targets的build phases选项下Compile Sources下选择,不使用arc编译的文件.双击它.输入 -fno-objc-arc 就可以(这个类就能够使用MRC模式) 2. ...