codeforces_652D. Nested Segments(树状数组、二分)

本文介绍了一种解决线段包含问题的有效算法。该算法首先对线段的端点进行排序,然后利用二分查找和树状数组来计算每个线段内所包含的其他线段数量。通过这种方式,可以在较短时间内解决大规模数据集的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

将点排序后,用二分找出线段两个端点在a数组的位置,将线段的末尾端点标1,用树状数组求出区间和,最后再按原先顺序输出。


#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <stack>
#include <bitset>
#include <map>
#include <string>
#include <algorithm>
#define Si(a) scanf("%d",&a)
#define Sl(a) scanf("%lld",&a)
#define Sd(a) scanf("%lf",&a)
#define Ss(a) scanf("%s",a)
#define Pi(a) printf("%d\n",(a))
#define Pl(a) printf("%lld\n",(a))
#define Pd(a) printf("%lf\n",(a))
#define Ps(a) printf("%s\n",(a))
#define W(a) while(a--)
#define mem(a,b) memset(a,(b),sizeof(a))
#define inf 0x3f3f3f3f
#define maxn 200010
#define mod 1000000007
#define PI acos(-1.0)
#define LL long long
using namespace std;

struct node
{
    int l,r,ans,id;
}p[2*maxn];


int n,top=0;
int c[2*maxn];
int a[2*maxn];

bool cmp1(node a,node b)
{
    return a.l<b.l;
}


bool cmp2(node a,node b)
{
    return a.id<b.id;
}

int update(int i,int x)
{
    while(i<=2*n)
    {
        c[i]+=x;
        i+=(i&(-i));
    }
}

int Sum(int pos)
{
    int sum=0;
    while(pos>0)
    {
        sum+=c[pos];
        pos-=(pos&(-pos));
    }
    return sum;
}

int main()
{
    Si(n);
    mem(c,0);
    int i;
    for(i=1;i<=n;i++)
    {
        int l,r;
        Si(l);Si(r);
        a[top++]=l;
        a[top++]=r;
        p[i].l=l;
        p[i].r=r;
        p[i].id=i;
    }
    sort(a,a+top);
    for(i=1;i<=n;i++)
    {
        p[i].l=upper_bound(a,a+top,p[i].l)-a;
        p[i].r=upper_bound(a,a+top,p[i].r)-a;
    }
    sort(p+1,p+n+1,cmp1);
    for(i=1;i<=n;i++)update(p[i].r,1);
    for(i=1;i<=n;i++)
    {
        if(p[i].l+1>p[i].r-1)p[i].ans=0;
        else
        {
            p[i].ans=Sum(p[i].r-1)-Sum(p[i].l+1);
        }

        update(p[i].r,-1);
    }
    sort(p+1,p+n+1,cmp2);
    for(i=1;i<=n;i++)
    Pi(p[i].ans);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值