一个关于区间的题目

题目:

给两个区间序列A和B,分别排序好了,比如([1,4],[6,8],[10,17]) 和([4,6],[7,9],[12,20]),求A-B的区间

 

输入:第一行为A和B中区间个数,后面为他们的区间

2 2
1 3
5 7
2 4
6 9

 

输出:A-B区间

1 2

5 6

 

程序:

#include <iostream>
#include <deque>
using namespace std;

class Seg{
public:
    int s,e;
    Seg(int x,int y):s(x),e(y){}
};


int main(){
    deque<Seg>    AA,BB,out;
    int na,nb;
    cin >> na >> nb;
    for(int i=0; i < na ; ++i){
        int s ,e;   
        cin >> s >> e;
        AA.push_back(* (new Seg(s,e)));
    }
    for(int i=0; i < nb ; ++i){
        int s ,e;   
        cin >> s >> e;
        BB.push_back(* (new Seg(s,e)));   
    }
    while(!AA.empty() && !BB.empty()){
        if(AA[0].s < BB[0].s){
            if(AA[0].e < BB[0].s){
                out.push_back(AA[0]);   
                AA.pop_front();
            }else if(AA[0].e <= BB[0].e){
                out.push_back(*(new Seg(AA[0].s, BB[0].s)));   
                AA.pop_front();   
            }else{
                out.push_back(*(new Seg(AA[0].s, BB[0].s)));   
                AA[0].s = BB[0].e;
                BB.pop_front();
            }
        }else if(AA[0].s > BB[0].s){
            if(BB[0].e <= AA[0].s){   
                BB.pop_front();
            }else if(BB[0].e < BB[0].e){
                AA[0].s = BB[0].e;
                BB.pop_front();   
            }else{
                AA.pop_front();
            }
        }else if(AA[0].e < BB[0].e){
            AA.pop_front();
        }else if(AA[0].e = BB[0].e){
            AA.pop_front();
            BB.pop_front();
        }else{
            AA[0].s = BB[0].e;
            BB.pop_front();
        }
    }
    for(int i = 0; i < out.size(); ++i){
        cout << out[i].s << " " << out[i].e << endl;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值