CodeForces 652D Nested Segments
离散化+树状数组
先对坐标离散化,把每条线段结尾所在点标1,
询问某条线段内有几条线段的时候,只需询问这段区间的和是多少,询问结束之后再把这条线段尾部所在点标为0
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<algorithm>
using namespace std; const int maxn=*1e5+;
struct X
{
int x,y,ans,id;
}p[*maxn];
int n;
int lsh[*maxn],tot;
int c[*maxn]; bool cmp(const X&a,const X&b)
{
return a.x<b.x;
} bool cmp2(const X&a,const X&b)
{
return a.id<b.id;
} int lowbit(int x)
{
return x&(-x);
} void update(int pos,int val)
{
while(pos<=*n)
{
c[pos]=c[pos]+val;
pos=pos+lowbit(pos);
}
} int getsum(int pos)
{
int res=;
while(pos>)
{
res=res+c[pos];
pos=pos-lowbit(pos);
}
return res;
} int get(int num)
{
int l=,r=tot-;
while(l<=r)
{
int mid=(l+r)/;
if(lsh[mid]<=num)
{
if(lsh[mid]==num) return mid+;
else l=mid+;
}
else r=mid-;
}
} int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
int li,ri; scanf("%d%d",&li,&ri);
lsh[tot++]=li; lsh[tot++]=ri;
p[i].x=li; p[i].y=ri; p[i].id=i;
}
sort(lsh,lsh+tot);
for(int i=;i<=n;i++)
{
p[i].x=get(p[i].x);
p[i].y=get(p[i].y);
}
sort(p+,p++n,cmp);
memset(c,,sizeof c);
for(int i=;i<=n;i++) update(p[i].y,); for(int i=;i<=n;i++)
{
if(p[i].x+>p[i].y-) p[i].ans=;
else p[i].ans=getsum(p[i].y-)-getsum(p[i].x+);
update(p[i].y,-);
}
sort(p+,p++n,cmp2);
for(int i=;i<=n;i++) printf("%d\n",p[i].ans); return ;
}
CodeForces 652D Nested Segments的更多相关文章
- [离散化+树状数组]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& ...
- 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 ...
- Codeforces 976C Nested Segments
题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...
- D - Nested Segments CodeForces - 652D (离散化+树桩数组)
D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...
- codeforces 652D D. Nested Segments(离散化+sort+树状数组)
题目链接: D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 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 ...
随机推荐
- linux的学习系列 2--文件系统
Linux中的所有数据都被保存在文件中,所有的文件被分配到不同的目录.目录是一种类似于树的结构,称为文件系统. 当你使用Linux时,大部分时间都会和文件打交道,通过本节可以了解基本的文件操作,如创建 ...
- java内部类继承--构造函数传参
/: innerclasses/InheritInner.java // Inheriting an inner class. class WithInner { class Inner {} } / ...
- MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出
先说下本人是菜鸟,通过网上资料学习,终于调通了MonkeyRunner 实现自动点击截屏后与本地图库进行对比输出,以后做静态UI测试就不需要眼睛盯着看图了,这一切交给MonkeyRunner了. 首先 ...
- 最简单的epoll的使用范例 : 监听 标准输入 ,并将数据回显到终端
#include<stdio.h>#include<stdlib.h>#include<unistd.h>#include<sys/epoll.h> # ...
- 利用未文档化API:RtlGetNtVersionNumbers 获取系统版本号
问题一:Windows SDK 8.1版本中的VersionHelper.h文件当中没有IsWindows10ORGreater,所以当你用IsWindows8Point1ORGreater判断出版本 ...
- PAT1001
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B Calculate a + b and output the sum in standard format 计算a+b ...
- 尝试在数据库 5 中提取逻辑页 (1:1640) 失败。该逻辑页属于分配单元XXX ,而非 XXX。
此信息表明数据库或表 已经部分损坏可以通过以下步骤尝试修复: 1. DBCC CHECKDB 重启服务器后,在没有进行任何操作的情况下,在SQL查询分析器中执行以下SQL进行数据库的修复,修复数据库存 ...
- 고서--做完A之后做B, B受A影响
1. 합격 소식을 듣고서 매우 기뻤어요.. 2. 친구하고 심하게 다투고서 마음이 안 좋았어요. 3. 급한 일을 먼저 끝내고서 이야기합시다.' 4. 창문을 열고서 상쾌한 공기를 마서 ...
- 转 Linux进程状态分析
众所周知,现在的分时操作系统能够在一个CPU上运行多个程序,让这些程序表面上看起来是在同时运行的.linux就是这样的一个操作系统.在linux系统中,每个被运行的程序实例对应一个或多个进程.l ...
- n皇后问题<dfs>
n皇后问题指的是: n*n的国际象棋棋盘上摆放n个皇后,使其不能互相攻击, 即任意两个皇后都不能处于同一行.同一列或同一斜线上, 问有多少种摆法. 和一般n皇后问题不同的是,现在棋盘上有可能已经放了一 ...