洛谷U525376 信号干扰 (判断多个区间是否有重叠)

U525376信号干扰

题目描述

n n n 座信号塔,第 i i i 座信号塔的信号将覆盖区间 [ l i , r i ] [l_i,r_i] [li,ri]

若某个点被超过一座信号塔的信号覆盖,则在该点会产生信号干扰。

对于信号塔区间 [ a , b ] [a,b] [a,b],若建造这些信号塔不会产生信号干扰,则称其为无干扰区间。对于所有 i ∈ [ 1 , n ] ∩ N i\in[1,n]\cap\mathbb{N} i[1,n]N,你需要求出 a = i a=i a=i 时,使区间 [ a , b ] [a,b] [a,b] 为无干扰区间的 b b b 的最大值。

输入格式

第一行一个正整数 n n n,表示信号塔数量。

接下来 n n n 行,每行两个正整数 l i , r i l_i,r_i li,ri,表示信号塔的信号范围。

输出格式

输出一行 n n n 个整数,第 i i i 个正整数表示当 a = i a=i a=i 时,使区间 [ a , b ] [a,b] [a,b] 为无干扰区间的 b b b 的最大值。

样例 #1

样例输入 #1

7
1 1
1 1000
1 3
3 3
4 6
2 3
1 1

样例输出 #1

1 2 3 5 7 7 7

提示

1 ≤ n ≤ 2 × 1 0 5 1\le n\le2\times10^5 1n2×105 1 ≤ l i ≤ r i ≤ 1 0 9 1\le l_i\le r_i\le10^9 1liri109

在本题中,约定区间的左右端点可以相等。

思路

定义结构体node储存区间,重载bool operator<(const nd& other)const{return r<other.l;}
然后用一个set<node>存区间,区间在其中自动排序,对于一个新区间aset中的区间都不重合,则有s.find(a) == s.end() 成立,否则说明aset中存的区间有重合部分。
原理大概是:如果b是与set中与a有重合部分的最左侧的区间,那么find在判断时会发现a不小于b,b不小于a,则认为a等于b,即找到目标,就会返回b的迭代器而不是end();

代码:

#include <bits/stdc++.h>
#define endl '\n'
#define int long long
typedef long long ll;
using namespace std;

struct nd{
    int l,r;
    nd(int L=0,int R=0){l=L,r=R;}
    bool operator<(const nd& other)const{
        return r<other.l;
    }
}a[200005];
set<nd> s;

signed main() {
    cin.tie(0)->ios::sync_with_stdio(0);
    int n;
    cin>>n;
    for(int i=1;i<=n;i++){
        int l,r;
        cin>>l>>r;
        a[i]=nd(l,r);
    }
    int cnt = 0;
    for(int i=1;i<=n;i++){
        while(cnt<n && s.find(a[cnt+1]) == s.end()){
            s.insert(a[cnt+1]);cnt++;
        }
        cout<<cnt<<" ";s.erase(a[i]);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值