区间合并板子

区间合并板子

#include <bits/stdc++.h>
using namespace std;

const int N = 100010;

typedef pair<int, int> PII;
PII a[N];
int n;
vector<PII> ans;

signed main(){
    scanf("%d", &n);
    for(int i = 0; i < n; ++i){
        scanf("%d%d", &a[i].first, &a[i].second);
    }
    
    sort(a, a + n);
    int res = 0;
    int st = -2e9, ed = -2e9;
    for(int i = 0; i < n; ++i){
        if(ed < a[i].first){
            ++res; 
            if(st != -2e9) ans.push_back({st, ed});
            st = a[i].first; ed = a[i].second;
        }
        else ed = max(ed, a[i].second);
    }
    printf("%d\n", res);
    if(st != -2e9) ans.push_back({st, ed});
    // for(auto item: ans){
    //     printf("%d %d\n", item.first, item.second);
    // }
    
    return 0;
}
posted @ 2025-04-05 19:09  awei040519  阅读(11)  评论(0)    收藏  举报