离散化+树状数组

先对坐标离散化,把每条线段结尾所在点标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. C# 实现屏幕键盘 (ScreenKeyboard)

    原文地址:http://www.cnblogs.com/youzai/archive/2008/05/19/1202732.html 要实现一个屏幕键盘,需要监听所有键盘事件,无论窗体是否被激活.因此 ...

  3. Oracle中清除BIN$开头的垃圾表的解决办法

    10g的新特性flashback闪回区 在10g中bin开头表示已经删除的放在回收站的表,oracle在删除表时并没有彻底的删除,而是把表放入回收站!purge recyclebin清空回收站即可. ...

  4. MyBatis中Like语句使用方式

    oracle数据库: SELECT * FROM user WHERE name like CONCAT('%',#{name},'%') 或 SELECT * FROM user WHERE nam ...

  5. 【转】使用ThinkPHP必须掌握的调试方法

    经常看到有人问到findAll的返回数据类型是什么之类的问题,以及出错了不知道什么原因的情况,其实还是没有熟悉ThinkPHP内置的调试手段和方法,抛开IDE本身自带的调试方式不说,如果你正在用或者打 ...

  6. CDockablePane使用总结

    基于 http://blog.csdn.net/kikaylee/article/details/8936953 CDockablePane的基本布局和用法 新建一个SDI工程,在CMainFrame ...

  7. 运行第一个SparkKPI程序

    1.复制一个examples中SparkPi.scala到IntelliJ IDEA编辑器,运行,出现错误: “org.apache.spark.SparkException: A master UR ...

  8. HDU2952:Counting Sheep(DFS)

    Counting Sheep Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Tota ...

  9. java 方法的重载的语法规则

    class People { float hello(int a,int b) { return a+b; } float hello(long a,int b) { return a-b; } do ...

  10. spark中groupByKey与reducByKey

    [译]避免使用GroupByKey Scala Spark 技术   by:leotse 原文:Avoid GroupByKey 译文 让我们来看两个wordcount的例子,一个使用了reduceB ...