Code Forces 652D Nested Segments(离散化+树状数组)
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
离散化+树状数组。
把所有区间按照右端点排序,然后统计左端点和右端点之间的已经包含的左端点个数,用树状数组求区间和会很快
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
#define MAX 2*100000
struct Node
{
int l,r;
int pos;
}a[MAX+5];
int n;
int num[2*MAX+5];
int c[2*MAX+5];
int ans[MAX+5];
int s;
int lowbit(int x)
{
return x&(-x);
}
void update(int x,int num)
{
while(x<=s)
{
c[x]+=num;
x+=lowbit(x);
}
}
int sum(int x)
{
int _sum=0;
while(x>0)
{
_sum+=c[x];
x-=lowbit(x);
}
return _sum;
}
int cmp(Node a,Node b)
{
return a.r<b.r;
}
int main()
{
scanf("%d",&n);
memset(c,0,sizeof(c));
int cnt=0;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&a[i].l,&a[i].r);
a[i].pos=i;
num[cnt++]=a[i].l;
num[cnt++]=a[i].r;
}
sort(num,num+cnt);
for(int i=1;i<=n;i++)
{
a[i].l=lower_bound(num,num+cnt,a[i].l)-num+1;
a[i].r=lower_bound(num,num+cnt,a[i].r)-num+1;
}
sort(a+1,a+n+1,cmp);
s=a[n].r;
for(int i=1;i<=n;i++)
{
int num=sum(a[i].r)-sum(a[i].l-1);
ans[a[i].pos]=num;
update(a[i].l,1);
}
for(int i=1;i<=n;i++)
{
printf("%d\n",ans[i]);
}
return 0;
}
Code Forces 652D Nested Segments(离散化+树状数组)的更多相关文章
- codeforces 652D Nested Segments 离散化+树状数组
题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- 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 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- CodeForces-652D:Nested Segments(树状数组+离散化)
You are given n segments on a line. There are no ends of some segments that coincide. For each segme ...
- D. Nested Segments(树状数组、离散化)
题目链接 参考博客 题意: 给n个线段,对于每个线段问它覆盖了多少个线段. 思路: 由于线段端点是在2e9范围内,所以要先离散化到2e5内(左右端点都离散化了,而且实际上离散化的范围是4e5),然后对 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
随机推荐
- MySQL 常用语法 之 UNION与UNION ALL
下面讲一下mySql中的union和union all 的功能以及区别 A union B 是将表A的数据和表B的数据连接成一张表,前提是查询的两张表的列数一定要一样否则出错. A union all ...
- Linux远程连接Windows桌面
Ubuntu对初始用户的界面友好是有目共睹的:Fedora一向以创新.傲慢的姿态示人.其实,对于两者,我虽然更倾向于选择Fedora,但不可避免地会两者比较,发现Ubuntu更加人性化,这点是经过很多 ...
- 【复杂】CentOS 6.4下PXE+Kickstart无人值守安装操作系统
一.简介 1.1 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持 ...
- JavaScript网页换肤
使网页背景颜色可选黄/粉 <!doctype html> <html> <head><title>网页换肤</title></head ...
- plsql developer中,清除登录历史
需求描述: 在使用plsql developer的时候,发现登录的时候,有太多的历史,想要把这些登录历史清除掉, 在此记录下. 操作过程: 1.登录plsql developer(或者在登录时取消也会 ...
- HTML&CSS精选笔记_HTML入门
HTML入门 什么是HTML HTML基本文档格式 HTML文档的基本格式,其中主要包括<!DOCTYPE>文档类型声明.<html>根标记.<head>头部标记. ...
- ios开发之--UICollectionView的使用
最近项目中需要实现一种布局,需要用到UICollectionView,特在此整理记录下! 贴上最终实现的效果图: 1,声明 @interface FirstViewController ()<U ...
- window 后台执行 redis(隐藏窗口)
方法是在知乎上看的,链接:https://www.zhihu.com/question/22771030 实现方法是利用一个vbe脚本去运行一个bat脚本,在bat脚本里启动exe软件 PS:要想启动 ...
- Python 使用正则表达式匹配URL网址
使用正则表达式匹配以 .com 或 .cn 为域名后缀的URL地址 In [1]: import re In [2]: str = "http://www.baidu.com/" ...
- WAS的部署
一.设置JAM参数 1.登录WAS的控制台 https://192.168.0.91:9043/ibm/console