题目链接:

D. Nested Segments

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given n segments on a line. There are no ends of some segments that coincide. For each segment find the number of segments it contains.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of segments on a line.

Each of the next n lines contains two integers li and ri ( - 109 ≤ li < ri ≤ 109) — the coordinates of the left and the right ends of the i-th segment. It is guaranteed that there are no ends of some segments that coincide.

Output

Print n lines. The j-th of them should contain the only integer aj — the number of segments contained in the j-th segment.

Examples
input
4
1 8
2 3
4 7
5 6
output
3
0
1
0
input
3
3 4
1 5
2 6
output
0
1
1 题意:给n个线段,对于每个线段问它覆盖了多少个线段;
思路:由于线段端点是在e9范围内,所以要先离散化到2e5内,只离散化一个端点即可,我离散化的是右端点,然后在对左端点排序(保证l[i]>=l[j]),然后再按顺序query右端点(保证r[i]<r[j])并update,就可以得到结果了,感觉线段树和树状数组真是丧心病狂;还有以前不会离散化,昨天睡觉的时候居然自己就领悟了,哈哈哈哈哈,开心;
AC代码:
/*2014300227 652D - 24 GNU C++11 Accepted 187 ms 5948 KB */
#include <bits/stdc++.h>
using namespace std;
const int N=2e5+;
int n,sum[N],ans[N];
struct node
{
int l,r,id;
};
node qu[N];
bool cmp1(node x,node y)
{
if(x.r==y.r)return x.l<y.l;
return x.r<y.r;
}
bool cmp2(node x,node y)
{
if(x.l==y.l)return x.r<y.r;//注意此处的大小关系,因为我把右端点相等的离散化成不等的了;
return x.l>y.l;
}
int lowbit(int x)
{
return x&(-x);
}
void update(int x)
{
while(x<=n)
{
sum[x]++;
x+=lowbit(x);
}
}
int query(int x)
{
int s=;
while(x>)
{
s+=sum[x];
x-=lowbit(x);
}
return s;
}
int main()
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d%d",&qu[i].l,&qu[i].r);
qu[i].id=i;
}
sort(qu+,qu+n+,cmp1);//按右端点排序以离散化
for(int i=;i<=n;i++)
{
qu[i].r=i;//离散化
}
sort(qu+,qu+n+,cmp2);
for(int i=;i<=n;i++)
{
ans[qu[i].id]=query(qu[i].r);
update(qu[i].r);
}
for(int i=;i<=n;i++)
{
printf("%d\n",ans[i]);
} return ;
}

codeforces 652D D. Nested Segments(离散化+sort+树状数组)的更多相关文章

  1. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  2. CodeForces - 220B Little Elephant and Array (莫队+离散化 / 离线树状数组)

    题意:N个数,M个查询,求[Li,Ri]区间内出现次数等于其数值大小的数的个数. 分析:用莫队处理离线问题是一种解决方案.但ai的范围可达到1e9,所以需要离散化预处理.每次区间向外扩的更新的过程中, ...

  3. Codeforces 777E(离散化+dp+树状数组或线段树维护最大值)

    E. Hanoi Factory time limit per test 1 second memory limit per test 256 megabytes input standard inp ...

  4. cf 61 E. Enemy is weak 离散化+树状数组

    题意: 给出一个数组,数组的每一个元素都是不一样的,求出对于3个数组下标 i, j, k such that i < j < k and ai > aj > ak where ...

  5. Codeforces 703D Mishka and Interesting sum(树状数组+扫描线)

    [题目链接] http://codeforces.com/contest/703/problem/D [题目大意] 给出一个数列以及m个询问,每个询问要求求出[L,R]区间内出现次数为偶数的数的异或和 ...

  6. POJ 2299 Ultra-QuickSort (离散化)+【树状数组】

    <题目链接> 题目大意: 给你一段序列,问你如果每次只交换该序列相邻的两个元素,最少需要交换多少步才能够使该序列变为升序排列. 解题分析: 不难发现,其实本题就是让我们求原始序列的逆序对, ...

  7. POJ 2299 Ultra-QuickSort 离散化加树状数组求逆序对

    http://poj.org/problem?id=2299 题意:求逆序对 题解:用树状数组.每读入一个数x,另a[x]=1.那么a数列的前缀和s[x]即为x前面(或者说,再x之前读入)小于x的个数 ...

  8. CodeForces 380C Sereja and Brackets(扫描线+树状数组)

    [题目链接] http://codeforces.com/problemset/problem/380/C [题目大意] 给出一个括号序列,求区间内左右括号匹配的个数. [题解] 我们发现对于每个右括 ...

  9. HDU 5862 Counting Intersections (离散化+扫描线+树状数组)

    题意:给你若干个平行于坐标轴的,长度大于0的线段,且任意两个线段没有公共点,不会重合覆盖.问有多少个交点. 析:题意很明确,可是并不好做,可以先把平行与x轴和y轴的分开,然后把平行y轴的按y坐标从小到 ...

随机推荐

  1. sql exist 优化查询时间

    1.非exist,查询需要20多秒 2.使用exist后 3.表连接也能优化

  2. oracle中nvl()函数

    oracle中nvl()函数  oracle的nvl函数的使用方法 通过查询获得某个字段的合计值,假设这个值位null将给出一个预设的默认值  select nvl(sum(t.dwxhl),1) f ...

  3. diamond源码阅读-目录监控

    PathNode(Path)StandardWatchEventKind(WatchEvent)Watchable(WatchKey WatchService WatchEvent)WatchKey( ...

  4. 贝塞尔曲线与CAShapeLayer的关系以及Stroke动画

    1.贝塞尔曲线与CAShapeLayer的关系    1.1CAShapeLayer须要一个形状才干生效,贝塞尔曲线能够创建基于矢量的路径.进而能够给CAShapeLayer提供路径,路径会闭环.   ...

  5. javascript中区分鼠标单击和拖动事件

    在javascript中,一般的DOM元素如div,都有onmousedown.onmousemove.onmouseup这3个鼠标事件. <div id="div1" on ...

  6. 【BZOJ1000】A+B Problem ★BZOJ1000题达成★

    [BZOJ1000]A+B Problem Description 输入两个数字,输出它们之和 Input 一行两个数字A,B(0<=A,B<100) Output 输出这两个数字之和 S ...

  7. Python学习笔记(二)在线用pip下载第三方包

    根据他人的博客,可以发现pip也是可以离线安装已经下载好的包的,具体请参考<pip常用命令>.由于现在还没进展到那个程度,所以本次博客先记录一下我已经学会的东西. 1.pip基本用法 (1 ...

  8. org.apache.poi3.1.7 Excle并发批量导入导出

    org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...

  9. 九度OJ 1195:最长&最短文本 (搜索)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3144 解决:1156 题目描述: 输入多行字符串,请按照原文本中的顺序输出其中最短和最长的字符串,如果最短和最长的字符串不止一个,请全部输 ...

  10. python初学者总结

    学习python首先配置好工作环境,因为不同版本之间的python是不兼容了 原创:01coding.com win7安装环境过程: 1:下载python 建议下载两个不同版本官方已给出 https: ...