前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Code Forces 652D Nested Segments(离散化+树状数组)

Code Forces 652D Nested Segments(离散化+树状数组)

作者头像
ShenduCC
发布2018-04-26 15:01:11
5790
发布2018-04-26 15:01:11
举报
文章被收录于专栏:算法修养算法修养

 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

代码语言:javascript
复制
4
1 8
2 3
4 7
5 6

output

代码语言:javascript
复制
3
0
1
0

input

代码语言:javascript
复制
3
3 4
1 5
2 6

output

代码语言:javascript
复制
0
1
1
代码语言:javascript
复制
代码语言:javascript
复制
代码语言:javascript
复制
离散化+树状数组。
代码语言:javascript
复制
把所有区间按照右端点排序,然后统计左端点和右端点之间的已经包含的左端点个数,用树状数组求区间和会很快
代码语言:javascript
复制
代码语言:javascript
复制
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>

using namespace std;
#define MAX 2*100000
struct Node
{
    int l,r;
    int pos;
}a[MAX+5];
int n;
int num[2*MAX+5];
int c[2*MAX+5];
int ans[MAX+5];
int s;
int lowbit(int x)
{
    return x&(-x);
}
void update(int x,int num)
{
    while(x<=s)
    {
        c[x]+=num;
        x+=lowbit(x);
    }
}
int sum(int x)
{
    int _sum=0;
    while(x>0)
    {
        _sum+=c[x];
        x-=lowbit(x);
    }
    return _sum;
}
int cmp(Node a,Node b)
{
    return a.r<b.r;
}
int main()
{
    scanf("%d",&n);
    memset(c,0,sizeof(c));
    int cnt=0;
    for(int i=1;i<=n;i++)
    {
        scanf("%d%d",&a[i].l,&a[i].r);
        a[i].pos=i;
        num[cnt++]=a[i].l;
        num[cnt++]=a[i].r;
    }
    sort(num,num+cnt);
    for(int i=1;i<=n;i++)
    {
        a[i].l=lower_bound(num,num+cnt,a[i].l)-num+1;
        a[i].r=lower_bound(num,num+cnt,a[i].r)-num+1;
    }
    sort(a+1,a+n+1,cmp);
    s=a[n].r;
    for(int i=1;i<=n;i++)
    {
        int num=sum(a[i].r)-sum(a[i].l-1);
        ans[a[i].pos]=num;
        update(a[i].l,1);
    }
    for(int i=1;i<=n;i++)
    {
        printf("%d\n",ans[i]);
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-03-29 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档