Nested Ranges Count

题目描述

Given n ranges, your task is to count for each range how many other ranges it contains and how many other ranges contain it.

Range [a,b] contains range [c,d] if a ≤ c and d ≤ b.

输入

The first input line has an integer n(1 ≤ n ≤ 2*1e5): the number of ranges.

After this, there are n lines that describe the ranges. Each line has two integers x and y(1 ≤ x < y ≤ 1e9): the range is [x,y].

You may assume that no range appears more than once in the input.

输出

First print a line that describes for each range (in the input order) how many other ranges it contains.

Then print a line that describes for each range (in the input order) how many other ranges contain it.

样例输入

4

1 6

2 4

4 8

3 6

样例输出

2 0 0 0

0 1 0 1

题目大意:n个区间,判断每个区间包含了多少个区间,以及每个区间被多少个区间包含

思路:一个区间 [l,r] 包含其他区间,意味着它的 l 要更小,r 要更大,按照 l 升序,r 降序进行排序,排序后,每个区间右边的,都要比它的 l 小,只需要找它右边有多少个区间的 r 比它大即可,我们可以考虑逆序遍历,先处理右边的,将每个区间的 r 记下来,最后输出值在1到a[i].r之间的数量,这个过程可以用树状数组维护。但由于 r 达到了1e9,个数只有2e5,离散化处理即可。

代码

cpp 复制代码
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
const int N=200010;
int tr[N];
int n,m;
int containing[N],contained[N];
struct Range{
    int l,r,idx;
}a[N];
int lowbit(int x){
    return x&-x;
}
void update(int k,int x){
    for (int i=k;i<=n;i+=lowbit(i)){
        tr[i]+=x;
    }
}
int query(int x){
    int res=0;
    for(int i=x;i>=1;i-=lowbit(i)){
        res+=tr[i];
    }
    return res;
}
int main() {
    ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>n;
    vector<int>alls;
    for (int i=1;i<=n;i++){
        cin>>a[i].l>>a[i].r;
        a[i].idx=i;
        alls.push_back(a[i].r);
    }
    sort(alls.begin(),alls.end());
    alls.erase(unique(alls.begin(),alls.end()),alls.end());
    m=alls.size();
    sort(a+1,a+n+1,[](const struct Range &x,const struct Range &y){
        if(x.l==y.l)
        return x.r>y.r;
        return x.l<y.l;
    });
    for (int i=n;i>=1;i--){
        int t=lower_bound(alls.begin(),alls.end(),a[i].r)-alls.begin()+1;
        containing[a[i].idx]=query(t);
        update(t,1);
    }
    memset(tr,0,sizeof(tr));
    for (int i=1;i<=n;i++){
        int t=lower_bound(alls.begin(),alls.end(),a[i].r)-alls.begin()+1;
        contained[a[i].idx]=query(m)-query(t-1);
        update(t,1);
    }
    for (int i=1;i<=n;i++)
    cout<<containing[i]<<' ';
    cout<<"\n";
    for (int i=1;i<=n;i++)
    cout<<contained[i]<<' ';
    return 0;
}
相关推荐
草履虫建模17 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
naruto_lnq19 小时前
分布式系统安全通信
开发语言·c++·算法
Jasmine_llq19 小时前
《P3157 [CQOI2011] 动态逆序对》
算法·cdq 分治·动态问题静态化+双向偏序统计·树状数组(高效统计元素大小关系·排序算法(预处理偏序和时间戳)·前缀和(合并单个贡献为总逆序对·动态问题静态化
爱吃rabbit的mq20 小时前
第09章:随机森林:集成学习的威力
算法·随机森林·集成学习
(❁´◡`❁)Jimmy(❁´◡`❁)21 小时前
Exgcd 学习笔记
笔记·学习·算法
YYuCChi21 小时前
代码随想录算法训练营第三十七天 | 52.携带研究材料(卡码网)、518.零钱兑换||、377.组合总和IV、57.爬楼梯(卡码网)
算法·动态规划
不能隔夜的咖喱21 小时前
牛客网刷题(2)
java·开发语言·算法
VT.馒头21 小时前
【力扣】2721. 并行执行异步函数
前端·javascript·算法·leetcode·typescript
进击的小头1 天前
实战案例:51单片机低功耗场景下的简易滤波实现
c语言·单片机·算法·51单片机
咖丨喱1 天前
IP校验和算法解析与实现
网络·tcp/ip·算法