Educational Codeforces Round 10 D.Nested Segments
2016-03-29 16:10
232 查看
D. Nested Segments
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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.
Examples
input
output
input
output
题意:给你n条线段(n<=2*10^5),每条线段一个[l,r],代表他的左右区间,输出对于每条线段,有多少条线段能被他完全覆盖。
思路:对r进行离散化,然后按L对查询从大到小进行排序,对每个r查询小于等于他的r有多少个
(树状数组求前缀和)
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=200100;
int C[maxn],Hash[maxn*2],ans[maxn];
struct Point{
int x,y,id;
}a[maxn];
int n;
bool cmp(Point u,Point v){
if(u.x!=v.x)
return u.x>v.x;
return u.y>v.y;
}
void add(int x){
while(x<=n){
C[x]+=1;
x+=(x&-x);
}
}
int sum(int x){
int ret=0;
while(x>0){
ret+=C[x];
x-=(x&-x);
}
return ret;
}
int main(){
int tot=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id=i;
Hash[++tot]=a[i].y;
}
sort(Hash+1,Hash+tot+1);
int m=unique(Hash+1,Hash+tot+1)-Hash;
for(int i=1;i<=n;i++)
a[i].y=lower_bound(Hash+1,Hash+m,a[i].y)-Hash;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
ans[a[i].id]=sum(a[i].y);
add(a[i].y);
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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.
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.
Examples
input
4 1 8 2 3 4 7 5 6
output
3 0 1 0
input
3 3 4 1 5 2 6
output
0 1 1
题意:给你n条线段(n<=2*10^5),每条线段一个[l,r],代表他的左右区间,输出对于每条线段,有多少条线段能被他完全覆盖。
思路:对r进行离散化,然后按L对查询从大到小进行排序,对每个r查询小于等于他的r有多少个
(树状数组求前缀和)
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <cmath>
#include <ctime>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define inf -0x3f3f3f3f
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a, b) memset(a, b, sizeof(a))
typedef __int64 ll;
const int maxn=200100;
int C[maxn],Hash[maxn*2],ans[maxn];
struct Point{
int x,y,id;
}a[maxn];
int n;
bool cmp(Point u,Point v){
if(u.x!=v.x)
return u.x>v.x;
return u.y>v.y;
}
void add(int x){
while(x<=n){
C[x]+=1;
x+=(x&-x);
}
}
int sum(int x){
int ret=0;
while(x>0){
ret+=C[x];
x-=(x&-x);
}
return ret;
}
int main(){
int tot=0;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&a[i].x,&a[i].y);
a[i].id=i;
Hash[++tot]=a[i].y;
}
sort(Hash+1,Hash+tot+1);
int m=unique(Hash+1,Hash+tot+1)-Hash;
for(int i=1;i<=n;i++)
a[i].y=lower_bound(Hash+1,Hash+m,a[i].y)-Hash;
sort(a+1,a+n+1,cmp);
for(int i=1;i<=n;i++){
ans[a[i].id]=sum(a[i].y);
add(a[i].y);
}
for(int i=1;i<=n;i++)
printf("%d\n",ans[i]);
return 0;
}
相关文章推荐
- iOS自适应布局之Masonry(一)
- ceil
- 2016年上半年系统集成中项3月28日作业
- 各种类型数据的转换+Unix时间戳转换+文本在线diff
- Mysql学习总结(14)——Mysql主从复制配置
- blufr 算法的理解---理解的不对的,欢迎拍砖
- 关于意义这个东西
- Git 读书笔记(二)
- 串口通信概念基础
- Mysql学习总结(14)——Mysql主从复制配置
- HTML5表单属性(四)
- oracle 导出指定表
- POJ 2828 Buy Tickets(多校连萌,线段树模拟插入)
- 清空NSMutableArray所有内容
- Mac系统清理 使用CleanMyMac最简便
- Swift中In-Out类型的参数
- 相机或相册选择图片
- Linux 防火墙 iptables基本操作
- Java学习笔记-eclipse配置
- 什么是 Web API