离散化+树状数组

先对坐标离散化,把每条线段结尾所在点标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. C语言头文件

    最近在工作当中遇到了一点小问题,关于C语言头文件的应用问题,主要还是关于全局变量的定义和声明问题.学习C语言已经有好几年了,工作使用也近半年了,但是对于这部分的东西的确还没有深入的思考过.概念上还是比 ...

  2. Hibernate 系列教程14-继承-PerTable策略

    Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...

  3. iOS开发frame, contentSize, contentOffset, contentInset 区别联系浅析

    1. 概述 iOS开发中,必然会涉及到布局相关问题,frame,bounds,contenSize,contentOffset,contentInset这几个布局相关概念让许多初学者感到困惑.虽然初步 ...

  4. [转]理解SSL(https)中的对称加密与非对称加密

    加密 解密 Tweet   密码学最早可以追溯到古希腊罗马时代,那时的加密方法很简单:替换字母. 早期的密码学 古希腊人用一种叫 Scytale 的工具加密.更快的工具是 transposition ...

  5. JS-运动基础(一)

    <title>无标题文档</title> <style> #div1{width:200px;height:200px; background:red; posit ...

  6. QT修改默认的滚动条样式

    这几天写一个类似于悬浮窗的小程序,可是qt自带的滚动条实在难看,经过多番查找终于找到一个类似于qq聊天窗口的滚动条,废话不说上代码.希望能帮到大家 1.写入到文件中,新建个xx.qss,然后复制一下内 ...

  7. EDA 事件驱动框架

    事件代表过去发生的事件,事件既是技术架构概念,也是业务概念.以事件为驱动的编程模型称为事件驱动架构EDA. EDA是一种以事件为媒介,实现组件或服务之间最大松耦合的方式.传统面向接口编程是以接口为媒介 ...

  8. wordpress安装插件--su

     Add to Any: Subscribe Button 让读者方便的订阅你的博客到任何Feed阅读器 Google XML Sitemaps 生成完全兼容各大搜索引擎的Sitemaps/网站地图. ...

  9. jQuery validation

    之前做客户端验证感觉自己javascript 不行,虽然能写出来一完整的验证,但从不自信,一直觉得客户端验证是比较繁琐的事情,但是又不能不做,只到最开始接触ajax ,遇到了一个jQuery vali ...

  10. 交换机VLAN、 TRUNK 、VTP 配置

    交换机VLAN. TRUNK .VTP 配置 1. 配置 CISCO 二层交换机的IP 地址(catalyst 2950 为例) SW1(config)#int vlan 1 //进入管理接口inte ...