codeforces 652D Nested Segments 离散化+树状数组
题意:给你若干个区间,询问每个区间包含几个其它区间
分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和
注:(都是套路)
#include<cstdio>
#include<cstring>
#include<queue>
#include<cstdlib>
#include<algorithm>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;
const int N=2e5+;
const int INF=0x3f3f3f3f;
int a[N<<],n,d;
struct pair{
int l,r,id;
bool operator<(const pair &e)const{
return r<e.r;
}
}p[N];
int c[N<<];
void change(int x){
for(int i=x;i<=d;i+=i&(-i))
++c[i];
}
int query(int x){
int ans=;
for(int i=x;i>;i-=i&(-i))
ans+=c[i];
return ans;
}
int res[N];
int main(){
scanf("%d",&n);
int tot=;
for(int i=;i<=n;++i){
scanf("%d%d",&p[i].l,&p[i].r),p[i].id=i;
a[++tot]=p[i].l;
a[++tot]=p[i].r;
}
sort(a+,a++tot);
d=;
for(int i=;i<=tot;++i)
if(a[i]!=a[i-])a[++d]=a[i];
sort(p+,p++n);
for(int i=;i<=n;++i){
int r=lower_bound(a+,a++d,p[i].r)-a;
int l=lower_bound(a+,a++d,p[i].l)-a;
res[p[i].id]=query(r)-query(l-);
change(l);
}
for(int i=;i<=n;++i)
printf("%d\n",res[i]);
return ;
}
codeforces 652D Nested Segments 离散化+树状数组的更多相关文章
- CF Educational Codeforces Round 10 D. Nested Segments 离散化+树状数组
题目链接:http://codeforces.com/problemset/problem/652/D 大意:给若干个线段,保证线段端点不重合,问每个线段内部包含了多少个线段. 方法是对所有线段的端点 ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化
D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...
- 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 ...
- 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),然后对 ...
- codeforces 652D . Nested Segments 线段树
题目链接 我们将线段按照右端点从小到大排序, 如果相同, 那么按照左端点从大到小排序. 然后对每一个l, 查询之前有多少个l比他大, 答案就是多少.因为之前的r都是比自己的r小的, 如果l还比自己大的 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
随机推荐
- itoa函数的递归实现(二级指针实现)
问题提出 <C Programming Language>书中在递归这一节预留了两个使用递归实现的函数,其中itoa函数是用来将一个整数转换为一个字符串.书中已有使用循环实现的版本,但是直 ...
- mysql create database 指定utf-8编码
CREATE DATABASE IF NOT EXISTS thinkphp DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
- CentOS 6.X更新Python2.7.x版本 安装pip
在安装新版之前安装 先安装bz2.zlib,执行下列代码进行安装 yum install -y zlib-devel bzip2-devel xz-libs wget openssl openssl- ...
- 响应式布局中重要的meta标签设置.适用于手机浏览器兼容性设置
<!-- #手机浏览器兼容性设置 --> <meta content="application/xhtml+xml;charset=UTF-8" http- ...
- Spring核心框架 - AOP的起源及介绍
一.AOP技术起源 AOP技术的诞生并不算晚,早在1990年开始,来自Xerox Palo Alto Research Lab(即PARC)的研究人员就对面向对象思想的局限性进行了分析.他们研究出了一 ...
- 我的PHP之旅--数据库连接MySQL服务器,添加 删除 查询
PHP连接MySQL服务器 连接MySQL的方法:mysql_connect(); 语法:resource $link = mysql_connect($hostname, $username, $p ...
- poj 2440 (找递推公式)
http://poj.org/problem?id=2440 DNA Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 3254 ...
- 【转载】Java 升级到jdk7后DbVisualizer 6 启动空指针的处理方案
将JDK从6升级到了7(或从其他电脑移植DBV文件夹后),每当启动DbVisualizer 6的时候都会报空指针异常 在官网上找到了相关的方案,如下: In the DbVisualizer inst ...
- oracle srvctl 命令
SRVCTL命令可以控制RAC数据库中的instance,listener以及services. 通常SRVCTL在ORACLE用户下执行.下面我们来介绍srvctl命令. 1.通过SRVCTL命令来 ...
- Oracle数据库安装完成之后的启动操作
由于是菜鸟,在 完成Oracle数据库的安装之后,不知道该怎么启动.在经过一番折腾之后明白了其中的一些道理,总结如下: 其实Oracle数据库和Mysql数据库的启动都是相同的原理. Mysql数据库 ...