离散化+树状数组

先对坐标离散化,把每条线段结尾所在点标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. windows下编译Android版本的boost库文件

    1.起因: 手上有一个用到了boost的asio库和thread库的工程要编译到手机上(Android版本和ios版本),本文只介绍如何编译到Android版本,ios版本之后再介绍,也许就不介绍了( ...

  2. @property(nonatomic) UIViewAutoresizing autoresizingMask;

    在 UIView 中有一个autoresizingMask的属性,它对应的是一个枚举的值(如下),属性的意思就是自动调整子控件与父控件中间的位置,宽高. 1 2 3 4 5 6 7 8 9 enum  ...

  3. 同步 异步 AJAX JS

    jQuery:$post.$get.$ajax与php,实现异步加载 什么是异步加载? 整个最通俗的说法就是将另外一个页面上的数据通过append() 或者 html()等函数插入到本页上.纯js写法 ...

  4. Swift 学习笔记 (一)

    原创: 转载请注明出处 Extention try catch rxSwift internal  public  private var  let as       as? 强转 ? ! didSe ...

  5. 河南多校大一训练赛 G 硬币

    题目链接:http://acm.hust.edu.cn/vjudge/contest/125004#problem/G 密码:acm Description 宇航员Bob有一天来到火星上,他有收集硬币 ...

  6. php-fpm配置优化

    PHP配置文件php-fpm的优化 2013/06/28 php, php-fpm 应用加速与性能调优 评论 6,029   本文所涉及的配置文件名为PHP-fpm.conf,里面比较重要的配置项有如 ...

  7. 理解用requireJs 来实现javascript的模块化加载

    这是我看到的一片关于requirejs的初学者的文章,写的不错,下面结合自己的理解记录一下: 原文:http://www.sitepoint.com/understanding-requirejs-f ...

  8. 使用compass更高效的编辑css --- 图片精灵

    compass是sass的一个库,关系相当于js中的jq.比较可惜的是compass的文档比较混乱 看的不太舒服,下面是compass的文档地址,有兴趣的可以去看看 http://compass-st ...

  9. 关于Eclipse无法生成class文件的问题

    今天调试东西的时候发现怎么都无法build 遂用Eclipse里的clean功能 打算重新编译一下结果所有的class文件全部消失了 重新打包发包也不行 经过查找后得到方法:把properties属性 ...

  10. (转)StringTokenizer类的使用

    StringTokenizer是一个用来分隔String的应用类,相当于VB的split函数.1.构造函数public StringTokenizer(String str)public String ...