离散化+树状数组

先对坐标离散化,把每条线段结尾所在点标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的更多相关文章

  1. [离散化+树状数组]CodeForces - 652D Nested Segments

    Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. codeforces 652D Nested Segments 离散化+树状数组

    题意:给你若干个区间,询问每个区间包含几个其它区间 分析:区间范围比较大,然后离散化,按右端点排序,每次更新树状数组中的区间左端点,查询区间和 注:(都是套路) #include<cstdio& ...

  3. codeforces 652D . Nested Segments 线段树

    题目链接 我们将线段按照右端点从小到大排序, 如果相同, 那么按照左端点从大到小排序. 然后对每一个l, 查询之前有多少个l比他大, 答案就是多少.因为之前的r都是比自己的r小的, 如果l还比自己大的 ...

  4. Code Forces 652D Nested Segments(离散化+树状数组)

     Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. Codeforces 976C Nested Segments

    题面: 传送门 C. Nested Segments Input file: standard input Output file: standard output Time limit: 2 secon ...

  6. D - Nested Segments CodeForces - 652D (离散化+树桩数组)

    D - Nested Segments CodeForces - 652D You are given n segments on a line. There are no ends of some ...

  7. codeforces 652D D. Nested Segments(离散化+sort+树状数组)

    题目链接: D. Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. Educational Codeforces Round 10 D. Nested Segments 离线树状数组 离散化

    D. Nested Segments 题目连接: http://www.codeforces.com/contest/652/problem/D Description You are given n ...

  9. 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 ...

随机推荐

  1. 非常简单的oracle和mysql数据互传

    工具是navicat,我用的是Navicat Premium 10: 这个工具可以同时连接mysql和oracle,如图: 同时连接上这两个库之后 工具->数据传输 左边是数据源,右边是导入目标 ...

  2. Filter 解决web网页跳转乱码

    为什么采用filter实现了字符集的统一编码 问题: 为什么会有字符集编码的问题呢?对于Java Web应用,使用Tomcat容器获取和传递的参数(request.getParameter())默认是 ...

  3. OpenGL—Android 开机动画源码分析二

    引自http://blog.csdn.net/luoshengyang/article/details/7691321/ BootAnimation类的成员函数的实现比较长,我们分段来阅读: 第三个开 ...

  4. 什么是dtd文件,为什么需要

    DTD为英文Document Type Definition,中文意思为“文档类定义”.DTD肩负着两重任务:一方面它帮助你编写合法的代码,另一方面它让浏览器正确地显示器代码.也许你会问它们居然有这样 ...

  5. iOS项目架构 小谈

    层级结构,自底向上 持久层(File,Realm,SQLite)<==>网络层(相信每个公司都有自己的网络层吧)==>业务层(ViewModel)==>展示层(View,VC) ...

  6. windows程序设计(三)

    MFC所有封装类一共200多个,但在MFC的内部技术不只是简单的封装 MFC的内部总共有六大关键技术,架构起了整个MFC的开发平台 一.MFC的六大关键技术包括: a).MFC程序的初始化过程 b). ...

  7. js基础之数据类型

    一:JavaScript 数据类型:字符串.数字.布尔.数组.对象.Null.Undefined JavaScript 字符串;        var carname="Bill Gates ...

  8. openstack镜像如何在vmware 环境中运行

    1.云镜像文件下载地址: http://sahara-files.mirantis.com/sahara-juno-spark-1.0.0-ubuntu-14.04.qcow2(安装有sahara-s ...

  9. boost之词法解析器spirit

    摘要:解析器就是编译原理中的语言的词法分析器,可以按照文法规则提取字符或者单词.功能:接受扫描器的输入,并根据语法规则对输入流进行匹配,匹配成功后执行语义动作,进行输入数据的处理. C++ 程序员需要 ...

  10. HBase伪分布安装

    1把hbase-0.94.2-security.tar.gz复制到/usr/local 2 解压缩.重命名.设置环境变量 cd /usr/local tar -zxvf hbase--security ...