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.

Example

Input
  1. 4
    1 8
    2 3
    4 7
    5 6
Output
  1. 3
    0
    1
    0
Input
  1. 3
    3 4
    1 5
    2 6
Output
  1. 0
    1
    1

题意:现在X轴上,有N个线段,给出每条线段是左端点和右端点,求每条线段覆盖了多少其他的线段。

思路:按右端点排序,然后一条一条的纳入考虑。每考虑一条新的,查询之前考虑过的左端点大于当前左端点的,就是当前线段的答案。然后把当前线段的左端点插入树状数组。(因为右端点是有序的,只需要考虑左端点即可,而左端点只需要树状数组来维护区间和及查询)。

  1. #include<cstdio>
  2. #include<cstdlib>
  3. #include<iostream>
  4. #include<algorithm>
  5. using namespace std;
  6. const int maxn=;
  7. int sum[maxn],a[maxn],ans[maxn],N,tot;
  8. struct in
  9. {
  10. int x; int y; int id;
  11. friend bool operator <(in a,in b){
  12. if(a.y==b.y) return a.x>b.x; return a.y<b.y;
  13. }
  14. }s[maxn];
  15. void add(int x){
  16. while(x<=tot) {
  17. sum[x]++;
  18. x+=(-x)&x;
  19. }
  20. }
  21. int query(int x){
  22. int res=;
  23. while(x){
  24. res+=sum[x];
  25. x-=(-x)&x;
  26. } return res;
  27. }
  28. int main()
  29. {
  30. scanf("%d",&N);
  31. for(int i=;i<=N;i++) {
  32. scanf("%d%d",&s[i].x,&s[i].y);
  33. s[i].id=i;
  34. a[++tot]=s[i].x; a[++tot]=s[i].y;
  35. }
  36. sort(a+,a+tot+);
  37. tot=unique(a+,a+tot+)-(a+);
  38. sort(s+,s+N+);
  39. for(int i=;i<=N;i++){
  40. int tx=lower_bound(a+,a+tot+,s[i].x)-a;
  41. int ty=lower_bound(a+,a+tot+,s[i].y)-a;
  42. ans[s[i].id]=query(ty)-query(tx-);
  43. add(tx);
  44. }
  45. for(int i=;i<=N;i++) printf("%d\n",ans[i]);
  46. return ;
  47. }

CodeForces-652D:Nested Segments(树状数组+离散化)的更多相关文章

  1. Educational Codeforces Round 10 D. Nested Segments (树状数组)

    题目链接:http://codeforces.com/problemset/problem/652/D 给你n个不同的区间,L或者R不会出现相同的数字,问你每一个区间包含多少个区间. 我是先把每个区间 ...

  2. hdu4605 树状数组+离散化+dfs

    Magic Ball Game Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) ...

  3. BZOJ_5055_膜法师_树状数组+离散化

    BZOJ_5055_膜法师_树状数组+离散化 Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然 ...

  4. POJ 2299 【树状数组 离散化】

    题目链接:POJ 2299 Ultra-QuickSort Description In this problem, you have to analyze a particular sorting ...

  5. [Codeforces 1208D]Restore Permutation (树状数组)

    [Codeforces 1208D]Restore Permutation (树状数组) 题面 有一个长度为n的排列a.对于每个元素i,\(s_i\)表示\(\sum_{j=1,a_j<a_i} ...

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

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

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

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

  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. codeforces 1042D - Petya and Array【树状数组+离散化】

    题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...

随机推荐

  1. 【RMAN】RMAN跨版本恢复(下)--大版本异机恢复

    [RMAN]RMAN跨版本恢复(下)--大版本异机恢复 BLOG文档结构图 ORACLE_SID=ORA1024G 关于10g的跨小版本恢复参考:http://blog.chinaunix.net/u ...

  2. 小米自动砸蛋机器js代码

    02 //地址:http://static.xiaomi.cn/515 03 //@author:liuzh 04 //@url:http://blog.csdn.net/isea533 05 var ...

  3. isinstance()和issubclass()

    内置函数中有个两个函数经常用到 isinstance()                    对象 是否是 类 的一个对象 from collections import Iterable prin ...

  4. python学习之-- RabbitMQ 消息队列

    记录:异步网络框架:twisted学习参考:www.cnblogs.com/alex3714/articles/5248247.html RabbitMQ 模块 <消息队列> 先说明:py ...

  5. HDU 6311 最少路径覆盖边集 欧拉路径

    Cover Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  6. Topcoder 2015_1C

    A:大水题; B:求一颗树中,有多少条路径 不存在路径中一点是另外一点的祖先,(后面废话说了很多) 其实一个点 可以到它本身也可以是一条路径结论是:统计叶子的节点.(真简单粗暴 C:题目不说,说也说不 ...

  7. OpenWrt 安装python-sqlite3失败

    https://dev.openwrt.org/ticket/12239 #12239 reopened defect Sqlite3 missing in python 汇报人: dgspai@- ...

  8. 【APUE】进程基础

    进程标识符:非负整数 ID为0的进程通常是是调度进程,常被称为交换进程.该进程是内核的一部分,它并不执行任何磁盘上的程序,因此也被称为系统进程 ID为1的进程是init进程,在自举过程结束时由内核调用 ...

  9. 【APUE】文件I/O

    Linux的内核将所有外部设备都可以看做一个文件来操作.那么我们对与外部设备的操作都可以看做对文件进行操作.我们对一个文件的读写,都通过调用内核提供的系统调用:内核给我们返回一个file descri ...

  10. Puppet基于Master/Agent模式实现LNMP平台部署

    前言 随着IT行业的迅猛发展,传统的运维方式靠大量人力比较吃力,运维人员面对日益增长的服务器和运维工作,不得不把很多重复的.繁琐的工作利用自动化处理.前期我们介绍了运维自动化工具ansible的简单应 ...